Class: Hyraft::Router::WebRouter
- Inherits:
-
Object
- Object
- Hyraft::Router::WebRouter
- Defined in:
- lib/hyraft/router/web_router.rb
Defined Under Namespace
Classes: Route
Class Method Summary collapse
Instance Method Summary collapse
- #add_route(method, path, to, template = nil) ⇒ Object
- #DELETE(path, to:, template: nil) ⇒ Object
- #GET(path, to:, template: nil) ⇒ Object
-
#initialize ⇒ WebRouter
constructor
A new instance of WebRouter.
- #POST(path, to:, template: nil) ⇒ Object
- #PUT(path, to:, template: nil) ⇒ Object
- #resolve(method, path) ⇒ Object
Constructor Details
#initialize ⇒ WebRouter
Returns a new instance of WebRouter.
16 17 18 |
# File 'lib/hyraft/router/web_router.rb', line 16 def initialize @routes = [] end |
Class Method Details
.draw(&block) ⇒ Object
10 11 12 13 14 |
# File 'lib/hyraft/router/web_router.rb', line 10 def self.draw(&block) router = new router.instance_eval(&block) router end |
Instance Method Details
#add_route(method, path, to, template = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hyraft/router/web_router.rb', line 36 def add_route(method, path, to, template = nil) handler_class, action = to @routes << Route.new( path, method, handler_class, action, template ) end |
#DELETE(path, to:, template: nil) ⇒ Object
32 33 34 |
# File 'lib/hyraft/router/web_router.rb', line 32 def DELETE(path, to:, template: nil) add_route("DELETE", path, to, template) end |
#GET(path, to:, template: nil) ⇒ Object
20 21 22 |
# File 'lib/hyraft/router/web_router.rb', line 20 def GET(path, to:, template: nil) add_route("GET", path, to, template) end |
#POST(path, to:, template: nil) ⇒ Object
24 25 26 |
# File 'lib/hyraft/router/web_router.rb', line 24 def POST(path, to:, template: nil) add_route("POST", path, to, template) end |
#PUT(path, to:, template: nil) ⇒ Object
28 29 30 |
# File 'lib/hyraft/router/web_router.rb', line 28 def PUT(path, to:, template: nil) add_route("PUT", path, to, template) end |
#resolve(method, path) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hyraft/router/web_router.rb', line 48 def resolve(method, path) normalized_path = normalize_path(path) exact = @routes.find do |route| route.method == method && route.path == normalized_path && !wildcard_route?(route.path) && !parameter_route?(route.path) end return [exact, []] if exact parameter = @routes.find do |route| route.method == method && parameter_route?(route.path) && match_path(route.path, normalized_path) end if parameter return [ parameter, extract_params(parameter.path, normalized_path) ] end wildcard = @routes.find do |route| route.method == method && wildcard_route?(route.path) && match_path(route.path, normalized_path) end if wildcard return [ wildcard, extract_params(wildcard.path, normalized_path) ] end nil end |