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. (SBQFʹΑΔ"1*࣮૷
    JOBDUJPO
    ೥݄೔
    ौ୩3VCZձٞ
    ,FOTVLF/BHBF

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. 3BJMT͸TUBST
    4JOBUSB͸TUBST

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. ˑAn opinionated micro-framework for creating
    REST-like APIs in Ruby.˒
    ๻͸͜͏ࢥͬͨοε
    %4-ଟΊ 4JOBUSBࣅ
    3&45ͱ͸ؔ܎ͳ͍+40/"1*

    View full-size slide

  9. 4JOBUSB෩ͷ%4-
    require 'grape'
    !
    class API < Grape::API
    get do
    {ok: true}
    end
    end
    !
    run API

    View full-size slide

  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}

    View full-size slide

  11. 4JOBUSB෩ͷ%4-
    require 'grape'
    !
    class API < Grape::API
    format :json
    get do
    {ok: true}
    end
    end
    !
    run API

    View full-size slide

  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
    }

    View full-size slide

  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ͷ
    ผ໊

    View full-size slide

  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
    }

    View full-size slide

  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ςϯϓϨʔτ૬౰ʣ

    View full-size slide

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

    View full-size slide

  17. 2VJQQFSͰͷߏ੒
    Database
    API ୯Ұͷ"1*ʹ਺छྨͷΫϥΠΞϯτ

    View full-size slide

  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Λআ͘ʣ

    View full-size slide

  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

    View full-size slide

  20. όʔδϣχϯά
    ࠜݩ͔ΒΨοπϦ෼͚͍ͯΔ
    /app1/v1/courses/:id
    /app2/v1/authentications
    /app3/v1/student_groups/:id/memberships

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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ͷ࢓૊Έ͕ͳ͍ͷͰ
    ࣗલͰॻ͘ඞཁ͕͋ͬͨ

    View full-size slide

  25. (SBQFPO3BJMT
    QuipperApi::Application.routes.draw do
    mount Api::Base => '/'
    end
    (SBQFΞϓϦέʔγϣϯΛ
    3BJMT͔ΒNPVOU͢Δ

    View full-size slide

  26. ๻͸͜͏ࢥͬͨοε
    QuipperApi::Application.routes.draw do
    mount Api::Base => '/'
    end
    (SBQFΞϓϦέʔγϣϯΛ
    3BJMT͔ΒNPVOU͢Δ
    3BJMTΛ౔୆ͱͯ͠࢖͏
    w (SBQF୯ମͰ͸࢖Θͣ࠷ॳ͔Β3BJMTͱηοτͰ࢖͏ͱָ

    View full-size slide

  27. ؀ڥʢ֎෦࿈ܞʣͷμϧ͞
    OFXSFMJD@SQNΛ(SBQFʹରԠͤ͞Δ
    OFXSFMJDHSBQFͱ͍͏HFN͕͋Δ͕
    ϞϯΩʔύον͕ᜁͬͯ੝େʹյΕͨ͜ͱ͕͋ͬͨ
    IUUQTHJUIVCDPNYJONJOMBCTOFXSFMJDHSBQFQVMM

    View full-size slide

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

    View full-size slide

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

    View full-size slide