Class: ApiSampler::RouteResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/api_sampler/route_resolver.rb

Overview

Finds a matching route for the request, if any.

Instance Method Summary collapse

Constructor Details

#initialize(request, router: nil) ⇒ RouteResolver

Returns a new instance of RouteResolver

Parameters:

  • request (ActionDispatch::Request, Rack::Request)

    the current request.

    If request has other type than ActionDispatch::Request, you must additionaly specify a router.

  • router (ActionDispatch::Journey::Router)

    an optional router.

    You can omit this parameter if request has type ActionDispatch::Request.


18
19
20
21
# File 'lib/api_sampler/route_resolver.rb', line 18

def initialize(request, router: nil)
  @request = request
  @router = router || request.routes.router
end

Instance Method Details

#resolve {|route| ... } ⇒ MatchedRoute?

Find a route matching the request, if any.

If a block is given and the matching route is found, yields the matched route.

Yields:

  • (route)

    the matched route, if any.

Yield Parameters:

Returns:

  • (MatchedRoute, nil)

    the route matching request, if any.


34
35
36
37
38
# File 'lib/api_sampler/route_resolver.rb', line 34

def resolve
  route = find_route
  yield route if route.present? && block_given?
  route
end