class Journey::Router

Constants

VERSION

Attributes

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

Public Class Methods

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

Public Instance Methods

call(env) click to toggle source
# 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) { |route, nil, parameters| ... } click to toggle source
# 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() click to toggle source
# 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

Private Instance Methods

ast() click to toggle source
# File lib/journey/router.rb, line 107
def ast
  routes.ast
end
custom_routes() click to toggle source
# File lib/journey/router.rb, line 115
def custom_routes
  partitioned_routes.last
end
filter_routes(path) click to toggle source
# File lib/journey/router.rb, line 119
def filter_routes path
  return [] unless ast
  data = simulator.match(path)
  data ? data.memos : []
end
find_routes(env) click to toggle source
# File lib/journey/router.rb, line 125
def find_routes env
  req = request_class.new env

  routes = filter_routes(req.path_info) + custom_routes.find_all { |r|
    r.path.match(req.path_info)
  }

  routes.sort_by(&:precedence).find_all { |r|
    r.constraints.all? { |k,v| v === req.send(k) } &&
      r.verb === req.request_method
  }.reject { |r| req.ip && !(r.ip === req.ip) }.map { |r|
    match_data  = r.path.match(req.path_info)
    match_names = match_data.names.map { |n| n.to_sym }
    match_values = match_data.captures.map { |v| v && Utils.unescape_uri(v) }
    info = Hash[match_names.zip(match_values).find_all { |_,y| y }]

    [match_data, r.defaults.merge(info), r]
  }
end
partitioned_routes() click to toggle source
# File lib/journey/router.rb, line 103
def partitioned_routes
  routes.partitioned_routes
end
simulator() click to toggle source
# File lib/journey/router.rb, line 111
def simulator
  routes.simulator
end