Class: Committee::RequestUnpacker

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/request_unpacker.rb

Instance Method Summary collapse

Constructor Details

#initialize(request, options = {}) ⇒ RequestUnpacker

Returns a new instance of RequestUnpacker.



5
6
7
8
9
10
11
12
13
14
# File 'lib/committee/request_unpacker.rb', line 5

def initialize(request, options={})
  @request = request

  @allow_form_params  = options[:allow_form_params]
  @allow_get_body     = options[:allow_get_body]
  @allow_query_params = options[:allow_query_params]
  @coerce_form_params = options[:coerce_form_params]
  @optimistic_json    = options[:optimistic_json]
  @schema_validator   = options[:schema_validator]
end

Instance Method Details

#callObject



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
46
47
48
# File 'lib/committee/request_unpacker.rb', line 16

def call
  # if Content-Type is empty or JSON, and there was a request body, try to
  # interpret it as JSON
  params = if !@request.media_type || @request.media_type =~ %r{application/(?:.*\+)?json}
    parse_json
  elsif @optimistic_json
    begin
      parse_json
    rescue JSON::ParserError
      nil
    end
  end

  params = if params
    params
  elsif @allow_form_params && %w[application/x-www-form-urlencoded multipart/form-data].include?(@request.media_type)
    # Actually, POST means anything in the request body, could be from
    # PUT or PATCH too. Silly Rack.
    p = @request.POST

    @schema_validator.coerce_form_params(p) if @coerce_form_params

    p
  else
    {}
  end

  if @allow_query_params
    [indifferent_params(@request.GET).merge(params), headers]
  else
    [params, headers]
  end
end