View
99
Download
4
Category
Preview:
Citation preview
FiNC
FiNC = Microservices
Microservices
API
Rails API
API
FiNC
FiNC
Grape
module V1 class UsersApi < Grape::API resource :users do params do requires :id, type: Integer end get '/:id', rabl: '/v1/users/index' do @users = User.limit(10) end
params do requires :id, type: Integer optional :name, type: String end patch '/:id', rabl: '/v1/users/show' do @user = User.create(user_params) end end end end
rabl
object @userattributes :id, :name, :created_at, :updated_at
{ "id": 1, "name": "shinofumijp", "created_at": "2017-02-02T10:13:52.000+09:00", "updated_at": "2017-02-02T10:13:52.000+09:00"}
ActiveModelSerializer
# eitherclass SomeResource < ActiveRecord::Base # columns: title, bodyend# orclass SomeResource < ActiveModelSerializers::Model attributes :title, :bodyend
class SomeSerializer < ActiveModel::Serializer attribute :title, key: :name attributes :bodyend
{ "name": "namevalue", "body": "bodyvalue"}
JSON Schema
JSON Schema
"$schema": http://json-schema.org/draft-04/hyper-schematitle: Daily Steps Countdescription: Daily Steps Count Resourcestability: prototypestrictProperties: truetype:- objectdefinitions: date: description: date (JST) example: "2016-11-01" type: string
( ) JSON Schema + prmd tips
docs/!"" schema #"" meta.yml #"" schema.json #"" schema.md !"" schemata #"" apis !"" resources
Controller
class DailyStepsController < ApplicationController def show steps = DailyStep.where(user_id: current_user.id) render json: steps endend
ActiveModelSerializer
class DailyStepSerializer < ActiveModel::Serializer attributes :id, :date, :stepend
it 'returns expected response' do expect(response.status).to eq 200 json = JSON.parse(response.body) expect(json['id']).to be_kind_of Integer expect(json[‘step']).to be_kind_of Integerend
it 'returns expected response' do expect(response.status).to eq 200 assert_schema_conformend
Recommended