0
0
zero/lib/zero/router.rb
Gibheer 79537632ac added new router to the tools
This small router is intended to work like URLMap, but with the feature
that it can extract variables from routes.
2012-10-13 11:48:11 +02:00

32 lines
816 B
Ruby

module Zero
class Router
# match for variables in routes
VARIABLE_MATCH = %r{:(\w+)[^/]?}
# the replacement string to make it an regex
VARIABLE_REGEX = '(?<\1>.+?)'
def initialize(routes)
@routes = {}
routes.each do |route, target|
@routes[
Regexp.new(
route.gsub(VARIABLE_MATCH, VARIABLE_REGEX) + '$')] = target
end
end
def call(env)
request = Rack::Request.new(env)
@routes.each do |route, target|
match = route.match(request.path)
if match
match.names.each_index do |i|
request.update_param(match.names[i], match.captures[i])
end
return target.call(request.env)
end
end
[404, {'Content-Type' => 'text/html'}, ['Not found!']]
end
end
end