Sha256: c5172646265a202eaecbb83d7365d710222ace6077a6a465e07ece3caed33489

Contents?: true

Size: 999 Bytes

Versions: 1

Compression:

Stored size: 999 Bytes

Contents

module Repia
  module Middlewares
    ##
    # 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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
repia-0.3.0 lib/repia/middlewares/http_method_not_allowed.rb