Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Grape による API 実装 in action

Kensuke Nagae
November 01, 2014

Grape による API 実装 in action

渋谷Ruby会議01 2014年11月1日 SHIDAX CULTURE HALL
Presented by Kensuke Nagae (@kyanny)
http://regional.rubykaigi.org/shibuya01/articles/0/01/10/member-1.html

Kensuke Nagae

November 01, 2014
Tweet

More Decks by Kensuke Nagae

Other Decks in Programming

Transcript

  1. 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}
  2. 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 }
  3. 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ͷ ผ໊
  4. 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 }
  5. ϓϨθϯςʔγϣϯ૚ 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ςϯϓϨʔτ૬౰ʣ
  6. ϓϨθϯςʔγϣϯ૚ 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" }
  7. commit 76b268be6410eb8a7d19bdea4e544e7d9824c4fb Date: Fri Jun 29 15:20:45 2012 +0100 !

    initial import $ find api app lib -name '*.rb' | xargs grep -v -E '^\s+#|^$' | wc -l 6490 $ find spec -name '*.rb' | xargs grep -v -E '^\s+#|^$' | wc -l 11235 quipper/api ࠷ॳͷίϛοτ͸ೋ೥Ҏ্લ ࣮૷ίʔυͱςετίʔυͷߦ਺ʢ˞.PEFMΛআ͘ʣ
  8. commit a8844b3658b1a03aea70a660d955b22e316fc2b8 Date: Wed Jul 18 12:05:14 2012 +0100 !

    Initial commit. $ find lib -name '*.rb' | xargs grep -v -E '^\s+#|^$' | wc -l 8511 $ find spec -name '*.rb' | xargs grep -v -E '^\s+#|^$' | wc -l 19557 quipper/schema ͜Ε΋࠷ॳͷίϛοτ͸ೋ೥Ҏ্લ ࣮૷ίʔυͱςετίʔυͷߦ਺ .PEFM͚ͩ੾Γग़ͨ͠HFN
  9. ؀ڥʢ଍ճΓʣͷμϧ͞ commit f7e6134df7f44cbff8e68fecdff443ddc42cbb71 Date: Thu Oct 17 12:06:45 2013 +0900

    ! initial setup for rails ! Procfile | 2 +- Rakefile | 22 +++++----------------- app/assets/images/rails.png | Bin 0 -> 6646 bytes app/assets/javascripts/application.js | 13 +++++++++++++ app/assets/stylesheets/application.css | 13 +++++++++++++ app/controllers/application_controller.rb | 3 +++ app/helpers/application_helper.rb | 2 ++ app/mailers/.gitkeep | 0 app/models/.gitkeep | 0 app/views/layouts/application.html.erb | 14 ++++++++++++++ config.ru | 76 +++------------------------------------------------------------------------- config/app.rb | 56 +++----------------------------------------------------- config/application.rb | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ config/boot.rb | 6 +++++- config/env.rb | 5 ----- config.ru 1 file changed, 3 insertions(+), 73 deletions(-) ! config/app.rb 1 file changed, 3 insertions(+), 53 deletions(-) FOWJSPONFOUTJOJUJBMJ[FSTͷ࢓૊Έ͕ͳ͍ͷͰ ࣗલͰॻ͘ඞཁ͕͋ͬͨ
  10. ๻͸͜͏ࢥͬͨοε QuipperApi::Application.routes.draw do mount Api::Base => '/' end (SBQFΞϓϦέʔγϣϯΛ 3BJMT͔ΒNPVOU͢Δ

    3BJMTΛ౔୆ͱͯ͠࢖͏ w (SBQF୯ମͰ͸࢖Θͣ࠷ॳ͔Β3BJMTͱηοτͰ࢖͏ͱָ