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

Grape による API 実装 in action

Fffd8bf62b842a46803fb07792029fa4?s=47 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

Fffd8bf62b842a46803fb07792029fa4?s=128

Kensuke Nagae

November 01, 2014
Tweet

Transcript

  1. (SBQFʹΑΔ"1*࣮૷ JOBDUJPO ೥݄೔ ौ୩3VCZձٞ ,FOTVLF/BHBF

  2. Kensuke Nagae @kyanny http://www.quipperschool.com/

  3. ࠓ೔࿩͢͜ͱ w (SBQFͬ͟ͱ঺հ w 2VJQQFSͰͷߏ੒ w (SBQFͷμϧ͍ͱ͜Ζ

  4. ˞ࢿྉ͸ެ։͠·͢ ݐઃ༧ఆ஍ https://speakerdeck.com/kyanny

  5. 3BJMT͸TUBST 4JOBUSB͸TUBST

  6. (SBQFͰάάΔͱ ੈքͰҐ

  7. ˑAn opinionated micro-framework for creating REST-like APIs in Ruby.˒ ᨳ͍จ۟

  8. ˑAn opinionated micro-framework for creating REST-like APIs in Ruby.˒ ๻͸͜͏ࢥͬͨοε

    %4-ଟΊ 4JOBUSBࣅ 3&45ͱ͸ؔ܎ͳ͍+40/"1*
  9. 4JOBUSB෩ͷ%4- require 'grape' ! class API < Grape::API get do

    {ok: true} end end ! run API
  10. 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}
  11. 4JOBUSB෩ͷ%4- require 'grape' ! class API < Grape::API format :json

    get do {ok: true} end end ! run API
  12. 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 }
  13. 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ͷ ผ໊
  14. 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 }
  15. ϓϨθϯςʔγϣϯ૚ 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ςϯϓϨʔτ૬౰ʣ
  16. ϓϨθϯςʔγϣϯ૚ 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" }
  17. 2VJQQFSͰͷߏ੒ Database API ୯Ұͷ"1*ʹ਺छྨͷΫϥΠΞϯτ

  18. 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Λআ͘ʣ
  19. 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
  20. όʔδϣχϯά ࠜݩ͔ΒΨοπϦ෼͚͍ͯΔ /app1/v1/courses/:id /app2/v1/authentications /app3/v1/student_groups/:id/memberships

  21. ๻͸͜͏ࢥͬͨοε ࠜݩ͔ΒΨοπϦ෼͚͍ͯΔ Wͱ͔ෆཁͳͷͰ͸ʜ /app1/v1/courses/:id /app2/v1/authentications /app3/v1/student_groups/:id/memberships

  22. ཧ૝ਤʢ/FUqJYͷ"1*ʣ http://techblog.netflix.com/2012/07/embracing-differences-inside-netflix.html

  23. ݱ࣮ʢಛʹਤͷԼଆʣ http://techblog.netflix.com/2012/07/embracing-differences-inside-netflix.html w ΤϯυϙΠϯτ͸෼͚ͯΔͷͰPL w ڞ༗͍ͯ͠Δ&OUJUZͷඇޓ׵ͳมߋ͕ා͍ w ݁ہ&OUJUZ΋ݸผʹ࡞ͬͯ͠·ͬͯΔ

  24. ؀ڥʢ଍ճΓʣͷμϧ͞ 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ͷ࢓૊Έ͕ͳ͍ͷͰ ࣗલͰॻ͘ඞཁ͕͋ͬͨ
  25. (SBQFPO3BJMT QuipperApi::Application.routes.draw do mount Api::Base => '/' end (SBQFΞϓϦέʔγϣϯΛ 3BJMT͔ΒNPVOU͢Δ

  26. ๻͸͜͏ࢥͬͨοε QuipperApi::Application.routes.draw do mount Api::Base => '/' end (SBQFΞϓϦέʔγϣϯΛ 3BJMT͔ΒNPVOU͢Δ

    3BJMTΛ౔୆ͱͯ͠࢖͏ w (SBQF୯ମͰ͸࢖Θͣ࠷ॳ͔Β3BJMTͱηοτͰ࢖͏ͱָ
  27. ؀ڥʢ֎෦࿈ܞʣͷμϧ͞ OFXSFMJD@SQNΛ(SBQFʹରԠͤ͞Δ OFXSFMJDHSBQFͱ͍͏HFN͕͋Δ͕ ϞϯΩʔύον͕ᜁͬͯ੝େʹյΕͨ͜ͱ͕͋ͬͨ IUUQTHJUIVCDPNYJONJOMBCTOFXSFMJDHSBQFQVMM

  28. ؀ڥʢ֎෦࿈ܞʣͷμϧ͞ OFXSFMJD@SQNͷ৽ػೳ͕ϞϯΩʔύονΛյͯ͠͠·͍ /FX3FMJDͷUSBOTBDUJPOTͰຊདྷΤϯυϙΠϯτ͝ͱʹ ·ͱΊΒΕΔ΂͖΋ͷ͕શ෦1SPDDBMMҰݸʹ·ͱΊΒΕͨ IUUQTHJUIVCDPNYJONJOMBCTOFXSFMJDHSBQFJTTVFT

  29. ݁࿦ w (SBQF୯ମͰ͸ͭΒ͘ͳΔ w 3BJMTͷྗΛआΓΑ͏ w ࣗྗͰ͕Μ͹Δ֮ޛ΋͍Δ

  30. ͓ΘΓ