lib/http/protocol/url.rb in http-protocol-0.13.0 vs lib/http/protocol/url.rb in http-protocol-0.14.0

- old
+ new

@@ -76,17 +76,15 @@ def self.split(name) name.scan(/([^\[]+)|(?:\[(.*?)\])/).flatten!.compact! end - def self.assign(name, value, parent) - top, *middle = self.split(name) + def self.assign(keys, value, parent) + top, *middle = keys - yield(top, middle) if block_given? - middle.each_with_index do |key, index| - if key.empty? + if key.nil? or key.empty? parent = (parent[top] ||= Array.new) top = parent.size if nested = middle[index+1] and last = parent.last top -= 1 unless last.include?(nested) @@ -98,18 +96,24 @@ end parent[top] = value end - def self.decode(string, maximum = 8) + def self.decode(string, maximum = 8, symbolize_keys: false) parameters = {} - self.scan(string) do |key, value| - self.assign(key, value, parameters) do |top, middle| - if maximum and middle.count > maximum - raise ArgumentError, "Key length exceeded limit!" - end + self.scan(string) do |name, value| + keys = self.split(name) + + if keys.count > maximum + raise ArgumentError, "Key length exceeded limit!" end + + if symbolize_keys + keys.collect!{|key| key.empty? ? nil : key.to_sym} + end + + self.assign(keys, value, parameters) end return parameters end end