Sha256: 67de7448e552922abaa34a43dee6e2daa9568c6d0011d6debb089f0303441d4e

Contents?: true

Size: 945 Bytes

Versions: 2

Compression:

Stored size: 945 Bytes

Contents

require 'goliath/rack/validation_error'

module Goliath
  module Rack
    module Validation
      # A middleware to validate that the request had a given HTTP method.
      #
      # @example
      #  use Goliath::Rack::Validation::RequestMethod, %w(GET POST)
      #
      class RequestMethod
        attr_reader :methods

        ERROR = 'Invalid request method'

        # Called by the framework to create the Goliath::Rack::Validation::RequestMethod validator
        #
        # @param app The app object
        # @param methods [Array] The accepted request methods
        # @return [Goliath::Rack::Validation::RequestMethod] The validator
        def initialize(app, methods = [])
          @app = app
          @methods = methods
        end

        def call(env)
          raise Goliath::Validation::Error.new(400, ERROR) unless methods.include?(env['REQUEST_METHOD'])
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
goliath-0.9.1 lib/goliath/rack/validation/request_method.rb
goliath-0.9.0 lib/goliath/rack/validation/request_method.rb