Class: Hanami::Routing::EndpointResolver Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/routing/endpoint_resolver.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Resolve duck-typed endpoints

Since:

  • 0.1.0

Constant Summary collapse

NAMING_PATTERN =

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.

Since:

  • 0.2.0

'%{controller}::%{action}'.freeze
DEFAULT_RESPONSE =

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.

Since:

  • 0.7.0

[404, {'X-Cascade' => 'pass'}, 'Not Found'].freeze
ACTION_SEPARATOR =

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.

Default separator for controller and action. A different separator can be passed to #initialize with the ‘:separator` option.

Examples:

require 'hanami/router'

router = Hanami::Router.new do
  get '/', to: 'articles#show'
end

See Also:

Since:

  • 0.1.0

'#'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hanami::Routing::EndpointResolver

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.

Initialize an endpoint resolver

Examples:

Specify custom endpoint class

require 'hanami/router'

resolver = Hanami::Routing::EndpointResolver.new(endpoint: CustomEndpoint)
router   = Hanami::Router.new(resolver: resolver)

router.get('/', to: endpoint).dest # => #<CustomEndpoint:0x007f97f3359570 ...>

Specify custom Ruby namespace

require 'hanami/router'

resolver = Hanami::Routing::EndpointResolver.new(namespace: MyApp)
router   = Hanami::Router.new(resolver: resolver)

router.get('/', to: 'articles#show')
  # => Will look for: MyApp::Articles::Show

Specify custom pattern

require 'hanami/router'

resolver = Hanami::Routing::EndpointResolver.new(pattern: '%{controller}Controller::%{action}')
router   = Hanami::Router.new(resolver: resolver)

router.get('/', to: 'articles#show')
  # => Will look for: ArticlesController::Show

Specify custom controller-action separator

require 'hanami/router'

resolver = Hanami::Routing::EndpointResolver.new(separator: '@')
router   = Hanami::Router.new(resolver: resolver)

router.get('/', to: 'articles@show')
  # => Will look for: Articles::Show

Parameters:

  • options (Hash) (defaults to: {})

    the options used to customize lookup behavior

Options Hash (options):

  • :endpoint (Class)

    the endpoint class that is returned by ‘#resolve`. (defaults to `Hanami::Routing::Endpoint`)

  • :namespace (Class, Module)

    the Ruby namespace where to lookup for controllers and actions. (defaults to ‘Object`)

  • :pattern (String)

    the string to interpolate in order to return an action name. This string SHOULD contain '%{controller}' and '%{action}', all the other keys will be ignored. See the examples below.

  • :action_separator (String)

    the separator between controller and action name. (defaults to ‘ACTION_SEPARATOR`)

Since:

  • 0.1.0



101
102
103
104
105
106
# File 'lib/hanami/routing/endpoint_resolver.rb', line 101

def initialize(options = {})
  @endpoint_class   = options[:endpoint]         || Endpoint
  @namespace        = options[:namespace]        || Object
  @action_separator = options[:action_separator] || ACTION_SEPARATOR
  @pattern          = options[:pattern]          || NAMING_PATTERN
end

Instance Attribute Details

#action_separatorObject (readonly)

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.

Since:

  • 0.1.0



37
38
39
# File 'lib/hanami/routing/endpoint_resolver.rb', line 37

def action_separator
  @action_separator
end

Instance Method Details

#find(options) ⇒ Object

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.

Finds a path from the given options.

Parameters:

  • options (Hash)

    the path description

Options Hash (options):

  • :to (String, Proc, Class, Object#call)

    the endpoint

  • :namespace (String)

    an optional namespace

Returns:

  • (Object)

Since:

  • 0.1.0



181
182
183
# File 'lib/hanami/routing/endpoint_resolver.rb', line 181

def find(options)
  options[:to]
end

#resolve(options, &endpoint) ⇒ Endpoint

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.

Resolve the given set of HTTP verb, path, endpoint and options. If it fails to resolve, it will mount the default endpoint to the given path, which returns an 404 (Not Found).

Examples:

Resolve to a Proc

require 'hanami/router'

router = Hanami::Router.new
router.get '/', to: ->(env) { [200, {}, ['Hi!']] }

Resolve to a class

require 'hanami/router'

router = Hanami::Router.new
router.get '/', to: RackMiddleware

Resolve to a Rack compatible object (respond to #call)

require 'hanami/router'

router = Hanami::Router.new
router.get '/', to: AnotherMiddleware.new

Resolve to a Hanami::Action from a string (see Hanami::Controller framework)

require 'hanami/router'

router = Hanami::Router.new
router.get '/', to: 'articles#show'

Resolve to a Hanami::Action (see Hanami::Controller framework)

require 'hanami/router'

router = Hanami::Router.new
router.get '/', to: Articles::Show

Resolve a redirect with a namespace

require 'hanami/router'

router = Hanami::Router.new
router.namespace 'users' do
  get '/home',           to: ->(env) { ... }
  redirect '/dashboard', to: '/home'
end

# GET /users/dashboard => 301 Location: "/users/home"

Parameters:

  • options (Hash)

    the options required to resolve the endpoint

Options Hash (options):

  • :to (String, Proc, Class, Object#call)

    the endpoint

  • :namespace (String)

    an optional routing namespace

Returns:

  • (Endpoint)

    this may vary according to the :endpoint option passed to #initialize

See Also:

Since:

  • 0.1.0



166
167
168
169
# File 'lib/hanami/routing/endpoint_resolver.rb', line 166

def resolve(options, &endpoint)
  result = endpoint || find(options)
  resolve_callable(result) || resolve_matchable(result) || default
end