Module: Renee::Core::Routing
Overview
Collection of useful methods for routing within a Renee::Core app.
Instance Method Summary collapse
-
#complete(&blk) ⇒ Object
Match only when the path is either '' or '/'.
-
#delete(path = nil, &blk) ⇒ Object
Respond to a DELETE request and yield the block.
-
#empty(&blk) ⇒ Object
Match only when the path is ''.
-
#extension(ext, &blk) ⇒ Object
(also: #ext)
Match an extension.
-
#get(path = nil, &blk) ⇒ Object
Respond to a GET request and yield the block.
-
#multi_variable(count, type = nil, &blk) ⇒ Object
(also: #multi_var, #mvar)
Same as variable except you can match multiple variables with the same type.
-
#no_extension(&blk) ⇒ Object
Match no extension.
-
#part(p, &blk) ⇒ Object
Like #path, but doesn't look for leading slashes.
-
#partial_variable(type = nil, &blk) ⇒ Object
(also: #part_var)
Match parts off the path as variables without a leading slash.
-
#path(p, &blk) ⇒ Object
Match a path to respond to.
-
#post(path = nil, &blk) ⇒ Object
Respond to a POST request and yield the block.
-
#put(path = nil, &blk) ⇒ Object
Respond to a PUT request and yield the block.
-
#query(q, &blk) ⇒ Object
Match variables within the query string.
-
#query_string(qs, &blk) ⇒ Object
Yield block if the query string matches.
-
#remainder(&blk) ⇒ Object
(also: #catchall)
Match any remaining path.
-
#repeating_variable(type = nil, &blk) ⇒ Object
(also: #glob)
Same as variable except it matches indefinitely.
-
#variable(type = nil, &blk) ⇒ Object
(also: #var)
Match parts off the path as variables.
-
#whole_path(p, &blk) ⇒ Object
Like #path, but requires the entire path to be consumed.
Instance Method Details
#complete(&blk) ⇒ Object
Match only when the path is either '' or '/'.
193 194 195 196 197 |
# File 'lib/renee_core/routing.rb', line 193 def complete(&blk) if env['PATH_INFO'] == '/' or env['PATH_INFO'] == '' with_path_part(env['PATH_INFO']) { blk.call } end end |
#delete(path = nil, &blk) ⇒ Object
Respond to a DELETE request and yield the block.
182 183 184 |
# File 'lib/renee_core/routing.rb', line 182 def delete(path = nil, &blk) request_method('DELETE', path, &blk) end |
#empty(&blk) ⇒ Object
Match only when the path is ''.
206 207 208 209 210 |
# File 'lib/renee_core/routing.rb', line 206 def empty(&blk) if env['PATH_INFO'] == '' with_path_part(env['PATH_INFO']) { blk.call } end end |
#extension(ext, &blk) ⇒ Object Also known as: ext
Match an extension.
109 110 111 112 113 114 115 116 |
# File 'lib/renee_core/routing.rb', line 109 def extension(ext, &blk) if detected_extension && match = detected_extension[ext] if match == detected_extension (ext_match = env['PATH_INFO'][/\/?\.#{match}/]) ? with_path_part(ext_match, &blk) : blk.call end end end |
#get(path = nil, &blk) ⇒ Object
Respond to a GET request and yield the block.
149 150 151 |
# File 'lib/renee_core/routing.rb', line 149 def get(path = nil, &blk) request_method('GET', path, &blk) end |
#multi_variable(count, type = nil, &blk) ⇒ Object Also known as: multi_var, mvar
Same as variable except you can match multiple variables with the same type.
79 80 81 |
# File 'lib/renee_core/routing.rb', line 79 def multi_variable(count, type = nil, &blk) complex_variable(type, '/', count, &blk) end |
#no_extension(&blk) ⇒ Object
Match no extension.
126 127 128 |
# File 'lib/renee_core/routing.rb', line 126 def no_extension(&blk) blk.call if detected_extension.nil? end |
#part(p, &blk) ⇒ Object
Like #path, but doesn't look for leading slashes.
38 39 40 41 42 43 |
# File 'lib/renee_core/routing.rb', line 38 def part(p, &blk) p = /^\/?#{Regexp.quote(p)}/ if p.is_a?(String) if match = env['PATH_INFO'][p] with_path_part(match) { blk.call } end end |
#partial_variable(type = nil, &blk) ⇒ Object Also known as: part_var
Match parts off the path as variables without a leading slash.
97 98 99 |
# File 'lib/renee_core/routing.rb', line 97 def partial_variable(type = nil, &blk) complex_variable(type, nil, 1, &blk) end |
#path(p, &blk) ⇒ Object
Match a path to respond to.
23 24 25 26 27 |
# File 'lib/renee_core/routing.rb', line 23 def path(p, &blk) p = p[1, p.size] if p[0] == ?/ extension_part = detected_extension ? "|\\.#{Regexp.quote(detected_extension)}" : "" part(/^\/#{Regexp.quote(p)}(?=\/|$#{extension_part})/, &blk) end |
#post(path = nil, &blk) ⇒ Object
Respond to a POST request and yield the block.
160 161 162 |
# File 'lib/renee_core/routing.rb', line 160 def post(path = nil, &blk) request_method('POST', path, &blk) end |
#put(path = nil, &blk) ⇒ Object
Respond to a PUT request and yield the block.
171 172 173 |
# File 'lib/renee_core/routing.rb', line 171 def put(path = nil, &blk) request_method('PUT', path, &blk) end |
#query(q, &blk) ⇒ Object
Match variables within the query string.
227 228 229 230 231 232 233 |
# File 'lib/renee_core/routing.rb', line 227 def query(q, &blk) case q when Hash then blk.call(Hash[q.map{|(k, v)| [k, transform(v, request[k.to_s]) || return]}]) when Array then blk.call(*q.map{|qk| request[qk.to_s] or return }) else query([q], &blk) end end |
#query_string(qs, &blk) ⇒ Object
Yield block if the query string matches.
250 251 252 |
# File 'lib/renee_core/routing.rb', line 250 def query_string(qs, &blk) blk.call if qs === env['QUERY_STRING'] end |
#remainder(&blk) ⇒ Object Also known as: catchall
Match any remaining path.
137 138 139 |
# File 'lib/renee_core/routing.rb', line 137 def remainder(&blk) with_path_part(env['PATH_INFO']) { |var| blk.call(var) } end |
#repeating_variable(type = nil, &blk) ⇒ Object Also known as: glob
Same as variable except it matches indefinitely.
88 89 90 |
# File 'lib/renee_core/routing.rb', line 88 def repeating_variable(type = nil, &blk) complex_variable(type, '/', nil, &blk) end |
#variable(type = nil, &blk) ⇒ Object Also known as: var
Match parts off the path as variables. The parts matcher can conform to either a regular expression, or be an Integer, or simply a String. @param[Object] type the type of object to match for. If you supply Integer, this will only match integers in addition to casting your variable for you. @param[Object] default the default value to use if your param cannot be successfully matched.
70 71 72 |
# File 'lib/renee_core/routing.rb', line 70 def variable(type = nil, &blk) complex_variable(type, '/', 1, &blk) end |
#whole_path(p, &blk) ⇒ Object
Like #path, but requires the entire path to be consumed.
32 33 34 |
# File 'lib/renee_core/routing.rb', line 32 def whole_path(p, &blk) path(p) { complete(&blk) } end |