Base class for all Sinatra applications and middleware.

Methods
C
D
F
G
H
N
O
P
R
S
T
U
Included Modules
Constants
CALLERS_TO_IGNORE = [ /\/sinatra(\/(base|main|showexceptions))?\.rb$/, # all sinatra code /lib\/tilt.*\.rb$/, # all tilt code /\(.*\)/, # generated code /custom_require\.rb$/, # rubygems require hacks /active_support/, # active_support require hacks ]
Attributes
[RW] app
[RW] env
[RW] request
[RW] response
[RW] params
[R] routes
[R] before_filters
[R] after_filters
[R] templates
[R] errors
Class Public methods
call(env)
     # File lib/sinatra/base.rb, line 962
962:       def call(env)
963:         synchronize { prototype.call(env) }
964:       end
caller_files()

Like Kernel#caller but excluding certain magic entries and without line / method information; the resulting array contains filenames only.

      # File lib/sinatra/base.rb, line 1012
1012:       def caller_files
1013:         caller_locations.
1014:           map { |file,line| file }
1015:       end
caller_locations()
      # File lib/sinatra/base.rb, line 1017
1017:       def caller_locations
1018:         caller(1).
1019:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
1020:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1021:       end
configure(*envs, &block)

Set configuration options for Sinatra and/or the app. Allows scoping of settings for certain environments.

     # File lib/sinatra/base.rb, line 912
912:       def configure(*envs, &block)
913:         yield self if envs.empty? || envs.include?(environment.to_sym)
914:       end
delete(path, opts={}, &bk)
     # File lib/sinatra/base.rb, line 830
830:       def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk end
development?()
     # File lib/sinatra/base.rb, line 906
906:       def development?; environment == :development end
get(path, opts={}, &block)

Defining a `GET` handler also automatically defines a `HEAD` handler.

     # File lib/sinatra/base.rb, line 820
820:       def get(path, opts={}, &block)
821:         conditions = @conditions.dup
822:         route('GET', path, opts, &block)
823: 
824:         @conditions = conditions
825:         route('HEAD', path, opts, &block)
826:       end
head(path, opts={}, &bk)
     # File lib/sinatra/base.rb, line 831
831:       def head(path, opts={}, &bk);   route 'HEAD',   path, opts, &bk end
helpers(*extensions, &block)

Makes the methods defined in the block and in the Modules given in `extensions` available to the handlers and templates

     # File lib/sinatra/base.rb, line 892
892:       def helpers(*extensions, &block)
893:         class_eval(&block)  if block_given?
894:         include(*extensions) if extensions.any?
895:       end
new(*args, &bk)

Create a new instance of the class fronted by its middleware pipeline. The object is guaranteed to respond to call but may not be an instance of the class new was called on.

     # File lib/sinatra/base.rb, line 950
950:       def new(*args, &bk)
951:         builder = Rack::Builder.new
952:         builder.use Rack::Session::Cookie if sessions?
953:         builder.use Rack::CommonLogger    if logging?
954:         builder.use Rack::MethodOverride  if methodoverride?
955:         builder.use ShowExceptions        if show_exceptions?
956:         middleware.each { |c,a,b| builder.use(c, *a, &b) }
957: 
958:         builder.run super
959:         builder.to_app
960:       end
new(app=nil)
     # File lib/sinatra/base.rb, line 372
372:     def initialize(app=nil)
373:       @app = app
374:       @template_cache = Tilt::Cache.new
375:       yield self if block_given?
376:     end
post(path, opts={}, &bk)
     # File lib/sinatra/base.rb, line 829
829:       def post(path, opts={}, &bk);   route 'POST',   path, opts, &bk end
production?()
     # File lib/sinatra/base.rb, line 907
907:       def production?;  environment == :production  end
prototype()

The prototype instance used to process requests.

     # File lib/sinatra/base.rb, line 943
943:       def prototype
944:         @prototype ||= new
945:       end
put(path, opts={}, &bk)
     # File lib/sinatra/base.rb, line 828
828:       def put(path, opts={}, &bk);    route 'PUT',    path, opts, &bk end
register(*extensions, &block)
     # File lib/sinatra/base.rb, line 897
897:       def register(*extensions, &block)
898:         extensions << Module.new(&block) if block_given?
899:         @extensions += extensions
900:         extensions.each do |extension|
901:           extend extension
902:           extension.registered(self) if extension.respond_to?(:registered)
903:         end
904:       end
run!(options={})

Run the Sinatra app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)

     # File lib/sinatra/base.rb, line 924
924:       def run!(options={})
925:         set options
926:         handler      = detect_rack_handler
927:         handler_name = handler.name.gsub(/.*::/, '')
928:         puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
929:           "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
930:         handler.run self, :Host => host, :Port => port do |server|
931:           trap(:INT) do
932:             ## Use thins' hard #stop! if available, otherwise just #stop
933:             server.respond_to?(:stop!) ? server.stop! : server.stop
934:             puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
935:           end
936:           set :running, true
937:         end
938:       rescue Errno::EADDRINUSE => e
939:         puts "== Someone is already performing on port #{port}!"
940:       end
test?()
     # File lib/sinatra/base.rb, line 908
908:       def test?;        environment == :test        end
use(middleware, *args, &block)

Use the specified Rack middleware

     # File lib/sinatra/base.rb, line 917
917:       def use(middleware, *args, &block)
918:         @prototype = nil
919:         @middleware << [middleware, args, block]
920:       end
Instance Public methods
call(env)

Rack call interface.

     # File lib/sinatra/base.rb, line 379
379:     def call(env)
380:       dup.call!(env)
381:     end
call!(env)
     # File lib/sinatra/base.rb, line 385
385:     def call!(env)
386:       @env      = env
387:       @request  = Request.new(env)
388:       @response = Response.new
389:       @params   = indifferent_params(@request.params)
390:       @template_cache.clear if settings.reload_templates
391: 
392:       invoke { dispatch! }
393:       invoke { error_block!(response.status) }
394: 
395:       status, header, body = @response.finish
396: 
397:       # Never produce a body on HEAD requests. Do retain the Content-Length
398:       # unless it's "0", in which case we assume it was calculated erroneously
399:       # for a manual HEAD response and remove it entirely.
400:       if @env['REQUEST_METHOD'] == 'HEAD'
401:         body = []
402:         header.delete('Content-Length') if header['Content-Length'] == '0'
403:       end
404: 
405:       [status, header, body]
406:     end
forward()

Forward the request to the downstream app — middleware only.

     # File lib/sinatra/base.rb, line 429
429:     def forward
430:       fail "downstream app not set" unless @app.respond_to? :call
431:       status, headers, body = @app.call(@request.env)
432:       @response.status = status
433:       @response.body = body
434:       @response.headers.merge! headers
435:       nil
436:     end
halt(*response)

Exit the current block, halts any further processing of the request, and returns the specified response.

     # File lib/sinatra/base.rb, line 416
416:     def halt(*response)
417:       response = response.first if response.length == 1
418:       throw :halt, response
419:     end
options()

Alias for settings

pass(&block)

Pass control to the next matching route. If there are no more matching routes, Sinatra will return a 404 response.

     # File lib/sinatra/base.rb, line 424
424:     def pass(&block)
425:       throw :pass, block
426:     end
settings()

Access settings defined with Base.set.

This method is also aliased as options
     # File lib/sinatra/base.rb, line 409
409:     def settings
410:       self.class
411:     end