Sha256: 94f0035511959d44d2fa948cc05fdb8af97af942188a3ec758961c43aab2938d
Contents?: true
Size: 1.26 KB
Versions: 1
Compression:
Stored size: 1.26 KB
Contents
require 'cgi' module Angelo class FormEncodingError < StandardError; end module ParamsParser EMPTY_JSON = '{}' 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 def parse_query_string parse_formencoded(request.query_string || EMPTY_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 recurse_symhash qs.merge! body else qs end end 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 def form_encoded? content_type? FORM_TYPE end def json? content_type? JSON_TYPE end 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 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
angelo-0.3.3 | lib/angelo/params_parser.rb |