Parent

Journey::Router

Constants

VERSION

Attributes

request_class[R]
formatter[R]
routes[RW]

Public Class Methods

new(routes, options) click to toggle source
    # File lib/journey/router.rb, line 46
46:     def initialize routes, options
47:       @options       = options
48:       @params_key    = options[:parameters_key]
49:       @request_class = options[:request_class] || NullReq
50:       @routes        = routes
51:     end

Public Instance Methods

call(env) click to toggle source
    # File lib/journey/router.rb, line 53
53:     def call env
54:       env['PATH_INFO'] = Utils.normalize_path env['PATH_INFO']
55: 
56:       find_routes(env).each do |match, parameters, route|
57:         script_name, path_info, set_params = env.values_at('SCRIPT_NAME',
58:                                                            'PATH_INFO',
59:                                                            @params_key)
60: 
61:         unless route.path.anchored
62:           env['SCRIPT_NAME'] = (script_name.to_s + match.to_s).chomp('/')
63:           env['PATH_INFO']   = Utils.normalize_path(match.post_match)
64:         end
65: 
66:         env[@params_key] = (set_params || {}).merge parameters
67: 
68:         status, headers, body = route.app.call(env)
69: 
70:         if 'pass' == headers['X-Cascade']
71:           env['SCRIPT_NAME'] = script_name
72:           env['PATH_INFO']   = path_info
73:           env[@params_key]   = set_params
74:           next
75:         end
76: 
77:         return [status, headers, body]
78:       end
79: 
80:       return [404, {'X-Cascade' => 'pass'}, ['Not Found']]
81:     end
recognize(req) click to toggle source
    # File lib/journey/router.rb, line 83
83:     def recognize req
84:       find_routes(req.env).each do |match, parameters, route|
85:         unless route.path.anchored
86:           req.env['SCRIPT_NAME'] = match.to_s
87:           req.env['PATH_INFO']   = match.post_match.sub(/^([^\/])/, '/\1')
88:         end
89: 
90:         yield(route, nil, parameters)
91:       end
92:     end
visualizer() click to toggle source
    # File lib/journey/router.rb, line 94
94:     def visualizer
95:       tt     = GTG::Builder.new(ast).transition_table
96:       groups = partitioned_routes.first.map(&:ast).group_by { |a| a.to_s }
97:       asts   = groups.values.map { |v| v.first }
98:       tt.visualizer asts
99:     end

Private Instance Methods

ast() click to toggle source
     # File lib/journey/router.rb, line 107
107:     def ast
108:       routes.ast
109:     end
custom_routes() click to toggle source
     # File lib/journey/router.rb, line 115
115:     def custom_routes
116:       partitioned_routes.last
117:     end
filter_routes(path) click to toggle source
     # File lib/journey/router.rb, line 119
119:     def filter_routes path
120:       return [] unless ast
121:       data = simulator.match(path)
122:       data ? data.memos : []
123:     end
find_routes(env) click to toggle source
     # File lib/journey/router.rb, line 125
125:     def find_routes env
126:       req = request_class.new env
127: 
128:       routes = filter_routes(req.path_info) + custom_routes.find_all { |r|
129:         r.path.match(req.path_info)
130:       }
131: 
132:       routes.sort_by(&:precedence).find_all { |r|
133:         r.constraints.all? { |k,v| v === req.send(k) } &&
134:           r.verb === req.request_method
135:       }.reject { |r| req.ip && !(r.ip === req.ip) }.map { |r|
136:         match_data  = r.path.match(req.path_info)
137:         match_names = match_data.names.map { |n| n.to_sym }
138:         match_values = match_data.captures.map { |v| v && Utils.unescape_uri(v) }
139:         info = Hash[match_names.zip(match_values).find_all { |_,y| y }]
140: 
141:         [match_data, r.defaults.merge(info), r]
142:       }
143:     end
partitioned_routes() click to toggle source
     # File lib/journey/router.rb, line 103
103:     def partitioned_routes
104:       routes.partitioned_routes
105:     end
simulator() click to toggle source
     # File lib/journey/router.rb, line 111
111:     def simulator
112:       routes.simulator
113:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.