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.
13 14 15 |
# File 'lib/hyraft/router/web_router.rb', line 13 def initialize @routes = [] end |
Class Method Details
.draw(&block) ⇒ Object
7 8 9 10 11 |
# File 'lib/hyraft/router/web_router.rb', line 7 def self.draw(&block) router = new router.instance_eval(&block) router end |
Instance Method Details
#add_route(method, path, to, template = nil) ⇒ Object
33 34 35 36 |
# File 'lib/hyraft/router/web_router.rb', line 33 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
29 30 31 |
# File 'lib/hyraft/router/web_router.rb', line 29 def DELETE(path, to:, template: nil) add_route('DELETE', path, to, template) end |
#GET(path, to:, template: nil) ⇒ Object
17 18 19 |
# File 'lib/hyraft/router/web_router.rb', line 17 def GET(path, to:, template: nil) add_route('GET', path, to, template) end |
#POST(path, to:, template: nil) ⇒ Object
21 22 23 |
# File 'lib/hyraft/router/web_router.rb', line 21 def POST(path, to:, template: nil) add_route('POST', path, to, template) end |
#PUT(path, to:, template: nil) ⇒ Object
25 26 27 |
# File 'lib/hyraft/router/web_router.rb', line 25 def PUT(path, to:, template: nil) add_route('PUT', path, to, template) end |
#resolve(method, path) ⇒ Object
38 39 40 41 42 43 44 45 46 47 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 |
# File 'lib/hyraft/router/web_router.rb', line 38 def resolve(method, path) # Normalize path - remove trailing slashes except for root normalized_path = path.gsub(%r{/+$}, '') normalized_path = '/' if normalized_path.empty? # puts "DEBUG: Resolving #{method} #{path} -> normalized: #{normalized_path}" # First, try to find exact matches (non-parameter routes) exact_match = @routes.find do |r| r.method == method && !r.path.include?(':') && !r.path.include?('*') && r.path == normalized_path end if exact_match # puts "DEBUG: Exact match found: #{exact_match.path}" return [exact_match, []] end # Then try parameter routes (routes with :) param_match = @routes.find do |r| r.method == method && r.path.include?(':') && !r.path.include?('*') && match_path(r.path, normalized_path) end if param_match # puts "DEBUG: Parameter match found: #{param_match.path}" params = extract_params(param_match.path, normalized_path) return [param_match, params] end # Finally try wildcard routes wildcard_match = @routes.find do |r| r.method == method && r.path.include?('*') && match_path(r.path, normalized_path) end if wildcard_match # puts "DEBUG: Wildcard match found: #{wildcard_match.path}" params = extract_params(wildcard_match.path, normalized_path) return [wildcard_match, params] end # puts "DEBUG: No route found for #{method} #{normalized_path}" nil end |