Module: Angelo::ParamsParser

Included in:
Base
Defined in:
lib/angelo/params_parser.rb

Constant Summary collapse

EMPTY_JSON =
'{}'

Instance Method Summary collapse

Instance Method Details

#content_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/angelo/params_parser.rb', line 47

def content_type? type
  if request.headers[CONTENT_TYPE_HEADER_KEY]
    request.headers[CONTENT_TYPE_HEADER_KEY].split(SEMICOLON).include? type
  else
    nil
  end
end

#form_encoded?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/angelo/params_parser.rb', line 39

def form_encoded?
  content_type? FORM_TYPE
end

#json?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/angelo/params_parser.rb', line 43

def json?
  content_type? JSON_TYPE
end

#parse_formencoded(str) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/angelo/params_parser.rb', line 11

def parse_formencoded str
  str.split(AMPERSAND).reduce(SymHash.new) do |p, kv|
    key, value = kv.split(EQUALS).map {|s| CGI.unescape s}
    p[key] = value
    p
  end
end

#parse_post_bodyObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/angelo/params_parser.rb', line 23

def parse_post_body
  body = request.body.to_s
  case
  when form_encoded?
    parse_formencoded body
  when json? && !body.empty?
    SymHash.new JSON.parse body
  else
    {}
  end
end

#parse_query_stringObject



19
20
21
# File 'lib/angelo/params_parser.rb', line 19

def parse_query_string
  parse_formencoded(request.query_string || EMPTY_STRING)
end

#parse_query_string_and_post_bodyObject



35
36
37
# File 'lib/angelo/params_parser.rb', line 35

def parse_query_string_and_post_body
  parse_query_string.merge! parse_post_body
end