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.
Constant Summary collapse
- VERSION =
'0.4.2'.freeze
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.
-
#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.
167 168 169 170 |
# File 'lib/lotus/router.rb', line 167 def initialize( = {}, &blk) @router = Routing::HttpRouter.new() define(&blk) end |
Instance Method Details
#call(env) ⇒ Rack::Response, Array
Resolve the given Rack env to a registered endpoint and invoke it.
877 878 879 |
# File 'lib/lotus/router.rb', line 877 def call(env) @router.call(env) end |
#define(&blk) ⇒ Lotus::Routing::Route
To support defining routes in the ‘define` wrapper.
206 207 208 |
# File 'lib/lotus/router.rb', line 206 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
224 225 226 |
# File 'lib/lotus/router.rb', line 224 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.
418 419 420 |
# File 'lib/lotus/router.rb', line 418 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.
342 343 344 |
# File 'lib/lotus/router.rb', line 342 def get(path, = {}, &blk) @router.get(path, , &blk) end |
#inspector ⇒ Object
Returns an routes inspector
956 957 958 959 |
# File 'lib/lotus/router.rb', line 956 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.
866 867 868 |
# File 'lib/lotus/router.rb', line 866 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.
532 533 534 |
# File 'lib/lotus/router.rb', line 532 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.
456 457 458 |
# File 'lib/lotus/router.rb', line 456 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.
399 400 401 |
# File 'lib/lotus/router.rb', line 399 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.
904 905 906 |
# File 'lib/lotus/router.rb', line 904 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.
361 362 363 |
# File 'lib/lotus/router.rb', line 361 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.
380 381 382 |
# File 'lib/lotus/router.rb', line 380 def put(path, = {}, &blk) @router.put(path, , &blk) end |
#redirect(path, options = {}, &endpoint) ⇒ Lotus::Routing::Route
Defines an HTTP redirect
486 487 488 |
# File 'lib/lotus/router.rb', line 486 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.
654 655 656 |
# File 'lib/lotus/router.rb', line 654 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.
777 778 779 |
# File 'lib/lotus/router.rb', line 777 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.
182 183 184 |
# File 'lib/lotus/router.rb', line 182 def routes self end |
#trace(path, options = {}, &blk) ⇒ Lotus::Routing::Route
Defines a route that accepts a TRACE request for the given path.
437 438 439 |
# File 'lib/lotus/router.rb', line 437 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.
931 932 933 |
# File 'lib/lotus/router.rb', line 931 def url(route, *args) @router.url(route, *args) end |