fincのweb api開発事情

36

Upload: fumiya-shinozuka

Post on 18-Feb-2017

97 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: FiNCのWeb API開発事情
Page 2: FiNCのWeb API開発事情
Page 3: FiNCのWeb API開発事情
Page 4: FiNCのWeb API開発事情

FiNC

Page 5: FiNCのWeb API開発事情

FiNC = Microservices

Page 6: FiNCのWeb API開発事情

Microservices

Page 7: FiNCのWeb API開発事情

API

Page 8: FiNCのWeb API開発事情

Rails API

Page 9: FiNCのWeb API開発事情

API

Page 10: FiNCのWeb API開発事情

FiNC

Page 11: FiNCのWeb API開発事情

FiNC

Page 12: FiNCのWeb API開発事情

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

Page 13: FiNCのWeb API開発事情

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"}

Page 15: FiNCのWeb API開発事情
Page 16: FiNCのWeb API開発事情
Page 17: FiNCのWeb API開発事情
Page 18: FiNCのWeb API開発事情
Page 19: FiNCのWeb API開発事情
Page 20: FiNCのWeb API開発事情

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"}

Page 21: FiNCのWeb API開発事情

JSON Schema

Page 24: FiNCのWeb API開発事情
Page 25: FiNCのWeb API開発事情
Page 26: FiNCのWeb API開発事情

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

Page 27: FiNCのWeb API開発事情

( ) JSON Schema + prmd tips

docs/!"" schema #"" meta.yml #"" schema.json #"" schema.md !"" schemata #"" apis !"" resources

Page 28: FiNCのWeb API開発事情

Controller

class DailyStepsController < ApplicationController def show steps = DailyStep.where(user_id: current_user.id) render json: steps endend

Page 29: FiNCのWeb API開発事情

ActiveModelSerializer

class DailyStepSerializer < ActiveModel::Serializer attributes :id, :date, :stepend

Page 30: FiNCのWeb API開発事情

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

Page 31: FiNCのWeb API開発事情
Page 32: FiNCのWeb API開発事情
Page 35: FiNCのWeb API開発事情
Page 36: FiNCのWeb API開発事情