Class: Goliath::Rack::Validation::RequestMethod

Inherits:
Object
  • Object
show all
Includes:
Goliath::Rack::Validator
Defined in:
lib/goliath/rack/validation/request_method.rb

Overview

A middleware to validate that the request had a given HTTP method.

Examples:

use Goliath::Rack::Validation::RequestMethod, %w(GET POST)

Constant Summary collapse

ERROR =
'Invalid request method'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Goliath::Rack::Validator

safely, validation_error

Constructor Details

#initialize(app, methods = []) ⇒ Goliath::Rack::Validation::RequestMethod

Called by the framework to create the Goliath::Rack::Validation::RequestMethod validator

Parameters:

  • app

    The app object

  • methods (Array) (defaults to: [])

    The accepted request methods



22
23
24
25
# File 'lib/goliath/rack/validation/request_method.rb', line 22

def initialize(app, methods = [])
  @app = app
  @methods = Array(methods)
end

Instance Attribute Details

#methodsObject (readonly)

Returns the value of attribute methods.



13
14
15
# File 'lib/goliath/rack/validation/request_method.rb', line 13

def methods
  @methods
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
# File 'lib/goliath/rack/validation/request_method.rb', line 27

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