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

Inherits:
Object
  • Object
show all
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

ERROR =
'Invalid request method'

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

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

Parameters:

  • app

    The app object

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

    The accepted request methods



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

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

Instance Attribute Details

- (Object) methods (readonly)

Returns the value of attribute methods



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

def methods
  @methods
end

Instance Method Details

- (Object) call(env)



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

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