lib/angelo/params_parser.rb in angelo-0.1.0 vs lib/angelo/params_parser.rb in angelo-0.1.1

- old
+ new

@@ -1,17 +1,22 @@ require 'cgi' module Angelo + class FormEncodingError < StandardError; end + module ParamsParser EMPTY_JSON = '{}' SEMICOLON = ';' + EQUALS = '=' + AMPERSAND = '&' def parse_formencoded str - str.split('&').reduce(Responder.symhash) do |p, kv| - key, value = kv.split('=').map {|s| CGI.unescape s} + raise FormEncodingError unless str.empty? or str.index EQUALS + 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 @@ -19,18 +24,22 @@ parse_formencoded(request.query_string || '') end 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 + qs.merge! body + else + qs end - parse_query_string.merge! body end def form_encoded? content_type? FORM_TYPE end @@ -38,10 +47,14 @@ def json? content_type? JSON_TYPE end def content_type? type - request.headers[CONTENT_TYPE_HEADER_KEY].split(SEMICOLON).include? type + if request.headers[CONTENT_TYPE_HEADER_KEY] + request.headers[CONTENT_TYPE_HEADER_KEY].split(SEMICOLON).include? type + else + nil + end end end end