Class: SideBro::Web::Router
- Inherits:
-
Object
- Object
- SideBro::Web::Router
- Defined in:
- lib/side_bro/web/router.rb
Constant Summary collapse
- ROUTE_PARAMS =
"rack.route_params"
Instance Method Summary collapse
- #add(method, path, &block) ⇒ Object
- #get(path, &block) ⇒ Object
- #head(path, &block) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #match(env) ⇒ Object
- #post(path, &block) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
8 9 10 |
# File 'lib/side_bro/web/router.rb', line 8 def initialize @routes = Hash.new { |h, k| h[k] = [] } end |
Instance Method Details
#add(method, path, &block) ⇒ Object
12 13 14 15 |
# File 'lib/side_bro/web/router.rb', line 12 def add(method, path, &block) pattern, keys = compile(path) @routes[method.upcase] << [pattern, keys, block] end |
#get(path, &block) ⇒ Object
17 |
# File 'lib/side_bro/web/router.rb', line 17 def get(path, &block) = add("GET", path, &block) |
#head(path, &block) ⇒ Object
19 |
# File 'lib/side_bro/web/router.rb', line 19 def head(path, &block) = add("HEAD", path, &block) |
#match(env) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/side_bro/web/router.rb', line 21 def match(env) method = env["REQUEST_METHOD"] path = env["PATH_INFO"] routes_for(method).each do |pattern, keys, block| next unless (m = pattern.match(path)) env[ROUTE_PARAMS] = keys.each_with_object({}) { |k, h| h[k] = m[k] } return block end nil end |
#post(path, &block) ⇒ Object
18 |
# File 'lib/side_bro/web/router.rb', line 18 def post(path, &block) = add("POST", path, &block) |