Module: Rack::Mount::Recognition::RouteSet

Included in:
Rack::Mount::RouteSet
Defined in:
lib/rack/mount/recognition/route_set.rb

Constant Summary collapse

X_CASCADE =
'X-Cascade'.freeze
PASS =
'pass'.freeze
PATH_INFO =
'PATH_INFO'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parameters_keyObject (readonly)

Returns the value of attribute parameters_key.



6
7
8
# File 'lib/rack/mount/recognition/route_set.rb', line 6

def parameters_key
  @parameters_key
end

Instance Method Details

#add_route(*args) ⇒ Object

Adds recognition aspects to RouteSet#add_route.



18
19
20
21
22
# File 'lib/rack/mount/recognition/route_set.rb', line 18

def add_route(*args)
  route = super
  @recognition_key_analyzer << route.conditions
  route
end

#call(env) ⇒ Object

Rack compatible recognition and dispatching method. Routes are tried until one returns a non-catch status code. If no routes match, the catch status code is returned.

This method can only be invoked after the RouteSet has been finalized.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rack/mount/recognition/route_set.rb', line 58

def call(env)
  raise 'route set not finalized' unless @recognition_graph

  env[PATH_INFO] = Utils.normalize_path(env[PATH_INFO])

  request = nil
  req = @request_class.new(env)
  recognize(req) do |route, params|
    # TODO: We only want to unescape params from uri related methods
    params.each { |k, v| params[k] = Utils.unescape_uri(v) if v.is_a?(String) }

    env[@parameters_key] = params
    result = route.app.call(env)
    return result unless result[1][X_CASCADE] == PASS
  end

  request || [404, {'Content-Type' => 'text/html', 'X-Cascade' => 'pass'}, ['Not Found']]
end

#initialize(options = {}) ⇒ Object

Adds recognition related concerns to RouteSet.new.



9
10
11
12
13
14
15
# File 'lib/rack/mount/recognition/route_set.rb', line 9

def initialize(options = {})
  @parameters_key = options.delete(:parameters_key) || 'rack.routing_args'
  @parameters_key.freeze
  @recognition_key_analyzer = Analysis::Frequency.new_with_module(Analysis::Splitting)

  super
end

#recognize(obj) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/mount/recognition/route_set.rb', line 24

def recognize(obj)
  raise 'route set not finalized' unless @recognition_graph

  cache = {}
  keys = @recognition_keys.map { |key|
    if key.is_a?(Array)
      key.call(cache, obj)
    else
      obj.send(key)
    end
  }
  @recognition_graph[*keys].each do |route|
    if params = route.recognize(obj)
      if block_given?
        yield route, params
      else
        return route, params
      end
    end
  end

  nil
end

#rehashObject

:nodoc:



77
78
79
80
81
82
# File 'lib/rack/mount/recognition/route_set.rb', line 77

def rehash #:nodoc:
  @recognition_keys  = build_recognition_keys
  @recognition_graph = build_recognition_graph

  super
end