Base class for all Sinatra applications and middleware.

Methods
C
D
E
F
G
H
N
O
P
R
T
U
Included Modules
Constants
CALLERS_TO_IGNORE = [ /\/sinatra(\/(base|main|showexceptions|compat))?\.rb$/, # all sinatra code /\(.*\)/, # generated code /custom_require\.rb$/, # rubygems require hacks /active_support/, # active_support require hacks ] unless self.const_defined?('CALLERS_TO_IGNORE')
Attributes
[RW] app
[RW] env
[RW] request
[RW] response
[RW] params
[RW] routes
[RW] filters
[RW] conditions
[RW] templates
[RW] middleware
[RW] errors
Class Public methods
call(env)
     # File lib/sinatra/base.rb, line 897
897:       def call(env)
898:         synchronize { prototype.call(env) }
899:       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 965
965:       def caller_files
966:         caller_locations.
967:           map { |file,line| file }
968:       end
caller_locations()
     # File lib/sinatra/base.rb, line 970
970:       def caller_locations
971:         caller(1).
972:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
973:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
974:       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 848
848:       def configure(*envs, &block)
849:         yield self if envs.empty? || envs.include?(environment.to_sym)
850:       end
delete(path, opts={}, &bk)
     # File lib/sinatra/base.rb, line 762
762:       def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk end
development?()
     # File lib/sinatra/base.rb, line 842
842:       def development?; environment == :development end
extensions()
     # File lib/sinatra/base.rb, line 829
829:       def extensions
830:         (@extensions + (superclass.extensions rescue [])).uniq
831:       end
get(path, opts={}, &block)

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

     # File lib/sinatra/base.rb, line 752
752:       def get(path, opts={}, &block)
753:         conditions = @conditions.dup
754:         route('GET', path, opts, &block)
755: 
756:         @conditions = conditions
757:         route('HEAD', path, opts, &block)
758:       end
head(path, opts={}, &bk)
     # File lib/sinatra/base.rb, line 763
763:       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 824
824:       def helpers(*extensions, &block)
825:         class_eval(&block)  if block_given?
826:         include(*extensions) if extensions.any?
827:       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 885
885:       def new(*args, &bk)
886:         builder = Rack::Builder.new
887:         builder.use Rack::Session::Cookie if sessions? && !test?
888:         builder.use Rack::CommonLogger    if logging?
889:         builder.use Rack::MethodOverride  if methodoverride?
890:         builder.use ShowExceptions        if show_exceptions?
891: 
892:         @middleware.each { |c,a,b| builder.use(c, *a, &b) }
893:         builder.run super
894:         builder.to_app
895:       end
new(app=nil)
     # File lib/sinatra/base.rb, line 357
357:     def initialize(app=nil)
358:       @app = app
359:       yield self if block_given?
360:     end
post(path, opts={}, &bk)
     # File lib/sinatra/base.rb, line 761
761:       def post(path, opts={}, &bk);   route 'POST',   path, opts, &bk end
production?()
     # File lib/sinatra/base.rb, line 843
843:       def production?;  environment == :production  end
prototype()

The prototype instance used to process requests.

     # File lib/sinatra/base.rb, line 878
878:       def prototype
879:         @prototype ||= new
880:       end
put(path, opts={}, &bk)
     # File lib/sinatra/base.rb, line 760
760:       def put(path, opts={}, &bk);    route 'PUT',    path, opts, &bk end
register(*extensions, &block)
     # File lib/sinatra/base.rb, line 833
833:       def register(*extensions, &block)
834:         extensions << Module.new(&block) if block_given?
835:         @extensions += extensions
836:         extensions.each do |extension|
837:           extend extension
838:           extension.registered(self) if extension.respond_to?(:registered)
839:         end
840:       end
reset!(base=superclass)
     # File lib/sinatra/base.rb, line 901
901:       def reset!(base=superclass)
902:         @routes     = base.dupe_routes
903:         @templates  = base.templates.dup
904:         @conditions = []
905:         @filters    = base.filters.dup
906:         @errors     = base.errors.dup
907:         @middleware = base.middleware.dup
908:         @prototype  = nil
909:         @extensions = []
910:       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 860
860:       def run!(options={})
861:         set options
862:         handler      = detect_rack_handler
863:         handler_name = handler.name.gsub(/.*::/, '')
864:         puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
865:           "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
866:         handler.run self, :Host => host, :Port => port do |server|
867:           trap(:INT) do
868:             ## Use thins' hard #stop! if available, otherwise just #stop
869:             server.respond_to?(:stop!) ? server.stop! : server.stop
870:             puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
871:           end
872:         end
873:       rescue Errno::EADDRINUSE => e
874:         puts "== Someone is already performing on port #{port}!"
875:       end
test?()
     # File lib/sinatra/base.rb, line 844
844:       def test?;        environment == :test        end
use(middleware, *args, &block)

Use the specified Rack middleware

     # File lib/sinatra/base.rb, line 853
853:       def use(middleware, *args, &block)
854:         @prototype = nil
855:         @middleware << [middleware, args, block]
856:       end
Class Protected methods
dupe_routes()
     # File lib/sinatra/base.rb, line 913
913:       def dupe_routes
914:         routes.inject({}) do |hash,(request_method,routes)|
915:           hash[request_method] = routes.dup
916:           hash
917:         end
918:       end
Instance Public methods
call(env)

Rack call interface.

     # File lib/sinatra/base.rb, line 363
363:     def call(env)
364:       dup.call!(env)
365:     end
call!(env)
     # File lib/sinatra/base.rb, line 369
369:     def call!(env)
370:       @env      = env
371:       @request  = Request.new(env)
372:       @response = Response.new
373:       @params   = nil
374: 
375:       invoke { dispatch! }
376:       invoke { error_block!(response.status) }
377: 
378:       status, header, body = @response.finish
379: 
380:       # Never produce a body on HEAD requests. Do retain the Content-Length
381:       # unless it's "0", in which case we assume it was calculated erroneously
382:       # for a manual HEAD response and remove it entirely.
383:       if @env['REQUEST_METHOD'] == 'HEAD'
384:         body = []
385:         header.delete('Content-Length') if header['Content-Length'] == '0'
386:       end
387: 
388:       [status, header, body]
389:     end
forward()

Forward the request to the downstream app — middleware only.

     # File lib/sinatra/base.rb, line 411
411:     def forward
412:       fail "downstream app not set" unless @app.respond_to? :call
413:       status, headers, body = @app.call(@request.env)
414:       @response.status = status
415:       @response.body = body
416:       @response.headers.merge! headers
417:       nil
418:     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 398
398:     def halt(*response)
399:       response = response.first if response.length == 1
400:       throw :halt, response
401:     end
options()

Access options defined with Base.set.

     # File lib/sinatra/base.rb, line 392
392:     def options
393:       self.class
394:     end
pass()

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 406
406:     def pass
407:       throw :pass
408:     end