module URI

Public Instance Methods

cgi_escape(string) click to toggle source

TODO: How does this compare to URI.escape?

# File lib/more/facets/uri.rb, line 109
def cgi_escape(string)
  string.gsub(%r([^ a-zA-Z0-9_.-]+)/) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end.tr(' ', '+')
end
cgi_parse(query) click to toggle source
# File lib/more/facets/uri.rb, line 125
def cgi_parse(query)
  params = Hash.new([].freeze)

  query.split(%r[&;]/).each do |pairs|
    key, value = pairs.split('=',2).collect{|v| cgi_unescape(v) }
    if params.has_key?(key)
      params[key].push(value)
    else
      params[key] = [value]
    end
  end

  params
end
cgi_unescape(string) click to toggle source
# File lib/more/facets/uri.rb, line 117
def cgi_unescape(string)
  string.tr('+', ' ').gsub(%r((?:%[0-9a-fA-F]{2})+)/) do
    [$1.delete('%')].pack('H*')
  end
end
chomp_query_string(uri) click to toggle source

Removes the query string from a uri

Input: the uri

Output: the chomped uri.

# File lib/more/facets/uri.rb, line 169
def chomp_query_string(uri)
  return nil unless uri
  query_string = self.get_query_string(uri)
  return uri.dup.chomp("?#{query_string}")
end
decode(uri) click to toggle source

Decode the uri components.

# File lib/more/facets/uri.rb, line 33
  def decode(uri)
    # gmosx: hmm is this needed?
    # guard against invalid filenames for example pictures with
    # spaces uploaded by users
    escaped_uri = uri.gsub(%r /, "+")

    if md = URI::REGEXP::REL_URI.match(escaped_uri)

      path = "#{md[5]}#{md[6]}"
      type = File.extname(path)
      query_string = md[7]

#      real_path = "#{$root_dir}/#{path}"

      parameters = URI.query_to_hash(query_string)
      path.gsub!(%r\+/, " ")

      return [path, type, parameters, query_string]

    end # match

    # this is usefull for uncovering bugs!
    raise ArgumentError.new("the parameter '#{uri}' is not a valid uri")
  end
get_query_string(uri) click to toggle source

This method returns the query string of a uri

Input: the uri

Output: the query string. returns nil if no query string

# File lib/more/facets/uri.rb, line 149
def get_query_string(uri)
  return nil unless uri
  # gmosx: INVESTIGATE ruby's URI seems to differently handle
  # abs and rel uris.
  if md = URI::REGEXP::ABS_URI.match(uri)
    return md[8]
  elsif md = URI::REGEXP::REL_URI.match(uri)
    return md[7]
  end
  return nil
end
hash_to_query(parameters) click to toggle source

Given a hash with parameter/value pairs construct a standard query string.

URI.hash_to_query(:a => 1, :b => 2)
=> "a=1&b=2"
# File lib/more/facets/uri.rb, line 94
def hash_to_query(parameters)
  return '' unless parameters
  pairs = []
  parameters.each do |param, value|
    pairs << "#{param}=#{cgi_escape(value.to_s)}"
  end
  #return pairs.join('&')
  return pairs.join(";")
end
Also aliased as: hash_to_query_string
hash_to_query_string(parameters) click to toggle source
Alias for: hash_to_query
query_string_to_hash(query_string) click to toggle source
Alias for: query_to_hash
query_to_hash(query_string) click to toggle source

Extend the basic query string parser provided by the cgi module. converts single valued params (the most common case) to objects instead of arrays

Input: the query string

Output: hash of parameters, contains arrays for multivalued parameters (multiselect, checkboxes , etc) If no query string is provided (nil or "") returns an empty hash.

# File lib/more/facets/uri.rb, line 70
def query_to_hash(query_string)
  return {} unless query_string

  query_parameters = cgi_parse(query_string)

  query_parameters.each { |key, val|
    # replace the array with an object
    query_parameters[key] = val[0] if 1 == val.length
  }

  # set default value to nil! cgi sets this to []
  query_parameters.default = nil

  return query_parameters
end
Also aliased as: query_string_to_hash
update_query_string(uri, parameters) click to toggle source

Get a uri and a hash of parameters. Inject the hash values as parameters in the query sting path. Returns the full uri.

Input: the uri to filter (String) hash of parameters to update

Output: the full updated query string

TODO: optimize

# File lib/more/facets/uri.rb, line 188
def update_query_string(uri, parameters)
  query_string = self.get_query_string(uri)
  rest = uri.dup.gsub(%r\?#{query_string}/, "")

  hash = self.query_string_to_hash(query_string)
  hash.update(parameters)
  query_string = self.hash_to_query_string(hash)

  unless query_string.blank?
    return "#{rest}?#{query_string}"
  else
    return rest
  end
end
update_request_uri(request, parameters) click to toggle source

TODO: find a better name. Gets the request uri, injects extra parameters in the query string and returns a new uri. The request object is not modified. There is always a qs string so an extra test is skipped.

# File lib/more/facets/uri.rb, line 208
def update_request_uri(request, parameters)
  hash = request.parameters.dup()
  hash.update(parameters)

  # use this in hash_to_querystring.
  query_string = hash.collect { |k, v|
    "#{k}=#{v}"
  }.join(";")

  #return "#{request.translated_uri}?#{query_string}"
  return "#{request.path}?#{query_string}"
end