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

API Safari

API Safari

A survey on how to create an API using different libraries

The objective is to show that Ruby has a rich ecosystem for building API servers pick one that you like

Would be covering Grape and Webmachine for REST APIs and Celluloid for RPC Apis

Grape is like Sinatra but focussed on creating a REST API

Webmachine is a port of an Erlang library which treats the HTTP request-respose cycle as a state machine

Not covering rails-api as it is like Rails

Celluloid is a framework for building multithreaded programs Will be using it to build a RPC server

Deepak Kannan

June 22, 2013
Tweet

More Decks by Deepak Kannan

Other Decks in Technology

Transcript

  1. 1 # config/routes.rb 2 resources :orders 3 4 # app/controllers/orders_controller.rb

    5 class OrdersController < ApplicationController 6 respond_to :json 7 8 def index 9 end 10 11 def show 12 end 13 end 14 15 24
  2. 1 class ApiConstraints 2 def matches?(req) 3 # match req.headers['Accept']

    4 end 5 end 6 7 namespace :api do 8 scope(module: :v2, 9 constraints: ApiConstraints.new('...')) do 10 resources :products 11 end 12 end 13 14 RAILS IS POWERFUL 29
  3. DSL Less Lines of Code 1 module Starbucks 2 class

    API < Grape::API 3 version 'v2' , using: :header 4 end 5 end 30
  4. SPECIFY URL PREFIX 1 module Starbucks 2 class API <

    Grape::API 3 prefix 'starbucks' 4 version 'v2' 6 format :json 7 8 end 9 end 34
  5. SPECIFY DEFAULT VERSION 1 module Starbucks 2 class API <

    Grape::API 3 prefix 'starbucks' 4 version 'v2' 6 format :json 7 8 end 9 end 35
  6. SPECIFY FORMAT 1 module Starbucks 2 class API < Grape::API

    3 prefix 'starbucks' 4 version 'v2' 6 format :json 7 8 end 9 end 36
  7. 1 module Starbucks 2 class API < Grape::API 3 resource

    :orders do 4 desc "get a order" 5 get '/:id' do 6 order = Order.where(id: params.id).first 7 8 unless order 9 throw :error, status: 404, message:"not found" 11 end 12 13 header "Last-Modified", order.updated_at 15 order 16 end 17 end 18 end 19 end 38
  8. 1 module Starbucks 2 class API < Grape::API 3 resource

    :orders do 4 desc "get a order" 5 get '/:id' do 6 order = Order.where(id: params.id).first 7 8 unless order 9 throw :error, status: 404, message:"not found" 11 end 12 13 header "Last-Modified", order.updated_at 15 order 16 end 17 end 18 end 19 end INLINE DOCS 39
  9. 51

  10. resource_exists? service_available? is_authorized? forbidden? allow_missing_post? malformed_request? uri_too_long? known_content_type? valid_content_headers? valid_entity_length?

    options allowed_methods known_methods delete_resource delete_completed? post_is_create? create_path base_uri process_post content_types_provided content_types_accepted charsets_provided languages_provided language_chosen encodings_provided variances 36 CALLBACKS 52
  11. 1 class SeeOrder < Webmachine::Resource 2 def service_available? 3 #

    HTTP/1.1 503 Service Unavailable 4 # Retry-After: Fri, 21 Jun 2013 04:30:00 GMT 5 now = Time.now 6 morning = now.at_5_morning 7 if now < now.at_5_morning 8 response.headers['Retry-After'] = 9 morning.httpdate 10 false 11 else 12 true 13 end 14 end 15 end 59
  12. 1 class SeeOrder < Webmachine::Resource 2 def content_types_provided 3 [["application/json",

    :to_json]] 4 end 5 6 # Does the resource exist? Returning a falsey 7 # value (false or nil) 8 # will result in a '404 Not Found' response. 9 def resource_exists? 10 order 11 end 12 13 def to_json 14 order.to_json 15 end 16 end 66
  13. 1 class OrderController < ApplicationController 2 respond_to :json 3 #

    before_filter :find_order 4 # before_filter :resource_exists! 5 6 def show 7 @order = Order.find(params[:id]) 8 if @order 9 respond_with(@order) 10 else 11 # have to explicitly specify the http code 12 # and hook-in the callback 13 head 404 14 false 15 end 16 end 17 end 70
  14. 73

  15. 1 MyApp = Webmachine::Application.new do |app| 2 app.routes do 3

    add ['<URL>'], ResourceClass do |request| 4 request.method == "GET" && request.query["id"] 5 end 6 end 7 end 74
  16. “Webmachine is an executable model for HTTP, while Grape is

    a DSL for RESTful APIs.” - @dblock 78
  17. “In Grape you have to be disciplined about those API

    methods - they should represent resources, not RPC service endpoints” - @dblock 79
  18. “In Webmachine you implement resource callbacks, while in Grape you

    use procedural logic within the API method implementation” - @dblock 80