Class: Lotus::Router
- Inherits:
-
Object
- Object
- Lotus::Router
- Defined in:
- lib/lotus/router.rb,
lib/lotus/router/version.rb
Overview
Rack compatible, lightweight and fast HTTP Router.
Defined Under Namespace
Classes: NotRoutableEndpointError
Constant Summary collapse
- VERSION =
'0.5.1'.freeze
Class Method Summary collapse
-
.define(&blk) ⇒ Proc
Returns the given block as it is.
Instance Method Summary collapse
-
#call(env) ⇒ Rack::Response, Array
Resolve the given Rack env to a registered endpoint and invoke it.
-
#define(&blk) ⇒ Lotus::Routing::Route
To support defining routes in the ‘define` wrapper.
-
#defined? ⇒ TrueClass, FalseClass
private
Check if there are defined routes.
-
#delete(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a DELETE request for the given path.
-
#get(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a GET request for the given path.
-
#initialize(options = {}, &blk) ⇒ Lotus::Router
constructor
Initialize the router.
-
#inspector ⇒ Object
Returns an routes inspector.
-
#mount(app, options) ⇒ Object
Mount a Rack application at the specified path.
-
#namespace(namespace, &blk) ⇒ Lotus::Routing::Namespace
Defines a Ruby block: all the routes defined within it will be namespaced with the given relative path.
-
#options(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a OPTIONS request for the given path.
-
#patch(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a PATCH request for the given path.
-
#path(route, *args) ⇒ String
Generate an relative URL for a specified named route.
-
#post(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a POST request for the given path.
-
#put(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a PUT request for the given path.
-
#recognize(env, options = {}, params = nil) ⇒ Lotus::Routing::RecognizedRoute
Recognize the given env, path, or name and return a route for testing inspection.
-
#redirect(path, options = {}, &endpoint) ⇒ Lotus::Routing::Route
Defines an HTTP redirect.
-
#resource(name, options = {}, &blk) ⇒ Lotus::Routing::Resource
Defines a set of named routes for a single RESTful resource.
-
#resources(name, options = {}, &blk) ⇒ Lotus::Routing::Resources
Defines a set of named routes for a plural RESTful resource.
-
#routes ⇒ self
private
Returns self.
-
#trace(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a TRACE request for the given path.
-
#url(route, *args) ⇒ String
Generate a URL for a specified named route.
Constructor Details
#initialize(options = {}, &blk) ⇒ Lotus::Router
Initialize the router.
220 221 222 223 |
# File 'lib/lotus/router.rb', line 220 def initialize( = {}, &blk) @router = Routing::HttpRouter.new() define(&blk) end |
Class Method Details
.define(&blk) ⇒ Proc
Returns the given block as it is.
When Lotus::Router is used as a standalone gem and the routes are defined into a configuration file, some systems could raise an exception.
Imagine the following file into a Ruby on Rails application:
get '/', to: 'api#index'
Because Ruby on Rails in production mode use to eager load code and the routes file uses top level method calls, it crashes the application.
If we wrap these routes with Lotus::Router.define
, the block doesn’t get yielded but just returned to the caller as it is.
Usually the receiver of this block is Lotus::Router#initialize
, which finally evaluates the block.
123 124 125 |
# File 'lib/lotus/router.rb', line 123 def self.define(&blk) blk end |
Instance Method Details
#call(env) ⇒ Rack::Response, Array
Resolve the given Rack env to a registered endpoint and invoke it.
930 931 932 |
# File 'lib/lotus/router.rb', line 930 def call(env) @router.call(env) end |
#define(&blk) ⇒ Lotus::Routing::Route
To support defining routes in the ‘define` wrapper.
259 260 261 |
# File 'lib/lotus/router.rb', line 259 def define(&blk) instance_eval(&blk) if block_given? end |
#defined? ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if there are defined routes
277 278 279 |
# File 'lib/lotus/router.rb', line 277 def defined? @router.routes.any? end |
#delete(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a DELETE request for the given path.
471 472 473 |
# File 'lib/lotus/router.rb', line 471 def delete(path, = {}, &blk) @router.delete(path, , &blk) end |
#get(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a GET request for the given path.
395 396 397 |
# File 'lib/lotus/router.rb', line 395 def get(path, = {}, &blk) @router.get(path, , &blk) end |
#inspector ⇒ Object
Returns an routes inspector
1128 1129 1130 1131 |
# File 'lib/lotus/router.rb', line 1128 def inspector require 'lotus/routing/routes_inspector' Routing::RoutesInspector.new(@router.routes) end |
#mount(app, options) ⇒ Object
Mount a Rack application at the specified path. All the requests starting with the specified path, will be forwarded to the given application.
All the other methods (eg #get) support callable objects, but they restrict the range of the acceptable HTTP verb. Mounting an application with #mount doesn’t apply this kind of restriction at the router level, but let the application to decide.
919 920 921 |
# File 'lib/lotus/router.rb', line 919 def mount(app, ) @router.mount(app, ) end |
#namespace(namespace, &blk) ⇒ Lotus::Routing::Namespace
Defines a Ruby block: all the routes defined within it will be namespaced with the given relative path.
Namespaces blocks can be nested multiple times.
585 586 587 |
# File 'lib/lotus/router.rb', line 585 def namespace(namespace, &blk) Routing::Namespace.new(self, namespace, &blk) end |
#options(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a OPTIONS request for the given path.
509 510 511 |
# File 'lib/lotus/router.rb', line 509 def (path, = {}, &blk) @router.(path, , &blk) end |
#patch(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a PATCH request for the given path.
452 453 454 |
# File 'lib/lotus/router.rb', line 452 def patch(path, = {}, &blk) @router.patch(path, , &blk) end |
#path(route, *args) ⇒ String
Generate an relative URL for a specified named route. The additional arguments will be used to compose the relative URL - in
case it has tokens to match - and for compose the query string.
1076 1077 1078 |
# File 'lib/lotus/router.rb', line 1076 def path(route, *args) @router.path(route, *args) end |
#post(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a POST request for the given path.
414 415 416 |
# File 'lib/lotus/router.rb', line 414 def post(path, = {}, &blk) @router.post(path, , &blk) end |
#put(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a PUT request for the given path.
433 434 435 |
# File 'lib/lotus/router.rb', line 433 def put(path, = {}, &blk) @router.put(path, , &blk) end |
#recognize(env, options = {}, params = nil) ⇒ Lotus::Routing::RecognizedRoute
Recognize the given env, path, or name and return a route for testing inspection.
If the route cannot be recognized, it still returns an object for testing inspection.
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 |
# File 'lib/lotus/router.rb', line 1042 def recognize(env, = {}, params = nil) require 'lotus/routing/recognized_route' env = env_for(env, , params) responses, _ = *@router.recognize(env) Routing::RecognizedRoute.new( responses.nil? ? responses : responses.first, env, @router) end |
#redirect(path, options = {}, &endpoint) ⇒ Lotus::Routing::Route
Defines an HTTP redirect
539 540 541 |
# File 'lib/lotus/router.rb', line 539 def redirect(path, = {}, &endpoint) get(path).redirect @router.find(), [:code] || 301 end |
#resource(name, options = {}, &blk) ⇒ Lotus::Routing::Resource
Defines a set of named routes for a single RESTful resource. It has a built-in integration for Lotus::Controller.
707 708 709 |
# File 'lib/lotus/router.rb', line 707 def resource(name, = {}, &blk) Routing::Resource.new(self, name, .merge(separator: @router.action_separator), &blk) end |
#resources(name, options = {}, &blk) ⇒ Lotus::Routing::Resources
Defines a set of named routes for a plural RESTful resource. It has a built-in integration for Lotus::Controller.
830 831 832 |
# File 'lib/lotus/router.rb', line 830 def resources(name, = {}, &blk) Routing::Resources.new(self, name, .merge(separator: @router.action_separator), &blk) end |
#routes ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns self
This is a duck-typing trick for compatibility with ‘Lotus::Application`. It’s used by ‘Lotus::Routing::RoutesInspector` to inspect both apps and routers.
235 236 237 |
# File 'lib/lotus/router.rb', line 235 def routes self end |
#trace(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a TRACE request for the given path.
490 491 492 |
# File 'lib/lotus/router.rb', line 490 def trace(path, = {}, &blk) @router.trace(path, , &blk) end |
#url(route, *args) ⇒ String
Generate a URL for a specified named route. The additional arguments will be used to compose the relative URL - in
case it has tokens to match - and for compose the query string.
1103 1104 1105 |
# File 'lib/lotus/router.rb', line 1103 def url(route, *args) @router.url(route, *args) end |