| Module | URI |
| In: |
lib/more/facets/uri.rb
|
TODO: How does this compare to URI.escape?
# File lib/more/facets/uri.rb, line 109
109: def cgi_escape(string)
110: string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
111: '%' + $1.unpack('H2' * $1.size).join('%').upcase
112: end.tr(' ', '+')
113: end
# File lib/more/facets/uri.rb, line 125
125: def cgi_parse(query)
126: params = Hash.new([].freeze)
127:
128: query.split(/[&;]/n).each do |pairs|
129: key, value = pairs.split('=',2).collect{|v| cgi_unescape(v) }
130: if params.has_key?(key)
131: params[key].push(value)
132: else
133: params[key] = [value]
134: end
135: end
136:
137: params
138: end
# File lib/more/facets/uri.rb, line 117
117: def cgi_unescape(string)
118: string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
119: [$1.delete('%')].pack('H*')
120: end
121: end
Removes the query string from a uri
Input: the uri
Output: the chomped uri.
# File lib/more/facets/uri.rb, line 169
169: def chomp_query_string(uri)
170: return nil unless uri
171: query_string = self.get_query_string(uri)
172: return uri.dup.chomp("?#{query_string}")
173: end
Decode the uri components.
# File lib/more/facets/uri.rb, line 33
33: def decode(uri)
34: # gmosx: hmm is this needed?
35: # guard against invalid filenames for example pictures with
36: # spaces uploaded by users
37: escaped_uri = uri.gsub(/ /, "+")
38:
39: if md = URI::REGEXP::REL_URI.match(escaped_uri)
40:
41: path = "#{md[5]}#{md[6]}"
42: type = File.extname(path)
43: query_string = md[7]
44:
45: # real_path = "#{$root_dir}/#{path}"
46:
47: parameters = URI.query_to_hash(query_string)
48: path.gsub!(/\+/, " ")
49:
50: return [path, type, parameters, query_string]
51:
52: end # match
53:
54: # this is usefull for uncovering bugs!
55: raise ArgumentError.new("the parameter '#{uri}' is not a valid uri")
56: end
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
149: def get_query_string(uri)
150: return nil unless uri
151: # gmosx: INVESTIGATE ruby's URI seems to differently handle
152: # abs and rel uris.
153: if md = URI::REGEXP::ABS_URI.match(uri)
154: return md[8]
155: elsif md = URI::REGEXP::REL_URI.match(uri)
156: return md[7]
157: end
158: return nil
159: end
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
94: def hash_to_query(parameters)
95: return '' unless parameters
96: pairs = []
97: parameters.each do |param, value|
98: pairs << "#{param}=#{cgi_escape(value.to_s)}"
99: end
100: #return pairs.join('&')
101: return pairs.join(";")
102: end
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
70: def query_to_hash(query_string)
71: return {} unless query_string
72:
73: query_parameters = cgi_parse(query_string)
74:
75: query_parameters.each { |key, val|
76: # replace the array with an object
77: query_parameters[key] = val[0] if 1 == val.length
78: }
79:
80: # set default value to nil! cgi sets this to []
81: query_parameters.default = nil
82:
83: return query_parameters
84: end
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
188: def update_query_string(uri, parameters)
189: query_string = self.get_query_string(uri)
190: rest = uri.dup.gsub(/\?#{query_string}/, "")
191:
192: hash = self.query_string_to_hash(query_string)
193: hash.update(parameters)
194: query_string = self.hash_to_query_string(hash)
195:
196: unless query_string.blank?
197: return "#{rest}?#{query_string}"
198: else
199: return rest
200: end
201: end
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
208: def update_request_uri(request, parameters)
209: hash = request.parameters.dup()
210: hash.update(parameters)
211:
212: # use this in hash_to_querystring.
213: query_string = hash.collect { |k, v|
214: "#{k}=#{v}"
215: }.join(";")
216:
217: #return "#{request.translated_uri}?#{query_string}"
218: return "#{request.path}?#{query_string}"
219: end