Peeps-UUIDs is a very basic contact management system implemented as an API that follows the JSON API spec. Peeps-UUIDs is based on https://github.com/cerebris/peeps.
Other apps will soon be written to demonstrate writing a consumer for this API.
This app requires that postgresql be installed locally. General instructions are available here for many operating systems.
After cloning this repo, run the following:
bundleEnsure that your config/database.yml is configured properly, and then run:
rake db:create db:migrateStart your server:
rails serverActually, by using docker-compose, no dependencies (including Postgres) need to be installed on your machine.
After cloning the repo, uncomment these three lines in config/database.yml:
host: <%= ENV['POSTGRES_PORT_5432_TCP_ADDR'] %>
port: <%= ENV['POSTGRES_PORT_5432_TCP_PORT'] %>
username: postgres
Next, cd to the root directory and run the following:
docker-compose up --build
This will build and start the two Docker containers: one for Postgres and one for the Rails app. They are set to link ports.
In a new Terminal window, run docker ps to determine which container is running the Rails app. Then run the following:
docker exec -it <CONTAINER ID> /bin/bash
This will open a bash session in that container where you will run this line:
rake db:create db:migrate
You are now all set to go, with the Rails app responding to requests at localhost:3000.
The instructions below were followed to create this app from scratch.
rails new peeps-uuids -d postgresql --skip-javascriptThe default database.yml may not work for your configuration, so you will need to set this up based on your installation.
rake db:createAdd the gem to your Gemfile
gem 'jsonapi-resources'Then bundle
bundleMake the following changes to application_controller.rb
class ApplicationController < ActionController::Base
include JSONAPI::ActsAsResourceController
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :null_session
endOR
class ApplicationController < JSONAPI::ResourceController
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :null_session
endYou can also do this on a per controller basis in your app, if only some controllers will serve the API.
Edit config/environments/development.rb
Eager loading of classes is recommended. The code will work without it, but I think it's the right way to go. See http://blog.plataformatec.com.br/2012/08/eager-loading-for-greater-good/
# Eager load code on boot so JSONAPI-Resources resources are loaded and processed globally
config.eager_load = trueconfig.consider_all_requests_local = falseThis will prevent the server from returning the HTML formatted error messages when an exception happens. Not strictly necessary, but it makes for nicer output when debugging using curl or a client library.
Create a migration to enable UUID support.
rails g migration EnableUuidsEdit the migration
class EnableUuids < ActiveRecord::Migration
def change
enable_extension 'uuid-ossp'
end
endrake db:migrateCreate an initializer, such as config/initializers/jsonapi.rb, that contains the following:
JSONAPI.configure do |config|
# Allowed values are :integer(default), :uuid, :string, or a proc
config.resource_key_type = :uuid
endThis setting could alternatively be made on a per-resource basis.
Use the standard rails generator to create a model for Contacts and one for related PhoneNumbers
rails g model Contact first_name:string last_name:string email:string twitter:stringEdit the migration to set the id to use uuids
class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts, id: :uuid do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :twitter
t.timestamps
end
end
endEdit the model
class Contact < ApplicationRecord
has_many :phone_numbers
### Validations
validates :first_name, presence: true
validates :last_name, presence: true
def self.creatable_fields(context)
super + [:id]
end
endCreate the PhoneNumber model
rails g model PhoneNumber contact_id:integer name:string phone_number:stringEdit the migration for uuid
class CreatePhoneNumbers < ActiveRecord::Migration
def change
create_table :phone_numbers, id: :uuid do |t|
t.uuid :contact_id
t.string :name
t.string :phone_number
t.timestamps
end
end
endEdit the model
class PhoneNumber < ApplicationRecord
belongs_to :contact
def self.creatable_fields(context)
super + [:id]
end
endrake db:migrateUse the rails generator to create empty controllers. These will be inherit methods from the ResourceController so they will know how to respond to the standard REST methods.
rails g controller Contacts --skip-assets
rails g controller PhoneNumbers --skip-assetsWe need a directory to hold our resources. Let's put in under our app directory
mkdir app/resourcesCreate a new file for each resource. This must be named in a standard way so it can be found. This should be the single underscored name of the model with _resource.rb appended. For Contacts this will be contact_resource.rb.
Make the two resource files
contact_resource.rb
class ContactResource < JSONAPI::Resource
attributes :first_name, :last_name, :email, :twitter
has_many :phone_numbers
endand phone_number_resource.rb
class PhoneNumberResource < JSONAPI::Resource
attributes :name, :phone_number
has_one :contact
filter :contact
endRequire jsonapi/routing_ext
require 'jsonapi/routing_ext'Add the routes for the new resources
UUID_regex = /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(,[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})*/
jsonapi_resources