Class: Committee::Middleware::RequestValidation

Inherits:
Base
  • Object
show all
Defined in:
lib/committee/middleware/request_validation.rb

Instance Method Summary collapse

Methods inherited from Base

#call

Constructor Details

#initialize(app, options = {}) ⇒ RequestValidation

Returns a new instance of RequestValidation.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/committee/middleware/request_validation.rb', line 3

def initialize(app, options={})
  super
  @allow_form_params  = options.fetch(:allow_form_params, true)
  @allow_query_params = options.fetch(:allow_query_params, true)
  @optimistic_json    = options.fetch(:optimistic_json, false)
  @raise              = options[:raise]
  @strict             = options[:strict]

  # deprecated
  @allow_extra = options[:allow_extra]
end

Instance Method Details

#handle(request) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/committee/middleware/request_validation.rb', line 15

def handle(request)
  request.env[@params_key] = Committee::RequestUnpacker.new(
    request,
    allow_form_params:  @allow_form_params,
    allow_query_params: @allow_query_params,
    optimistic_json:    @optimistic_json
  ).call

  if link = @router.find_request_link(request)
    validator = Committee::RequestValidator.new(link)
    validator.call(request, request.env[@params_key])
    @app.call(request.env)
  elsif @strict
    raise Committee::NotFound
  else
    @app.call(request.env)
  end
rescue Committee::BadRequest, Committee::InvalidRequest
  raise if @raise
  render_error(400, :bad_request, $!.message)
rescue Committee::NotFound
  raise if @raise
  render_error(404, :not_found,
    "That request method and path combination isn't defined.")
rescue MultiJson::LoadError
  raise Committee::InvalidRequest if @raise
  render_error(400, :bad_request, "Request body wasn't valid JSON.")
end