Classes and Modules
Public Instance methods
normalize_keys(key, value, cond = nil, &block)
[show source]
# File lib/httpx/transcoder.rb 7 def normalize_keys(key, value, cond = nil, &block) 8 if cond && cond.call(value) 9 block.call(key.to_s, value) 10 elsif value.respond_to?(:to_ary) 11 if value.empty? 12 block.call("#{key}[]") 13 else 14 value.to_ary.each do |element| 15 normalize_keys("#{key}[]", element, cond, &block) 16 end 17 end 18 elsif value.respond_to?(:to_hash) 19 value.to_hash.each do |child_key, child_value| 20 normalize_keys("#{key}[#{child_key}]", child_value, cond, &block) 21 end 22 else 23 block.call(key.to_s, value) 24 end 25 end
normalize_query(params, name, v, depth)
based on github.com/rack/rack/blob/d15dd728440710cfc35ed155d66a98dc2c07ae42/lib/rack/query_parser.rb#L82
[show source]
# File lib/httpx/transcoder.rb 28 def normalize_query(params, name, v, depth) 29 raise Error, "params depth surpasses what's supported" if depth <= 0 30 31 name =~ /\A[\[\]]*([^\[\]]+)\]*/ 32 k = Regexp.last_match(1) || "" 33 after = Regexp.last_match ? Regexp.last_match.post_match : "" 34 35 if k.empty? 36 return Array(v) if !v.empty? && name == "[]" 37 38 return 39 end 40 41 case after 42 when "" 43 params[k] = v 44 when "[" 45 params[name] = v 46 when "[]" 47 params[k] ||= [] 48 raise Error, "expected Array (got #{params[k].class}) for param '#{k}'" unless params[k].is_a?(Array) 49 50 params[k] << v 51 when /^\[\]\[([^\[\]]+)\]$/, /^\[\](.+)$/ 52 child_key = Regexp.last_match(1) 53 params[k] ||= [] 54 raise Error, "expected Array (got #{params[k].class}) for param '#{k}'" unless params[k].is_a?(Array) 55 56 if params[k].last.is_a?(Hash) && !params_hash_has_key?(params[k].last, child_key) 57 normalize_query(params[k].last, child_key, v, depth - 1) 58 else 59 params[k] << normalize_query({}, child_key, v, depth - 1) 60 end 61 else 62 params[k] ||= {} 63 raise Error, "expected Hash (got #{params[k].class}) for param '#{k}'" unless params[k].is_a?(Hash) 64 65 params[k] = normalize_query(params[k], after, v, depth - 1) 66 end 67 68 params 69 end
params_hash_has_key?(hash, key)
[show source]
# File lib/httpx/transcoder.rb 71 def params_hash_has_key?(hash, key) 72 return false if key.include?("[]") 73 74 key.split(/[\[\]]+/).inject(hash) do |h, part| 75 next h if part == "" 76 return false unless h.is_a?(Hash) && h.key?(part) 77 78 h[part] 79 end 80 81 true 82 end