Class: Rack::RestBook

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/rest_book.rb

Defined Under Namespace

Classes: RestBookError

Constant Summary collapse

HTTP_METHODS =
%w(GET HEAD PUT POST DELETE OPTIONS)
METHOD_OVERRIDE_PARAM_KEY =
'_method'.freeze
HTTP_METHOD_OVERRIDE_HEADER =
'HTTP_X_HTTP_METHOD_OVERRIDE'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RestBook

Returns a new instance of RestBook.



9
# File 'lib/rack/rest_book.rb', line 9

def initialize(app); @app = app end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rack/rest_book.rb', line 11

def call(env)
  if env['REQUEST_METHOD'] == 'POST'
    req = Request.new env
    meth = (req.params[METHOD_OVERRIDE_PARAM_KEY] || 'GET').to_s.upcase
    raise RestBookError, "invalid HTTP verb for method override: #{req.params[METHOD_OVERRIDE_PARAM_KEY]}" unless HTTP_METHODS.include?(meth)
    env['rack.methodoverride.original_method'] = env['REQUEST_METHOD'] and env['REQUEST_METHOD'] = meth unless meth == env['REQUEST_METHOD']
  end
  @app.call env
end