Class: Hanami::Router
- Inherits:
-
Object
- Object
- Hanami::Router
- Defined in:
- lib/hanami/router.rb,
lib/hanami/router/version.rb
Overview
Rack compatible, lightweight and fast HTTP Router.
Defined Under Namespace
Classes: NotRoutableEndpointError
Constant Summary collapse
- ROOT_PATH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Defines root path
'/'.freeze
- VERSION =
'1.3.2'.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) ⇒ Hanami::Routing::Route
To support defining routes in the ‘define` wrapper.
-
#defined? ⇒ TrueClass, FalseClass
private
Check if there are defined routes.
-
#delete(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a DELETE request for the given path.
-
#get(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a GET request for the given path.
-
#initialize(options = {}, &blk) ⇒ Hanami::Router
constructor
Initialize the router.
-
#inspector ⇒ Object
Returns an routes inspector.
-
#link(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a LINK request for the given path.
-
#mount(app, options) ⇒ Object
Mount a Rack application at the specified path.
-
#namespace(namespace, &blk) ⇒ Hanami::Routing::Namespace
Defines a Ruby block: all the routes defined within it will be namespaced with the given relative path.
-
#options(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a OPTIONS request for the given path.
-
#patch(path, options = {}, &blk) ⇒ Hanami::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) ⇒ Hanami::Routing::Route
Defines a route that accepts a POST request for the given path.
-
#put(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a PUT request for the given path.
-
#recognize(env, options = {}, params = nil) ⇒ Hanami::Routing::RecognizedRoute
Recognize the given env, path, or name and return a route for testing inspection.
-
#redirect(path, options = {}, &endpoint) ⇒ Hanami::Routing::Route
Defines an HTTP redirect.
-
#resource(name, options = {}, &blk) ⇒ Hanami::Routing::Resource
Defines a set of named routes for a single RESTful resource.
-
#resources(name, options = {}, &blk) ⇒ Hanami::Routing::Resources
Defines a set of named routes for a plural RESTful resource.
-
#root(options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a root route (a GET route for ‘/’).
-
#routes ⇒ self
private
Returns self.
-
#trace(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a TRACE request for the given path.
-
#unlink(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts an UNLINK request for the given path.
-
#url(route, *args) ⇒ String
Generate a URL for a specified named route.
Constructor Details
#initialize(options = {}, &blk) ⇒ Hanami::Router
Initialize the router.
237 238 239 240 |
# File 'lib/hanami/router.rb', line 237 def initialize( = {}, &blk) @router = Routing::HttpRouter.new() define(&blk) end |
Class Method Details
.define(&blk) ⇒ Proc
Returns the given block as it is.
When Hanami::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 Hanami::Router.define
, the block doesn’t get yielded but just returned to the caller as it is.
Usually the receiver of this block is Hanami::Router#initialize
, which finally evaluates the block.
140 141 142 |
# File 'lib/hanami/router.rb', line 140 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.
1018 1019 1020 |
# File 'lib/hanami/router.rb', line 1018 def call(env) @router.call(env) end |
#define(&blk) ⇒ Hanami::Routing::Route
To support defining routes in the ‘define` wrapper.
276 277 278 |
# File 'lib/hanami/router.rb', line 276 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
294 295 296 |
# File 'lib/hanami/router.rb', line 294 def defined? @router.routes.any? end |
#delete(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a DELETE request for the given path.
488 489 490 |
# File 'lib/hanami/router.rb', line 488 def delete(path, = {}, &blk) @router.delete(path, , &blk) end |
#get(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a GET request for the given path.
412 413 414 |
# File 'lib/hanami/router.rb', line 412 def get(path, = {}, &blk) @router.get(path, , &blk) end |
#inspector ⇒ Object
Returns an routes inspector
1216 1217 1218 1219 |
# File 'lib/hanami/router.rb', line 1216 def inspector require 'hanami/routing/routes_inspector' Routing::RoutesInspector.new(@router.routes, @router.prefix) end |
#link(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a LINK request for the given path.
526 527 528 |
# File 'lib/hanami/router.rb', line 526 def link(path, = {}, &blk) @router.link(path, , &blk) 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.
1007 1008 1009 |
# File 'lib/hanami/router.rb', line 1007 def mount(app, ) @router.mount(app, ) end |
#namespace(namespace, &blk) ⇒ Hanami::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.
673 674 675 |
# File 'lib/hanami/router.rb', line 673 def namespace(namespace, &blk) Routing::Namespace.new(self, namespace, &blk) end |
#options(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a OPTIONS request for the given path.
594 595 596 |
# File 'lib/hanami/router.rb', line 594 def (path, = {}, &blk) @router.(path, , &blk) end |
#patch(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a PATCH request for the given path.
469 470 471 |
# File 'lib/hanami/router.rb', line 469 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.
1164 1165 1166 |
# File 'lib/hanami/router.rb', line 1164 def path(route, *args) @router.path(route, *args) end |
#post(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a POST request for the given path.
431 432 433 |
# File 'lib/hanami/router.rb', line 431 def post(path, = {}, &blk) @router.post(path, , &blk) end |
#put(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a PUT request for the given path.
450 451 452 |
# File 'lib/hanami/router.rb', line 450 def put(path, = {}, &blk) @router.put(path, , &blk) end |
#recognize(env, options = {}, params = nil) ⇒ Hanami::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.
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/hanami/router.rb', line 1130 def recognize(env, = {}, params = nil) require 'hanami/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) ⇒ Hanami::Routing::Route
Defines an HTTP redirect
624 625 626 627 628 629 |
# File 'lib/hanami/router.rb', line 624 def redirect(path, = {}, &endpoint) destination_path = @router.find() get(path).redirect(destination_path, [:code] || 301).tap do |route| route.dest = Hanami::Routing::RedirectEndpoint.new(destination_path, route.dest) end end |
#resource(name, options = {}, &blk) ⇒ Hanami::Routing::Resource
Defines a set of named routes for a single RESTful resource. It has a built-in integration for Hanami::Controller.
795 796 797 |
# File 'lib/hanami/router.rb', line 795 def resource(name, = {}, &blk) Routing::Resource.new(self, name, .merge(separator: @router.action_separator), &blk) end |
#resources(name, options = {}, &blk) ⇒ Hanami::Routing::Resources
Defines a set of named routes for a plural RESTful resource. It has a built-in integration for Hanami::Controller.
918 919 920 |
# File 'lib/hanami/router.rb', line 918 def resources(name, = {}, &blk) Routing::Resources.new(self, name, .merge(separator: @router.action_separator), &blk) end |
#root(options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a root route (a GET route for ‘/’)
575 576 577 |
# File 'lib/hanami/router.rb', line 575 def root( = {}, &blk) @router.get(ROOT_PATH, .merge(as: :root), &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 ‘Hanami::Application`. It’s used by ‘Hanami::Routing::RoutesInspector` to inspect both apps and routers.
252 253 254 |
# File 'lib/hanami/router.rb', line 252 def routes self end |
#trace(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a TRACE request for the given path.
507 508 509 |
# File 'lib/hanami/router.rb', line 507 def trace(path, = {}, &blk) @router.trace(path, , &blk) end |
#unlink(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts an UNLINK request for the given path.
545 546 547 |
# File 'lib/hanami/router.rb', line 545 def unlink(path, = {}, &blk) @router.unlink(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.
1191 1192 1193 |
# File 'lib/hanami/router.rb', line 1191 def url(route, *args) @router.url(route, *args) end |