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)
  @check_content_type = options.fetch(:check_content_type, true)
  @optimistic_json    = options.fetch(:optimistic_json, false)
  @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
43
44
45
# 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, check_content_type: @check_content_type)
    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
  @error_class.new(400, :bad_request, $!.message).render
rescue Committee::NotFound
  raise if @raise
  @error_class.new(
    404,
    :not_found,
    "That request method and path combination isn't defined."
  ).render
rescue MultiJson::LoadError
  raise Committee::InvalidRequest if @raise
  @error_class.new(400, :bad_request, "Request body wasn't valid JSON.").render
end