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 unless routable?
  namespace = NAMESPACE % @namespace

  if destination.match(namespace)
    Hanami::Utils::String.new(
      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

#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.nil?
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