module Angelo::ParamsParser

Constants

EMPTY_JSON

Public Instance Methods

content_type?(type) click to toggle source
# File lib/angelo/params_parser.rb, line 56
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?() click to toggle source
# File lib/angelo/params_parser.rb, line 48
def form_encoded?
  content_type? FORM_TYPE
end
json?() click to toggle source
# File lib/angelo/params_parser.rb, line 52
def json?
  content_type? JSON_TYPE
end
parse_formencoded(str) click to toggle source
# 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_body() click to toggle source
# File lib/angelo/params_parser.rb, line 23
def parse_post_body
  body = request_body rescue request.body.to_s
  case
  when form_encoded?
    parse_formencoded body
  when json? && !body.empty?
    parsed_body = JSON.parse body
    parsed_body = SymHash.new parsed_body if Hash === parsed_body
    parsed_body
  else
    {}
  end
end
parse_query_string() click to toggle source
# 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_body() click to toggle source
# File lib/angelo/params_parser.rb, line 37
def parse_query_string_and_post_body
  parsed_body = parse_post_body
  case parsed_body
  when Hash
    parse_query_string.merge! parse_post_body
  when Array
    self.request_body = parsed_body
    parse_query_string
  end
end