Class: RouterSimple::Router
- Inherits:
-
Object
- Object
- RouterSimple::Router
- Defined in:
- lib/router_simple.rb
Overview
Yet another HTTP Router
Instance Method Summary collapse
-
#initialize ⇒ Router
constructor
Create new instance.
-
#match(http_method, path) ⇒ Object
match the path with this router.
-
#register(http_method, path, dest) ⇒ Object
register a path to router.
Constructor Details
#initialize ⇒ Router
Create new instance
30 31 32 |
# File 'lib/router_simple.rb', line 30 def initialize @patterns = [] end |
Instance Method Details
#match(http_method, path) ⇒ Object
match the path with this router.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/router_simple.rb', line 58 def match(http_method, path) found_method_not_allowed = false @patterns.each do |pattern| dest, captured, method_not_allowed = pattern.match(http_method, path) if dest return dest, captured elsif method_not_allowed found_method_not_allowed = true end end return nil, nil, found_method_not_allowed end |
#register(http_method, path, dest) ⇒ Object
register a path to router.
You can specify three pattern of path.
-
Use regexp directly
-
Use /:name/:id
-
Use /*path
46 47 48 |
# File 'lib/router_simple.rb', line 46 def register(http_method, path, dest) @patterns.push(Route.new(http_method, path, dest)) end |