|
3 | 3 |
|
4 | 4 | module Pact
|
5 | 5 | class Query
|
| 6 | + DEFAULT_SEP = /[&;] */n |
| 7 | + COMMON_SEP = { ";" => /[;] */n, ";," => /[;,] */n, "&" => /[&] */n } |
| 8 | + |
6 | 9 | def self.create query
|
7 | 10 | if query.is_a? Hash
|
8 | 11 | Pact::QueryHash.new(query)
|
9 | 12 | else
|
10 | 13 | Pact::QueryString.new(query)
|
11 | 14 | end
|
12 | 15 | end
|
| 16 | + |
| 17 | + def self.parse_string query_string |
| 18 | + parsed_query = parse_query(query_string) |
| 19 | + |
| 20 | + # If Rails nested params... |
| 21 | + if parsed_query.keys.any?{ | key| key.include?("[") } |
| 22 | + parse_nested_query(query_string) |
| 23 | + else |
| 24 | + parsed_query.each_with_object({}) do | (key, value), new_hash | |
| 25 | + new_hash[key] = [*value] |
| 26 | + end |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + # Ripped from Rack to avoid adding an unnecessary dependency, thank you Rack |
| 31 | + # https://github.com/rack/rack/blob/649c72bab9e7b50d657b5b432d0c205c95c2be07/lib/rack/utils.rb |
| 32 | + def self.parse_query(qs, d = nil, &unescaper) |
| 33 | + unescaper ||= method(:unescape) |
| 34 | + |
| 35 | + params = {} |
| 36 | + |
| 37 | + (qs || '').split(d ? (COMMON_SEP[d] || /[#{d}] */n) : DEFAULT_SEP).each do |p| |
| 38 | + next if p.empty? |
| 39 | + k, v = p.split('=', 2).map!(&unescaper) |
| 40 | + |
| 41 | + if cur = params[k] |
| 42 | + if cur.class == Array |
| 43 | + params[k] << v |
| 44 | + else |
| 45 | + params[k] = [cur, v] |
| 46 | + end |
| 47 | + else |
| 48 | + params[k] = v |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + return params.to_h |
| 53 | + end |
| 54 | + |
| 55 | + def self.parse_nested_query(qs, d = nil) |
| 56 | + params = {} |
| 57 | + |
| 58 | + unless qs.nil? || qs.empty? |
| 59 | + (qs || '').split(d ? (COMMON_SEP[d] || /[#{d}] */n) : DEFAULT_SEP).each do |p| |
| 60 | + k, v = p.split('=', 2).map! { |s| unescape(s) } |
| 61 | + |
| 62 | + normalize_params(params, k, v) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + return params.to_h |
| 67 | + end |
| 68 | + |
| 69 | + def self.normalize_params(params, name, v) |
| 70 | + name =~ %r(\A[\[\]]*([^\[\]]+)\]*) |
| 71 | + k = $1 || '' |
| 72 | + after = $' || '' |
| 73 | + |
| 74 | + if k.empty? |
| 75 | + if !v.nil? && name == "[]" |
| 76 | + return Array(v) |
| 77 | + else |
| 78 | + return |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + if after == '' |
| 83 | + params[k] = v |
| 84 | + elsif after == "[" |
| 85 | + params[name] = v |
| 86 | + elsif after == "[]" |
| 87 | + params[k] ||= [] |
| 88 | + raise ParameterTypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array) |
| 89 | + params[k] << v |
| 90 | + elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$) |
| 91 | + child_key = $1 |
| 92 | + params[k] ||= [] |
| 93 | + raise ParameterTypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array) |
| 94 | + if params_hash_type?(params[k].last) && !params_hash_has_key?(params[k].last, child_key) |
| 95 | + normalize_params(params[k].last, child_key, v) |
| 96 | + else |
| 97 | + params[k] << normalize_params({}, child_key, v) |
| 98 | + end |
| 99 | + else |
| 100 | + params[k] ||= {} |
| 101 | + raise ParameterTypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params_hash_type?(params[k]) |
| 102 | + params[k] = normalize_params(params[k], after, v, depth - 1) |
| 103 | + end |
| 104 | + |
| 105 | + params |
| 106 | + end |
| 107 | + |
| 108 | + def self.unescape(s, encoding = Encoding::UTF_8) |
| 109 | + URI.decode_www_form_component(s, encoding) |
| 110 | + end |
13 | 111 | end
|
14 | 112 | end
|
0 commit comments