Module: Renee::Core::Routing

Includes:
Chaining
Included in:
Renee::Core
Defined in:
lib/renee_core/routing.rb

Overview

Collection of useful methods for routing within a Renee::Core app.

Instance Method Summary collapse

Instance Method Details

#complete(&blk) ⇒ Object

Match only when the path is either '' or '/'.

Examples:

complete { halt [200, {}, "hello world"] }


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.

Examples:

delete { halt [200, {}, "hello world"] }


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 ''.

Examples:

empty { halt [200, {}, "hello world"] }


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.

Examples:

extension('html') { |path| halt [200, {}, path] }


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.

Examples:

get { halt [200, {}, "hello world"] }


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.

Examples:

no_extension { |path| halt [200, {}, path] }


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.

See Also:



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.

Examples:

path('/')    { ... } #=> '/'
path('test') { ... } #=> '/test'

path 'foo' do
  path('bar') { ... } #=> '/foo/bar'
end


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.

Examples:

post { halt [200, {}, "hello world"] }


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.

Examples:

put { halt [200, {}, "hello world"] }


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.

Examples:

query(:key => :integer) { |h| halt [200, {}, "hello world #{h[:key]}"] }
query(:key) { |val| halt [200, {}, "key is #{val}"] }


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.

Examples:

path 'test' do
  query_string 'foo=bar' do
    halt [200, {}, 'matched']
  end
end
GET /test?foo=bar #=> 'matched'


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.

Examples:

remainder { |path| halt [200, {}, 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.

Examples:

path '/' do
  variable { |id| halt [200, {}, id] }
end
GET /hey  #=> [200, {}, 'hey']
path '/' do
  variable(:integer) { |id| halt [200, {}, "This is a numeric id: #{id}"] }
end
GET /123  #=> [200, {}, 'This is a numeric id: 123']
path '/test' do
  variable { |foo, bar| halt [200, {}, "#{foo}-#{bar}"] }
end
GET /test/hey/there  #=> [200, {}, 'hey-there']


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.

See Also:



32
33
34
# File 'lib/renee_core/routing.rb', line 32

def whole_path(p, &blk)
  path(p) { complete(&blk) }
end