Class: Flon::API

Inherits:
Object
  • Object
show all
Defined in:
lib/flon/api.rb

Overview

This class is Flon’s Rack application.

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ API

Creates a new Flon::API object with the given API.

Parameters:

  • api (Object)

    the API to use; its class must respond to #router and the result must be a Router.

Raises:

  • (ArgumentError)


82
83
84
85
86
87
# File 'lib/flon/api.rb', line 82

def initialize(api)
  raise ArgumentError, "given API's class does not respond to #routes" unless valid_api?(api)

  @api    = api
  @router = api.class.router
end

Instance Method Details

#call(env) ⇒ Array

Implements the Rack interface.

Parameters:

  • env (Hash)

    a rack environment

Returns:

  • (Array)

    a rack-suitable response



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/flon/api.rb', line 93

def call(env)
  request = Rack::Request.new(env)

  method = http_method_to_symbol(request.request_method)
  path   = request.path_info

  response = case (match = @router.match(method, path))
             when :bad_path   then Response.new(404, '"404 Not Found"')
             when :bad_method then Response.new(405, '"405 Method Not Allowed"')
             else dispatch(method, request, match)
             end

  rack_response(method, response)
end