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 =
'0.8.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) ⇒ 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.
228 229 230 231 |
# File 'lib/hanami/router.rb', line 228 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.
131 132 133 |
# File 'lib/hanami/router.rb', line 131 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.
1006 1007 1008 |
# File 'lib/hanami/router.rb', line 1006 def call(env) @router.call(env) end |
#define(&blk) ⇒ Hanami::Routing::Route
To support defining routes in the ‘define` wrapper.
267 268 269 |
# File 'lib/hanami/router.rb', line 267 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
285 286 287 |
# File 'lib/hanami/router.rb', line 285 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.
479 480 481 |
# File 'lib/hanami/router.rb', line 479 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.
403 404 405 |
# File 'lib/hanami/router.rb', line 403 def get(path, = {}, &blk) @router.get(path, , &blk) end |
#inspector ⇒ Object
Returns an routes inspector
1204 1205 1206 1207 |
# File 'lib/hanami/router.rb', line 1204 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.
517 518 519 |
# File 'lib/hanami/router.rb', line 517 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.
995 996 997 |
# File 'lib/hanami/router.rb', line 995 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.
661 662 663 |
# File 'lib/hanami/router.rb', line 661 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.
585 586 587 |
# File 'lib/hanami/router.rb', line 585 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.
460 461 462 |
# File 'lib/hanami/router.rb', line 460 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.
1152 1153 1154 |
# File 'lib/hanami/router.rb', line 1152 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.
422 423 424 |
# File 'lib/hanami/router.rb', line 422 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.
441 442 443 |
# File 'lib/hanami/router.rb', line 441 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.
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 |
# File 'lib/hanami/router.rb', line 1118 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
615 616 617 |
# File 'lib/hanami/router.rb', line 615 def redirect(path, = {}, &endpoint) get(path).redirect @router.find(), [:code] || 301 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.
783 784 785 |
# File 'lib/hanami/router.rb', line 783 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.
906 907 908 |
# File 'lib/hanami/router.rb', line 906 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 ‘/’)
566 567 568 |
# File 'lib/hanami/router.rb', line 566 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.
243 244 245 |
# File 'lib/hanami/router.rb', line 243 def routes self end |
#trace(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a TRACE request for the given path.
498 499 500 |
# File 'lib/hanami/router.rb', line 498 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.
536 537 538 |
# File 'lib/hanami/router.rb', line 536 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.
1179 1180 1181 |
# File 'lib/hanami/router.rb', line 1179 def url(route, *args) @router.url(route, *args) end |