Method: Addressable::URI#query_values

Defined in:
lib/vendor/addressable/lib/addressable/uri.rb

#query_values(options = {}) ⇒ Hash

Converts the query component to a Hash value.

The :dot notation is not supported for assignment. Default value is :subscript.

Examples:

Addressable::URI.parse("?one=1&two=2&three=3").query_values
#=> {"one" => "1", "two" => "2", "three" => "3"}
Addressable::URI.parse("?one[two][three]=four").query_values
#=> {"one" => {"two" => {"three" => "four"}}}
Addressable::URI.parse("?one.two.three=four").query_values(
  :notation => :dot
)
#=> {"one" => {"two" => {"three" => "four"}}}
Addressable::URI.parse("?one[two][three]=four").query_values(
  :notation => :flat
)
#=> {"one[two][three]" => "four"}
Addressable::URI.parse("?one.two.three=four").query_values(
  :notation => :flat
)
#=> {"one.two.three" => "four"}
Addressable::URI.parse(
  "?one[two][three][]=four&one[two][three][]=five"
).query_values
#=> {"one" => {"two" => {"three" => ["four", "five"]}}}

Parameters:

  • [Symbol] (Hash)

    a customizable set of options

Returns:

  • (Hash)

    The query string parsed as a Hash object.



1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
# File 'lib/vendor/addressable/lib/addressable/uri.rb', line 1246

def query_values(options={})
  defaults = {:notation => :subscript}
  options = defaults.merge(options)
  if ![:flat, :dot, :subscript].include?(options[:notation])
    raise ArgumentError,
      "Invalid notation. Must be one of: [:flat, :dot, :subscript]."
  end
  dehash = lambda do |hash|
    hash.each do |(key, value)|
      if value.kind_of?(Hash)
        hash[key] = dehash.call(value)
      end
    end
    if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ }
      hash.sort.inject([]) do |accu, (key, value)|
        accu << value; accu
      end
    else
      hash
    end
  end
  return nil if self.query == nil
  return ((self.query.split("&").map do |pair|
    pair.split("=")
  end).inject({}) do |accumulator, (key, value)|
    value = true if value.nil?
    key = self.class.unencode_component(key)
    if value != true
      value = self.class.unencode_component(value).gsub(/\+/, " ")
    end
    if options[:notation] == :flat
      if accumulator[key]
        raise ArgumentError, "Key was repeated: #{key.inspect}"
      end
      accumulator[key] = value
    else
      if options[:notation] == :dot
        array_value = false
        subkeys = key.split(".")
      elsif options[:notation] == :subscript
        array_value = !!(key =~ /\[\]$/)
        subkeys = key.split(/[\[\]]+/)
      end
      current_hash = accumulator
      for i in 0...(subkeys.size - 1)
        subkey = subkeys[i]
        current_hash[subkey] = {} unless current_hash[subkey]
        current_hash = current_hash[subkey]
      end
      if array_value
        current_hash[subkeys.last] = [] unless current_hash[subkeys.last]
        current_hash[subkeys.last] << value
      else
        current_hash[subkeys.last] = value
      end
    end
    accumulator
  end).inject({}) do |accumulator, (key, value)|
    accumulator[key] = value.kind_of?(Hash) ? dehash.call(value) : value
    accumulator
  end
end