Class Index [+]

Quicksearch

Rack::Utils

Rack::Utils contains a grab-bag of useful methods for writing web applications adopted from all kinds of Ruby libraries.

Constants

HTTP_STATUS_CODES

Every standard HTTP code mapped to the appropriate message. Stolen from Mongrel.

STATUS_WITH_NO_ENTITY_BODY

Responses with HTTP status codes that should not have an entity body

Public Class Methods

build_query(params) click to toggle source

(Not documented)

     # File lib/rack/utils.rb, line 97
 97:     def build_query(params)
 98:       params.map { |k, v|
 99:         if v.class == Array
100:           build_query(v.map { |x| [k, x] })
101:         else
102:           escape(k) + "=" + escape(v)
103:         end
104:       }.join("&")
105:     end
bytesize(string) click to toggle source

(Not documented)

     # File lib/rack/utils.rb, line 155
155:       def bytesize(string)
156:         string.size
157:       end
bytesize(string) click to toggle source

(Not documented)

     # File lib/rack/utils.rb, line 151
151:       def bytesize(string)
152:         string.bytesize
153:       end
escape(s) click to toggle source

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 12
12:     def escape(s)
13:       s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
14:         '%'+$1.unpack('H2'*$1.size).join('%').upcase
15:       }.tr(' ', '+')
16:     end
escape_html(string) click to toggle source

Escape ampersands, brackets and quotes to their HTML/XML entities.

     # File lib/rack/utils.rb, line 109
109:     def escape_html(string)
110:       string.to_s.gsub("&", "&").
111:         gsub("<", "&lt;").
112:         gsub(">", "&gt;").
113:         gsub("'", "&#39;").
114:         gsub('"', "&quot;")
115:     end
normalize_params(params, name, v = nil) click to toggle source

(Not documented)

    # File lib/rack/utils.rb, line 65
65:     def normalize_params(params, name, v = nil)
66:       name =~ %r([\[\]]*([^\[\]]+)\]*)
67:       k = $1 || ''
68:       after = $' || ''
69: 
70:       return if k.empty?
71: 
72:       if after == ""
73:         params[k] = v
74:       elsif after == "[]"
75:         params[k] ||= []
76:         raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
77:         params[k] << v
78:       elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
79:         child_key = $1
80:         params[k] ||= []
81:         raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
82:         if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
83:           normalize_params(params[k].last, child_key, v)
84:         else
85:           params[k] << normalize_params({}, child_key, v)
86:         end
87:       else
88:         params[k] ||= {}
89:         raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash)
90:         params[k] = normalize_params(params[k], after, v)
91:       end
92: 
93:       return params
94:     end
parse_nested_query(qs, d = '&;') click to toggle source

(Not documented)

    # File lib/rack/utils.rb, line 53
