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