Rack::Utils contains a grab-bag of useful methods for writing web applications adopted from all kinds of Ruby libraries.
Performs URI escaping so that you can construct proper query strings faster. Use this rather than the cgi.rb version since it’s faster. (Stolen from Camping).
# File lib/rack/utils.rb, line 14
14: def escape(s)
15: s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
16: '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
17: }.tr(' ', '+')
18: end
Stolen from Mongrel, with some small modifications: Parses a query string by breaking it up at the ’&’ and ’;’ characters. You can also use this to parse cookies by changing the characters used in the second parameter (which defaults to ’&;’).
# File lib/rack/utils.rb, line 36
36: def parse_query(qs, d = nil)
37: params = {}
38:
39: (qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
40: k, v = p.split('=', 2).map { |x| unescape(x) }
41: if v =~ /^("|')(.*)\1$/
42: v = $2.gsub('\\'+$1, $1)
43: end
44: if cur = params[k]
45: if cur.class == Array
46: params[k] << v
47: else
48: params[k] = [cur, v]
49: end
50: else
51: params[k] = v
52: end
53: end
Performs URI escaping so that you can construct proper query strings faster. Use this rather than the cgi.rb version since it’s faster. (Stolen from Camping).
# File lib/rack/utils.rb, line 14
14: def escape(s)
15: s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
16: '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
17: }.tr(' ', '+')
18: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.