Method: Addressable::URI#query_values=
- Defined in:
- lib/vendor/addressable/lib/addressable/uri.rb
#query_values=(new_query_values) ⇒ Object
Sets the query component for this URI from a Hash object. This method produces a query string using the :subscript notation.
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 |
# File 'lib/vendor/addressable/lib/addressable/uri.rb', line 1314 def query_values=(new_query_values) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if !new_query_values.respond_to?(:to_hash) raise TypeError, "Can't convert #{new_query_values.class} into Hash." end new_query_values = new_query_values.to_hash # Algorithm shamelessly stolen from Julien Genestoux, slightly modified buffer = "" stack = [] e = lambda do |component| component = component.to_s if component.kind_of?(Symbol) self.class.encode_component(component, CharacterClasses::UNRESERVED) end new_query_values.each do |key, value| if value.kind_of?(Hash) stack << [key, value] elsif value.kind_of?(Array) stack << [ key, value.inject({}) { |accu, x| accu[accu.size.to_s] = x; accu } ] elsif value == true buffer << "#{e.call(key)}&" else buffer << "#{e.call(key)}=#{e.call(value)}&" end end stack.each do |(parent, hash)| (hash.sort_by { |key| key.to_s }).each do |(key, value)| if value.kind_of?(Hash) stack << ["#{parent}[#{key}]", value] elsif value == true buffer << "#{parent}[#{e.call(key)}]&" else buffer << "#{parent}[#{e.call(key)}]=#{e.call(value)}&" end end end @query = buffer.chop # Reset dependant values @normalized_query = nil @uri_string = nil end |