Sha256: bd24222d017d81af3acc64afe6704c6d31857d351e5d996451433dabb4cc41e5

Contents?: true

Size: 919 Bytes

Versions: 3

Compression:

Stored size: 919 Bytes

Contents

module Repia
  ##
  # 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 https://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.
  #
  class HttpMethodNotAllowed
    def initialize(app)
      @app = app
    end

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
repia-0.2.0 lib/repia/middlewares.rb
repia-0.1.1 lib/repia/middlewares.rb
repia-0.1.0 lib/repia/middlewares.rb