Sha256: b7fb7b14cce6c11d45d48a9fee2219febf0a60c9818d140cc295cf4e78fac36e

Contents?: true

Size: 1020 Bytes

Versions: 2

Compression:

Stored size: 1020 Bytes

Contents

require 'goliath/rack/validator'

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
        include Goliath::Rack::Validator
        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)
          return validation_error(405, ERROR, "Allow" => methods.map{|m| m.to_s.upcase}.join(', ')) 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.4 lib/goliath/rack/validation/request_method.rb
goliath-0.9.2 lib/goliath/rack/validation/request_method.rb