Class: Sinatra::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/router.rb

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, *args, &block) ⇒ Router

Returns a new instance of Router.



3
4
5
6
7
8
9
10
11
# 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

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



31
32
33
34
# 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) ⇒ Object

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



26
27
28
29
# File 'lib/sinatra/router.rb', line 26

def run(app)
  raise "@run already set" if @run
  @run = app
end

#version(version, &block) ⇒ Object

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



38
39
40
41
42
# File 'lib/sinatra/router.rb', line 38

def version(version, &block)
  @conditions = { version: version }
  instance_eval(&block) if block
  @conditions = {}
end