class Hanami::Routing::RecognizedRoute

Represents a result of router path recognition.

@since 0.5.0

@see Hanami::Router#recognize

Constants

ACTION_PATH_SEPARATOR

@since 0.5.0 @api private

NAMESPACE

@since 0.5.0 @api private

NAMESPACE_REPLACEMENT

@since 0.5.0 @api private

PATH_INFO

@since 0.7.0 @api private

REQUEST_METHOD

@since 0.5.0 @api private

Attributes

params[R]

@since 0.5.0 @api public

Public Class Methods

new(response, env, router) click to toggle source

Creates a new instance

@param response [HttpRouter::Response] raw response of recognition @param env [Hash] Rack env @param router [Hanami::Routing::HttpRouter] low level router

@return [Hanami::Routing::RecognizedRoute]

@since 0.5.0 @api private

# File lib/hanami/routing/recognized_route.rb, line 45
def initialize(response, env, router)
  @env      = env
  @endpoint = nil
  @params   = {}

  unless response.nil?
    @endpoint = response.route.dest
    @params   = response.params
  end

  @namespace        = router.namespace
  @action_separator = router.action_separator
end

Public Instance Methods

action() click to toggle source

Action name

@return [String]

@since 0.5.0 @api public

@see Hanami::Router#recognize

@example

require 'hanami/router'

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

puts router.recognize('/books/23').action # => "books#show"
# File lib/hanami/routing/recognized_route.rb, line 117
def action
  return if !routable? || redirect?
  namespace = NAMESPACE % @namespace

  if destination.match(namespace)
    Hanami::Utils::String.transform(
      destination.sub(namespace, NAMESPACE_REPLACEMENT),
      :underscore, [:rsub, ACTION_PATH_SEPARATOR, @action_separator])
  else
    destination
  end
end
call(env) click to toggle source

Rack protocol compatibility

@param env [Hash] Rack env

@return [Array] serialized Rack response

@raise [Hanami::Router::NotRoutableEndpointError] if not routable

@since 0.5.0 @api public

@see Hanami::Routing::RecognizedRoute#routable? @see Hanami::Router::NotRoutableEndpointError

# File lib/hanami/routing/recognized_route.rb, line 72
def call(env)
  if routable?
    @endpoint.call(env)
  else
    raise Hanami::Router::NotRoutableEndpointError.new(@env)
  end
end
path() click to toggle source

Relative URL (path)

@return [String]

@since 0.7.0 @api public

# File lib/hanami/routing/recognized_route.rb, line 96
def path
  @env[PATH_INFO]
end
redirect?() click to toggle source

Check if redirect

@return [TrueClass,FalseClass]

@since 1.0.1 @api public

@see Hanami::Router#recognize

@example

require 'hanami/router'

router = Hanami::Router.new do
  get '/', to: 'home#index'
  redirect '/home', to: '/'
end

puts router.recognize('/home').redirect? # => true
puts router.recognize('/').redirect?     # => false
# File lib/hanami/routing/recognized_route.rb, line 171
def redirect?
  @endpoint&.redirect? || false
end
redirection_path() click to toggle source

Returns the redirect destination path

@return [String,NilClass] the destination path, if it's a redirect

@since 1.0.1 @api public

@see Hanami::Router#recognize @see redirect?

@example

require 'hanami/router'

router = Hanami::Router.new do
  get '/', to: 'home#index'
  redirect '/home', to: '/'
end

puts router.recognize('/home').destination_path # => "/"
puts router.recognize('/').destination_path     # => nil
# File lib/hanami/routing/recognized_route.rb, line 195
def redirection_path
  @endpoint&.destination_path
end
routable?() click to toggle source

Check if routable

@return [TrueClass,FalseClass]

@since 0.5.0 @api public

@see Hanami::Router#recognize

@example

require 'hanami/router'

router = Hanami::Router.new do
  get '/', to: 'home#index'
end

puts router.recognize('/').routable?    # => true
puts router.recognize('/foo').routable? # => false
# File lib/hanami/routing/recognized_route.rb, line 148
def routable?
  @endpoint&.routable? || false
end
verb() click to toggle source

HTTP verb (aka method)

@return [String]

@since 0.5.0 @api public

# File lib/hanami/routing/recognized_route.rb, line 86
def verb
  @env[REQUEST_METHOD]
end

Private Instance Methods

destination() click to toggle source

@since 0.5.0 @api private

@see Hanami::Routing::Endpoint

# File lib/hanami/routing/recognized_route.rb, line 205
def destination
  @destination ||= begin
    case k = @endpoint.__getobj__
    when Class
      k.name
    when Proc
      @endpoint.inspect
    else
      k.class.name
    end
  end
end