Class: Repia::Middlewares::HttpMethodNotAllowed

Inherits:
Object
  • Object
show all
Defined in:
lib/repia/middlewares/http_method_not_allowed.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.



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

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

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