Module: Angelo::ParamsParser

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

Constant Summary collapse

EMPTY_JSON =
'{}'
SEMICOLON =
';'
EQUALS =
'='
AMPERSAND =
'&'

Instance Method Summary collapse

Instance Method Details

#content_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/angelo/params_parser.rb', line 60

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)


52
53
54
# File 'lib/angelo/params_parser.rb', line 52

def form_encoded?
  content_type? FORM_TYPE
end

#json?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/angelo/params_parser.rb', line 56

def json?
  content_type? JSON_TYPE
end

#parse_formencoded(str) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/angelo/params_parser.rb', line 14

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

#parse_post_bodyObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/angelo/params_parser.rb', line 26

def parse_post_body
  body = request.body.to_s
  qs = parse_query_string
  case
  when form_encoded?
    body = parse_formencoded body
    qs.merge! body
  when json?
    body = EMPTY_JSON if body.empty?
    body = JSON.parse body
    recurse_symhash qs.merge! body
  else
    qs
  end
end

#parse_query_stringObject



22
23
24
# File 'lib/angelo/params_parser.rb', line 22

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

#recurse_symhash(h) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/angelo/params_parser.rb', line 42

def recurse_symhash h
  h.each do |k,v|
    if Hash === v
      h[k] = Responder.symhash.merge! v
      recurse_symhash h[k]
    end
  end
  h
end