Class: Journey::Router
- Inherits:
-
Object
- Object
- Journey::Router
- Defined in:
- lib/journey/router.rb,
lib/journey/router/utils.rb,
lib/journey/router/strexp.rb
Defined Under Namespace
Classes: NullReq, RoutingError, Strexp, Utils
Constant Summary collapse
Instance Attribute Summary collapse
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#request_class ⇒ Object
readonly
Returns the value of attribute request_class.
-
#routes ⇒ Object
Returns the value of attribute routes.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(routes, options) ⇒ Router
constructor
A new instance of Router.
- #recognize(req) ⇒ Object
- #visualizer ⇒ Object
Constructor Details
Instance Attribute Details
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
43 44 45 |
# File 'lib/journey/router.rb', line 43 def formatter @formatter end |
#request_class ⇒ Object (readonly)
Returns the value of attribute request_class.
43 44 45 |
# File 'lib/journey/router.rb', line 43 def request_class @request_class end |
#routes ⇒ Object
Returns the value of attribute routes.
44 45 46 |
# File 'lib/journey/router.rb', line 44 def routes @routes end |
Instance Method Details
#call(env) ⇒ Object
53 54 55 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 |
# File 'lib/journey/router.rb', line 53 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('/') env['PATH_INFO'] = Utils.normalize_path(match.post_match) 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
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/journey/router.rb', line 83 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 |
#visualizer ⇒ Object
94 95 96 97 98 99 |
# File 'lib/journey/router.rb', line 94 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 |