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

RESTful API with Grape

RESTful API with Grape

Charles Lalonde

May 20, 2016
Tweet

Transcript

  1. RESTful API with
    Grape

    View full-size slide

  2. Hi, I’m Charles
    Rails Developer @ Unsplash

    View full-size slide

  3. Unsplash
    Do whatever you want high-resolution photos

    View full-size slide

  4. Why Grape?
    • Framework to build API
    • Easily extensible
    • Supports Rails, Sinatra 

    and others

    View full-size slide

  5. Setup Rails in 4 simple steps

    View full-size slide

  6. • app/
    • api/
    • v1/
    • base.rb
    Create folder structure
    Setup Rails in 4 simple steps

    View full-size slide

  7. Setup Rails in 4 simple steps
    Auto load the API files in your application.rb

    View full-size slide

  8. Mount the API in routes.rb
    Setup Rails in 4 simple steps

    View full-size slide

  9. Setup Rails in 4 simple steps
    • Versioning
    • Format
    • Endpoint mounting
    • Default headers
    • Logs

    View full-size slide

  10. { Grape } Basic

    View full-size slide

  11. { Grape } Basic
    • Call Overview
    • Headers
    • Parameters
    • Http Methods
    • Error Handling
    • Helpers

    View full-size slide

  12. Overview
    • Resource
    • Route params
    • HTTP methods
    • Parameters
    GET api.montrealrb.com/v1/:resource/:route_param

    View full-size slide

  13. Headers
    • Useful for bots
    • Alerting users of deprecated endpoints
    • Allow multiple origins
    • etc

    View full-size slide

  14. Parameters
    • Can manage optional and required params
    • Multiple parameter types
    • Custom validation

    View full-size slide

  15. HTTP methods
    • Supports all HTTP methods
    • Specify endpoint names

    View full-size slide

  16. Error handling
    • Message
    • Status

    View full-size slide

  17. Helpers
    • Create reusable code
    • Can include custom modules

    View full-size slide

  18. { Grape } gem extension

    View full-size slide

  19. { Grape } gem extension
    WineBouncer 

    Oauth2 protection with Doorkeeper

    https://github.com/antek-drzewiecki/wine_bouncer
    grape-rails-cache

    Server side cache

    https://github.com/monterail/grape-rails-cache
    grape-swagger

    Automatically generate documentation

    https://github.com/ruby-grape/grape-swagger

    View full-size slide

  20. { Grape } gem extension

    View full-size slide

  21. Unsplash API Stats
    API Requests
    230 740 265
    Last 30 days
    API Developers
    2 479
    Approved Applications
    184

    View full-size slide

  22. Connect
    @dechuck on Twitter, GitHub & Unsplash
    Thanks

    View full-size slide