Class: Hyraft::Router::ApiRouter
- Inherits:
-
Object
- Object
- Hyraft::Router::ApiRouter
- Defined in:
- lib/hyraft/router/api_router.rb
Defined Under Namespace
Classes: Route
Class Method Summary collapse
-
.draw(&block) ⇒ Object
DSL entry point.
Instance Method Summary collapse
-
#initialize ⇒ ApiRouter
constructor
A new instance of ApiRouter.
-
#resolve(http_method, path_info) ⇒ Object
Resolve a path to a route and dynamic params Returns [route, route_params] or nil.
Constructor Details
#initialize ⇒ ApiRouter
Returns a new instance of ApiRouter.
7 8 9 |
# File 'lib/hyraft/router/api_router.rb', line 7 def initialize @routes = [] end |
Class Method Details
.draw(&block) ⇒ Object
DSL entry point
12 13 14 15 16 |
# File 'lib/hyraft/router/api_router.rb', line 12 def self.draw(&block) router = new router.instance_eval(&block) router end |
Instance Method Details
#resolve(http_method, path_info) ⇒ Object
Resolve a path to a route and dynamic params Returns [route, route_params] or nil
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/hyraft/router/api_router.rb', line 27 def resolve(http_method, path_info) # Normalize path: remove trailing slash unless root "/" path = path_info.split('?').first path = path.chomp('/') unless path == "/" # 1️⃣ Exact match first (collection routes) exact = @routes.find do |r| r.http_method == http_method && normalize_route_path(r.path) == path end return [exact, []] if exact # 2️⃣ Regex match for dynamic routes (like :id) @routes.each do |r| next unless r.http_method == http_method if (m = r.regex.match(path)) return [r, m.captures] # captures = dynamic segments end end # 3️⃣ No match nil end |