Class: Hanami::Routing::RecognizedRoute

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

Overview

Represents a result of router path recognition.

See Also:

Since:

  • 0.5.0

Constant Summary collapse

REQUEST_METHOD =

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.5.0

'REQUEST_METHOD'.freeze
PATH_INFO =

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

'PATH_INFO'.freeze
NAMESPACE =

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.5.0

'%s::'.freeze
NAMESPACE_REPLACEMENT =

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.5.0

''.freeze
ACTION_PATH_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.

Since:

  • 0.5.0

'/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, env, router) ⇒ Hanami::Routing::RecognizedRoute

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.

Creates a new instance

Parameters:

  • response (HttpRouter::Response)

    raw response of recognition

  • env (Hash)

    Rack env

  • router (Hanami::Routing::HttpRouter)

    low level router

Since:

  • 0.5.0



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Attribute Details

#paramsObject (readonly)

Since:

  • 0.5.0



33
34
35
# File 'lib/hanami/routing/recognized_route.rb', line 33

def params
  @params
end

Instance Method Details

#actionString

Action name

Examples:

require 'hanami/router'

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

puts router.recognize('/books/23').action # => "books#show"

Returns:

  • (String)

See Also:

Since:

  • 0.5.0



117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Array

Rack protocol compatibility

Parameters:

  • env (Hash)

    Rack env

Returns:

  • (Array)

    serialized Rack response

Raises:

See Also:

Since:

  • 0.5.0



72
73
74
75
76
77
78
# 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

#pathString

Relative URL (path)

Returns:

  • (String)

Since:

  • 0.7.0



96
97
98
# File 'lib/hanami/routing/recognized_route.rb', line 96

def path
  @env[PATH_INFO]
end

#redirect?TrueClass, FalseClass

Check if redirect

Examples:

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

Returns:

  • (TrueClass, FalseClass)

See Also:

Since:

  • 1.0.1



171
172
173
# File 'lib/hanami/routing/recognized_route.rb', line 171

def redirect?
  @endpoint&.redirect? || false
end

#redirection_pathString, NilClass

Returns the redirect destination path

Examples:

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

Returns:

  • (String, NilClass)

    the destination path, if it’s a redirect

See Also:

Since:

  • 1.0.1



195
196
197
# File 'lib/hanami/routing/recognized_route.rb', line 195

def redirection_path
  @endpoint&.destination_path
end

#routable?TrueClass, FalseClass

Check if routable

Examples:

require 'hanami/router'

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

puts router.recognize('/').routable?    # => true
puts router.recognize('/foo').routable? # => false

Returns:

  • (TrueClass, FalseClass)

See Also:

Since:

  • 0.5.0



148
149
150
# File 'lib/hanami/routing/recognized_route.rb', line 148

def routable?
  @endpoint&.routable? || false
end

#verbString

HTTP verb (aka method)

Returns:

  • (String)

Since:

  • 0.5.0



86
87
88
# File 'lib/hanami/routing/recognized_route.rb', line 86

def verb
  @env[REQUEST_METHOD]
end