# class Rack::MethodOverride

### Constants

ALLOWED_METHODS
HTTP_METHODS
HTTP_METHOD_OVERRIDE_HEADER
METHOD_OVERRIDE_PARAM_KEY

### Public Class Methods

new(app) click to toggle source

    # File lib/rack/method_override.rb, line 11
    def initialize(app)
      @app = app
    end

### Public Instance Methods

call(env) click to toggle source

    # File lib/rack/method_override.rb, line 15
    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) click to toggle source

    # File lib/rack/method_override.rb, line 27
    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

### Private Instance Methods

allowed_methods() click to toggle source

    # File lib/rack/method_override.rb, line 40
    def allowed_methods
      ALLOWED_METHODS
    end

method_override_param(req) click to toggle source

    # File lib/rack/method_override.rb, line 44
    def method_override_param(req)
      req.POST[METHOD_OVERRIDE_PARAM_KEY]
    rescue Utils::InvalidParameterError, Utils::ParameterTypeError, QueryParser::ParamsTooDeepError
      req.get_header(RACK_ERRORS).puts "Invalid or incomplete POST params"
    rescue EOFError
      req.get_header(RACK_ERRORS).puts "Bad request content body"
    end