class Addressable::URI

def query_values(return_type=Hash)

Returns:
  • (Hash, Array, nil) - The query string parsed as a Hash or Array

Parameters:
  • return_type (Class) -- The return type desired. Value must be either
def query_values(return_type=Hash)
  empty_accumulator = Array == return_type ? [] : {}
  if return_type != Hash && return_type != Array
    raise ArgumentError, "Invalid return type. Must be Hash or Array."
  end
  return nil if self.query == nil
  split_query = self.query.split("&").map do |pair|
    pair.split("=", 2) if pair && !pair.empty?
  end.compact
  return split_query.inject(empty_accumulator.dup) do |accu, pair|
    # I'd rather use key/value identifiers instead of array lookups,
    # but in this case I really want to maintain the exact pair structure,
    # so it's best to make all changes in-place.
    pair[0] = URI.unencode_component(pair[0])
    if pair[1].respond_to?(:to_str)
      # I loathe the fact that I have to do this. Stupid HTML 4.01.
      # Treating '+' as a space was just an unbelievably bad idea.
      # There was nothing wrong with '%20'!
      # If it ain't broke, don't fix it!
      pair[1] = URI.unencode_component(pair[1].to_str.tr("+", " "))
    end
    if return_type == Hash
      accu[pair[0]] = pair[1]
    else
      accu << pair
    end
    accu
  end
end