Class: ActionDispatch::Journey::Router

Inherits:
Object
  • Object
show all
Defined in:
actionpack/lib/action_dispatch/journey/router.rb,
actionpack/lib/action_dispatch/journey/router/utils.rb,
actionpack/lib/action_dispatch/journey/router/strexp.rb

Overview

:nodoc:

Defined Under Namespace

Classes: NullReq, RoutingError, Strexp, Utils

Constant Summary collapse

VERSION =

:nodoc:

'2.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routes, options) ⇒ Router

Returns a new instance of Router.



49
50
51
52
53
54
# File 'actionpack/lib/action_dispatch/journey/router.rb', line 49

def initialize(routes, options)
  @options       = options
  @params_key    = options[:parameters_key]
  @request_class = options[:request_class] || NullReq
  @routes        = routes
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter



46
47
48
# File 'actionpack/lib/action_dispatch/journey/router.rb', line 46

def formatter
  @formatter
end

#request_classObject (readonly)

Returns the value of attribute request_class



46
47
48
# File 'actionpack/lib/action_dispatch/journey/router.rb', line 46

def request_class
  @request_class
end

#routesObject

Returns the value of attribute routes



47
48
49
# File 'actionpack/lib/action_dispatch/journey/router.rb', line 47

def routes
  @routes
end

Instance Method Details

#call(env) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'actionpack/lib/action_dispatch/journey/router.rb', line 56

def call(env)
  env['PATH_INFO'] = Utils.normalize_path(env['PATH_INFO'])

  find_routes(env).each do |match, parameters, route|
    script_name, path_info, set_params = env.values_at('SCRIPT_NAME',
                                                       'PATH_INFO',
                                                       @params_key)

    unless route.path.anchored
      env['SCRIPT_NAME'] = (script_name.to_s + match.to_s).chomp('/')
      path_info = match.post_match
      env['PATH_INFO']   = path_info
      env['PATH_INFO']   = "/" + path_info unless path_info.start_with? "/"
    end

    env[@params_key] = (set_params || {}).merge parameters

    status, headers, body = route.app.call(env)

    if 'pass' == headers['X-Cascade']
      env['SCRIPT_NAME'] = script_name
      env['PATH_INFO']   = path_info
      env[@params_key]   = set_params
      next
    end

    return [status, headers, body]
  end

  return [404, {'X-Cascade' => 'pass'}, ['Not Found']]
end

#recognize(req) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'actionpack/lib/action_dispatch/journey/router.rb', line 88

def recognize(req)
  find_routes(req.env).each do |match, parameters, route|
    unless route.path.anchored
      req.env['SCRIPT_NAME'] = match.to_s
      req.env['PATH_INFO']   = match.post_match.sub(/^([^\/])/, '/\1')
    end

    yield(route, nil, parameters)
  end
end

#visualizerObject



99
100
101
102
103
104
# File 'actionpack/lib/action_dispatch/journey/router.rb', line 99

def visualizer
  tt     = GTG::Builder.new(ast).transition_table
  groups = partitioned_routes.first.map(&:ast).group_by { |a| a.to_s }
  asts   = groups.values.map { |v| v.first }
  tt.visualizer(asts)
end