Class: Rack::MethodOverride

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

Constant Summary collapse

HTTP_METHODS =
%w[GET HEAD PUT POST DELETE OPTIONS PATCH LINK UNLINK]
METHOD_OVERRIDE_PARAM_KEY =
"_method"
HTTP_METHOD_OVERRIDE_HEADER =
"HTTP_X_HTTP_METHOD_OVERRIDE"
ALLOWED_METHODS =
%w[POST]

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ MethodOverride

Returns a new instance of MethodOverride.



15
16
17
# File 'lib/rack/method_override.rb', line 15

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rack/method_override.rb', line 19

def call(env)
  if allowed_methods.include?(env[REQUEST_METHOD])
    method = method_override(env)
    if HTTP_METHODS.include?(method)
      env[RACK_METHODOVERRIDE_ORIGINAL_METHOD] = env[REQUEST_METHOD]
      env[REQUEST_METHOD] = method
    end
  end

  @app.call(env)
end

#method_override(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/rack/method_override.rb', line 31

def method_override(env)
  req = Request.new(env)
  method = method_override_param(req) ||
    env[HTTP_METHOD_OVERRIDE_HEADER]
  begin
    method.to_s.upcase
  rescue ArgumentError
    env[RACK_ERRORS].puts "Invalid string for method"
  end
end