Class: Repia::HttpMethodNotAllowed

Inherits:
Object
  • Object
show all
Defined in:
lib/repia/middlewares.rb

Overview

This class serves as a middleware to handle Method Not Allowed error (which is not handled by show_exceptions for some reason).

Code was excerpted from gist.github.com/viola/1243572 and was modified to serve a JSON response.

Add it after ActionDispatch::RequestId to keep the request ID in the response headers.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ HttpMethodNotAllowed

Returns a new instance of HttpMethodNotAllowed.



13
14
15
# File 'lib/repia/middlewares.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/repia/middlewares.rb', line 17

def call(env)
  if !ActionDispatch::Request::HTTP_METHODS.include?(env["REQUEST_METHOD"].upcase)
    Rails.logger.info("ActionController::UnknownHttpMethod: #{env.inspect}")
    [405, 
     {"Content-Type" => "application/json; charset=utf-8"},
     [JSON.generate({errors: ["Method not allowed"]})]
    ]
  else
    @status, @headers, @response = @app.call(env)
    [@status, @headers, @response]
  end
end