4JOBUSB෩ͷ%4- require 'grape' ! class API < Grape::API get do {ok: true} end end ! run API $ http localhost:9292 HTTP/1.1 200 OK Connection: keep-alive Content-Length: 11 Content-Type: text/plain Server: thin ! {:ok=>true}
4JOBUSB෩ͷ%4- require 'grape' ! class API < Grape::API format :json get do {ok: true} end end ! run API $ http localhost:9292 HTTP/1.1 200 OK Connection: keep-alive Content-Length: 11 Content-Type: application/json Server: thin ! { "ok": true }
4JOBUSB෩Ͱͳ͍%4- class API < Grape::API format :json prefix '/quipper' version 'v1' namespace :app1 do resources :users do get ':id' do User.find(params.id) end end end end OBNFTQBDFͷ ผ໊
4JOBUSB෩Ͱͳ͍%4- class API < Grape::API format :json prefix '/quipper' version 'v1' namespace :app1 do resources :users do get ':id' do User.find(params.id) end end end end $ http localhost:9292/quipper/v1/app1/users/1 HTTP/1.1 200 OK Connection: keep-alive Content-Length: 8 Content-Type: application/json Server: thin ! { "id": 1 }
ϓϨθϯςʔγϣϯ require 'grape-entity' ! class UserEntity < Grape::Entity expose :id expose :name end ! class API < Grape::API format :json resources :users do get ':id' do present User.find(params.id), with: UserEntity end end end HSBQFFOUJUZHFN͕7JFX୲ ʢKCVJMEFSͰ͍͏CVJMEFSςϯϓϨʔτ૬ʣ
ϓϨθϯςʔγϣϯ require 'grape-entity' ! class UserEntity < Grape::Entity expose :id expose :name end ! class API < Grape::API format :json resources :users do get ':id' do present User.find(params.id), with: UserEntity end end end $ http localhost:9292/users/1 HTTP/1.1 200 OK Connection: keep-alive Content-Length: 27 Content-Type: application/json Server: thin ! { "id": 1, "name": "Anonymous" }
ݱ࣮ʢಛʹਤͷԼଆʣ http://techblog.netflix.com/2012/07/embracing-differences-inside-netflix.html w ΤϯυϙΠϯτ͚ͯΔͷͰPL w ڞ༗͍ͯ͠Δ&OUJUZͷඇޓͳมߋ͕ා͍ w ݁ہ&OUJUZݸผʹ࡞ͬͯ͠·ͬͯΔ
͜͏ࢥͬͨοε QuipperApi::Application.routes.draw do mount Api::Base => '/' end (SBQFΞϓϦέʔγϣϯΛ 3BJMT͔ΒNPVOU͢Δ 3BJMTΛͱͯ͠͏ w (SBQF୯ମͰΘͣ࠷ॳ͔Β3BJMTͱηοτͰ͏ͱָ