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.



3
4
5
6
7
8
9
10
11
# File 'lib/committee/request_unpacker.rb', line 3

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

  @allow_form_params  = options[:allow_form_params]
  @allow_query_params = options[:allow_query_params]
  @coerce_form_params = options[:coerce_form_params]
  @optimistic_json    = options[:optimistic_json]
  @schema             = options[:schema]
end

Instance Method Details

#callObject



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

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 && @request.media_type == "application/x-www-form-urlencoded"
    # Actually, POST means anything in the request body, could be from
    # PUT or PATCH too. Silly Rack.
    p = @request.POST

    if @coerce_form_params && @schema
      Committee::StringParamsCoercer.new(p, @schema).call!
    end

    p
  else
    {}
  end

  if @allow_query_params
    indifferent_params(@request.GET).merge(params)
  else
    params
  end
end