53:     def parse_nested_query(qs, d = '&;')
54:       params = {}
55: 
56:       (qs || '').split(/[#{d}] */n).each do |p|
57:         k, v = unescape(p).split('=', 2)
58:         normalize_params(params, k, v)
59:       end
60: 
61:       return params
62:     end
parse_query(qs, d = '&;') click to toggle source

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 32
32:     def parse_query(qs, d = '&;')
33:       params = {}
34: 
35:       (qs || '').split(/[#{d}] */n).each do |p|
36:         k, v = unescape(p).split('=', 2)
37: 
38:         if cur = params[k]
39:           if cur.class == Array
40:             params[k] << v
41:           else
42:             params[k] = [cur, v]
43:           end
44:         else
45:           params[k] = v
46:         end
47:       end
48: 
49:       return params
50:     end
select_best_encoding(available_encodings, accept_encoding) click to toggle source

(Not documented)

     # File lib/rack/utils.rb, line 118
118:     def select_best_encoding(available_encodings, accept_encoding)
119:       # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
120: 
121:       expanded_accept_encoding =
122:         accept_encoding.map { |m, q|
123:           if m == "*"
124:             (available_encodings - accept_encoding.map { |m2, _| m2 }).map { |m2| [m2, q] }
125:           else
126:             [[m, q]]
127:           end
128:         }.inject([]) { |mem, list|
129:           mem + list
130:         }
131: 
132:       encoding_candidates = expanded_accept_encoding.sort_by { |_, q| -q }.map { |m, _| m }
133: 
134:       unless encoding_candidates.include?("identity")
135:         encoding_candidates.push("identity")
136:       end
137: 
138:       expanded_accept_encoding.find_all { |m, q|
139:         q == 0.0
140:       }.each { |m, _|
141:         encoding_candidates.delete(m)
142:       }
143: 
144:       return (encoding_candidates & available_encodings)[0]
145:     end
unescape(s) click to toggle source

Unescapes a URI escaped string. (Stolen from Camping).

    # File lib/rack/utils.rb, line 20
20:     def unescape(s)
21:       s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
22:         [$1.delete('%')].pack('H*')
23:       }
24:     end

Private Instance Methods

build_query(params) click to toggle source

(Not documented)

     # File lib/rack/utils.rb, line 97
 97:     def build_query(params)
 98:       params.map { |k, v|
 99:         if v.class == Array
100:           build_query(v.map { |x| [k, x] })
101:         else
102:           escape(k) + "=" + escape(v)
103:         end
104:       }.join("&")
105:     end
bytesize(string) click to toggle source

(Not documented)

     # File lib/rack/utils.rb, line 151
151:       def bytesize(string)
152:         string.bytesize
153:       end
bytesize(string) click to toggle source

(Not documented)

     # File lib/rack/utils.rb, line 155
155:       def bytesize(string)
156:         string.size
157:       end
escape(s) click to toggle source

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 12
12:     def escape(s)
13:       s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
14:         '%'+$1.unpack('H2'*$1.size).join('%').upcase
15:       }.tr(' ', '+')
16:     end
escape_html(string) click to toggle source

Escape ampersands, brackets and quotes to their HTML/XML entities.

     # File lib/rack/utils.rb, line 109
109:     def escape_html(string)
110:       string.to_s.gsub("&", "&amp;").
111:         gsub("<", "&lt;").
112:         gsub(">", "&gt;").
113:         gsub("'", "&#39;").
114:         gsub('"', "&quot;")
115:     end
normalize_params(params, name, v = nil) click to toggle source

(Not documented)

    # File lib/rack/utils.rb, line 65
65:     def normalize_params(params, name, v = nil)
66:       name =~ %r([\[\]]*([^\[\]]+)\]*)
67:       k = $1 || ''
68:       after = $' || ''
69: 
70:       return if k.empty?
71: 
72:       if after == ""
73:         params[k] = v
74:       elsif after == "[]"
75:         params[k] ||= []
76:         raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
77:         params[k] << v
78:       elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
79:         child_key = $1
80:         params[k] ||= []
81:         raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
82:         if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
83:           normalize_params(params[k].last, child_key, v)
84:         else
85:           params[k] << normalize_params({}, child_key, v)
86:         end
87:       else
88:         params[k] ||= {}
89:         raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash)
90:         params[k] = normalize_params(params[k], after, v)
91:       end
92: 
93:       return params
94:     end
parse_nested_query(qs, d = '&;') click to toggle source

(Not documented)

    # File lib/rack/utils.rb, line 53
53:     def parse_nested_query(qs, d = '&;')
54:       params = {}
55: 
56:       (qs || '').split(/[#{d}] */n).each do |p|
57:         k, v = unescape(p).split('=', 2)
58:         normalize_params(params, k, v)
59:       end
60: 
61:       return params
62:     end
parse_query(qs, d = '&;') click to toggle source

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 32
32:     def parse_query(qs, d = '&;')
33:       params = {}
34: 
35:       (qs || '').split(/[#{d}] */n).each do |p|
36:         k, v = unescape(p).split('=', 2)
37: 
38:         if cur = params[k]
39:           if cur.class == Array
40:             params[k] << v
41:           else
42:             params[k] = [cur, v]
43:           end
44:         else
45:           params[k] = v
46:         end
47:       end
48: 
49:       return params
50:     end
select_best_encoding(available_encodings, accept_encoding) click to toggle source

(Not documented)

     # File lib/rack/utils.rb, line 118
118:     def select_best_encoding(available_encodings, accept_encoding)
119:       # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
120: 
121:       expanded_accept_encoding =
122:         accept_encoding.map { |m, q|
123:           if m == "*"
124:             (available_encodings - accept_encoding.map { |m2, _| m2 }).map { |m2| [m2, q] }
125:           else
126:             [[m, q]]
127:           end
128:         }.inject([]) { |mem, list|
129:           mem + list
130:         }
131: 
132:       encoding_candidates = expanded_accept_encoding.sort_by { |_, q| -q }.map { |m, _| m }
133: 
134:       unless encoding_candidates.include?("identity")
135:         encoding_candidates.push("identity")
136:       end
137: 
138:       expanded_accept_encoding.find_all { |m, q|
139:         q == 0.0
140:       }.each { |m, _|
141:         encoding_candidates.delete(m)
142:       }
143: 
144:       return (encoding_candidates & available_encodings)[0]
145:     end
unescape(s) click to toggle source

Unescapes a URI escaped string. (Stolen from Camping).

    # File lib/rack/utils.rb, line 20
20:     def unescape(s)
21:       s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
22:         [$1.delete('%')].pack('H*')
23:       }
24:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.