Class: LIFXHTTP::MethodOverride

Inherits:
Object
  • Object
show all
Defined in:
lib/lifx-http/method_override.rb

Constant Summary collapse

HTTP_METHODS =
%w(GET HEAD PUT POST DELETE OPTIONS PATCH LINK UNLINK)
METHOD_OVERRIDE_PARAM_KEY =
"_method".freeze
HTTP_METHOD_OVERRIDE_HEADER =
"HTTP_X_HTTP_METHOD_OVERRIDE".freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ MethodOverride

Returns a new instance of MethodOverride.



8
9
10
# File 'lib/lifx-http/method_override.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/lifx-http/method_override.rb', line 12

def call(env)
  method = method_override(env)
  if HTTP_METHODS.include?(method)
    env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
    env["REQUEST_METHOD"] = method
  end

  @app.call(env)
end

#method_override(env) ⇒ Object



22
23
24
25
26
27
# File 'lib/lifx-http/method_override.rb', line 22

def method_override(env)
  req = Rack::Request.new(env)
  method = req.params[METHOD_OVERRIDE_PARAM_KEY] ||
    env[HTTP_METHOD_OVERRIDE_HEADER]
  method.to_s.upcase
end