Class: Lotus::Routing::RecognizedRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/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
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) ⇒ Lotus::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 (Lotus::Routing::HttpRouter)

    low level router

Since:

  • 0.5.0



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lotus/routing/recognized_route.rb', line 41

def initialize(response, env, router)
  @env = env

  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



29
30
31
# File 'lib/lotus/routing/recognized_route.rb', line 29

def params
  @params
end

Instance Method Details

#actionString

Action name

Examples:

require 'lotus/router'

router = Lotus::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



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/lotus/routing/recognized_route.rb', line 101

def action
  namespace = NAMESPACE % @namespace

  if destination.match(namespace)
    Lotus::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



66
67
68
69
70
71
72
# File 'lib/lotus/routing/recognized_route.rb', line 66

def call(env)
  if routable?
    @endpoint.call(env)
  else
    raise Lotus::Router::NotRoutableEndpointError.new(@env)
  end
end

#routable?TrueClass, FalseClass

Check if routable

Examples:

require 'lotus/router'

router = Lotus::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



131
132
133
# File 'lib/lotus/routing/recognized_route.rb', line 131

def routable?
  !!@endpoint
end

#verbString

HTTP verb (aka method)

Returns:

  • (String)

Since:

  • 0.5.0



80
81
82
# File 'lib/lotus/routing/recognized_route.rb', line 80

def verb
  @env[REQUEST_METHOD]
end