Class: Hanami::Router
- Inherits:
-
Object
- Object
- Hanami::Router
- Defined in:
- gems/gems/hanami-router-1.3.1/lib/hanami/router.rb,
gems/gems/hanami-router-1.3.1/lib/hanami/router/version.rb,
gems/gems/hanami-router-1.3.2/lib/hanami/router/version.rb,
gems/gems/hanami-router-1.3.2/lib/hanami/router.rb
Overview
Rack compatible, lightweight and fast HTTP Router.
Defined Under Namespace
Classes: NotRoutableEndpointError
Constant Summary collapse
- 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. -
#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 ‘/’).
-
#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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
def call(env) @router.call(env) end |
#define(&blk) ⇒ Hanami::Routing::Route
To support defining routes in the define
wrapper.
Source: | on GitHub
def define(&blk) instance_eval(&blk) if block_given? end |
#delete(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a DELETE request for the given path.
Source: | on GitHub
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.
Source: | on GitHub
def get(path, = {}, &blk) @router.get(path, , &blk) end |
#inspector ⇒ Object
Returns an routes inspector
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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
#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.
Source: | on GitHub
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.
Source: | on GitHub
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 ‘/’)
#trace(path, options = {}, &blk) ⇒ Hanami::Routing::Route
Defines a route that accepts a TRACE request for the given path.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
def url(route, *args) @router.url(route, *args) end |