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
# 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]
  @optimistic_json    = options[:optimistic_json]
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/committee/request_unpacker.rb', line 11

def call
  # if Content-Type is empty or JSON, and there was a request body, try to
  # interpret it as JSON
  params = if !@request.content_type || @request.content_type =~ %r{application/json}
    parse_json
  elsif @optimistic_json
    parse_json rescue MultiJson::LoadError nil
  end

  params = if params
    params
  elsif @allow_form_params && @request.content_type == "application/x-www-form-urlencoded"
    # Actually, POST means anything in the request body, could be from
    # PUT or PATCH too. Silly Rack.
    indifferent_params(@request.POST)
  else
    {}
  end

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