class Sinatra::Router

Public Class Methods

new(app=nil, *args, &block) click to toggle source
# File lib/sinatra/router.rb, line 3
def initialize(app=nil, *args, &block)
  @app        = app
  @apps       = []
  @conditions = []
  @run        = nil

  instance_eval(&block) if block
  @routes = build_routing_table
end

Public Instance Methods

call(env) click to toggle source
# File lib/sinatra/router.rb, line 13
def call(env)
  if ret = try_route(env["REQUEST_METHOD"], env["PATH_INFO"], env)
    ret
  else
    raise "router needs to be (1) mounted as middleware or (b) contain " +
      "a default run statement" if !@app && !@run

    # if set as middlware, prefer that, otherwise try default run module
    (@app || @run).call(env)
  end
end
mount(app, *conditions) click to toggle source
# File lib/sinatra/router.rb, line 31
def mount(app, *conditions)
  # mix in context based conditions with conditions given by parameter
  @apps << [app, @conditions + conditions]
end
run(app) click to toggle source

specify the default app to run if no other app routes matched

# File lib/sinatra/router.rb, line 26
def run(app)
  raise "@run already set" if @run
  @run = app
end
version(version, &block) click to toggle source

yield to a builder block in which all defined apps will only respond for the given version

# File lib/sinatra/router.rb, line 38
def version(version, &block)
  @conditions = { version: version }
  instance_eval(&block) if block
  @conditions = {}
end

Protected Instance Methods

with_conditions(*args, &block) click to toggle source
# File lib/sinatra/router.rb, line 46
def with_conditions(*args, &block)
  old = @conditions
  @conditions = @conditions + args
  instance_eval(&block) if block
  @conditions = old
end

Private Instance Methods

build_routing_table() click to toggle source
# File lib/sinatra/router.rb, line 55
def build_routing_table
  all_routes = {}
  @apps.each do |app, conditions|
    next unless app.respond_to?(:routes)
    app.routes.each do |verb, routes|
      all_routes[verb] ||= []
      all_routes[verb] += routes.map do |pattern, _, _, _|
        [pattern, conditions, app]
      end
    end
  end
  all_routes
end
conditions_match?(conditions, env) click to toggle source
# File lib/sinatra/router.rb, line 69
def conditions_match?(conditions, env)
  conditions.each do |condition|
    return false unless condition.call(env)
  end
  true
end
try_route(verb, path, env) click to toggle source
# File lib/sinatra/router.rb, line 76
def try_route(verb, path, env)
  # see Sinatra's `route!`
  if verb_routes = @routes[verb]
    verb_routes.each do |pattern, conditions, app|
      if pattern.match(path) && conditions_match?(conditions, env)
        status, headers, response = app.call(env)

        # if we got a pass, keep trying routes
        next if headers["X-Cascade"] == "pass"

        return status, headers, response
      end
    end
  end
  nil
end