Class Sinatra::Base
In: lib/sinatra/base.rb
Parent: Object

Base class for all Sinatra applications and middleware.

Methods

call   call   caller_files   caller_locations   configure   delete   development?   forward   get   halt   head   helpers   new   new   options   options   pass   post   production?   prototype   put   quit!   register   run!   settings   settings   test?   use  

Included Modules

Rack::Utils Helpers Templates

Constants

CALLERS_TO_IGNORE = [ # :nodoc: /\/sinatra(\/(base|main|showexceptions))?\.rb$/, # all sinatra code /lib\/tilt.*\.rb$/, # all tilt code /\(.*\)/, # generated code /rubygems\/custom_require\.rb$/, # rubygems require hacks /active_support/, # active_support require hacks /<internal:/, # internal in ruby >= 1.9.2 ]

External Aliases

user_agent -> agent
method_override? -> methodoverride?
method_override= -> methodoverride=

Attributes

app  [RW] 
env  [RW] 
errors  [R] 
filters  [R] 
params  [RW] 
request  [RW] 
response  [RW] 
routes  [R] 
template_cache  [R] 
templates  [R] 

Public Class methods

[Source]

      # File lib/sinatra/base.rb, line 1141
1141:       def call(env)
1142:         synchronize { prototype.call(env) }
1143:       end

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

[Source]

      # File lib/sinatra/base.rb, line 1192
1192:       def caller_files
1193:         caller_locations.
1194:           map { |file,line| file }
1195:       end

Like caller_files, but containing Arrays rather than strings with the first element being the file, and the second being the line.

[Source]

      # File lib/sinatra/base.rb, line 1199
1199:       def caller_locations
1200:         caller(1).
1201:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
1202:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1203:       end

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

[Source]

      # File lib/sinatra/base.rb, line 1089
1089:       def configure(*envs, &block)
1090:         yield self if envs.empty? || envs.include?(environment.to_sym)
1091:       end

[Source]

      # File lib/sinatra/base.rb, line 1002
1002:       def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk end

[Source]

      # File lib/sinatra/base.rb, line 1083
1083:       def development?; environment == :development end

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

[Source]

     # File lib/sinatra/base.rb, line 992
992:       def get(path, opts={}, &block)
993:         conditions = @conditions.dup
994:         route('GET', path, opts, &block)
995: 
996:         @conditions = conditions
997:         route('HEAD', path, opts, &block)
998:       end

[Source]

      # File lib/sinatra/base.rb, line 1003
1003:       def head(path, opts={}, &bk);   route 'HEAD',   path, opts, &bk end

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

[Source]

      # File lib/sinatra/base.rb, line 1067
1067:       def helpers(*extensions, &block)
1068:         class_eval(&block)  if block_given?
1069:         include(*extensions) if extensions.any?
1070:       end

[Source]

     # File lib/sinatra/base.rb, line 508
508:     def initialize(app=nil)
509:       @app = app
510:       @template_cache = Tilt::Cache.new
511:       yield self if block_given?
512:     end

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.

[Source]

      # File lib/sinatra/base.rb, line 1129
1129:       def new(*args, &bk)
1130:         builder = Rack::Builder.new
1131:         builder.use Rack::Session::Cookie if sessions?
1132:         builder.use Rack::CommonLogger    if logging?
1133:         builder.use Rack::MethodOverride  if method_override?
1134:         builder.use ShowExceptions        if show_exceptions?
1135:         middleware.each { |c,a,b| builder.use(c, *a, &b) }
1136: 
1137:         builder.run super
1138:         builder.to_app
1139:       end

[Source]

      # File lib/sinatra/base.rb, line 1001
1001:       def post(path, opts={}, &bk);   route 'POST',   path, opts, &bk end

[Source]

      # File lib/sinatra/base.rb, line 1084
1084:       def production?;  environment == :production  end

The prototype instance used to process requests.

[Source]

      # File lib/sinatra/base.rb, line 1122
1122:       def prototype
1123:         @prototype ||= new
1124:       end

[Source]

      # File lib/sinatra/base.rb, line 1000
1000:       def put(path, opts={}, &bk);    route 'PUT',    path, opts, &bk end

[Source]

      # File lib/sinatra/base.rb, line 1099
1099:       def quit!(server, handler_name)
1100:         ## Use thins' hard #stop! if available, otherwise just #stop
1101:         server.respond_to?(:stop!) ? server.stop! : server.stop
1102:         puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
1103:       end

Register an extension. Alternatively take a block from which an extension will be created and registered on the fly.

[Source]

      # File lib/sinatra/base.rb, line 1074
1074:       def register(*extensions, &block)
1075:         extensions << Module.new(&block) if block_given?
1076:         @extensions += extensions
1077:         extensions.each do |extension|
1078:           extend extension
1079:           extension.registered(self) if extension.respond_to?(:registered)
1080:         end
1081:       end

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

[Source]

      # File lib/sinatra/base.rb, line 1107
1107:       def run!(options={})
1108:         set options
1109:         handler      = detect_rack_handler
1110:         handler_name = handler.name.gsub(/.*::/, '')
1111:         puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
1112:           "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
1113:         handler.run self, :Host => bind, :Port => port do |server|
1114:           [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
1115:           set :running, true
1116:         end
1117:       rescue Errno::EADDRINUSE => e
1118:         puts "== Someone is already performing on port #{port}!"
1119:       end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 554
554:     def self.settings
555:       self
556:     end

[Source]

      # File lib/sinatra/base.rb, line 1085
1085:       def test?;        environment == :test        end

Use the specified Rack middleware

[Source]

      # File lib/sinatra/base.rb, line 1094
1094:       def use(middleware, *args, &block)
1095:         @prototype = nil
1096:         @middleware << [middleware, args, block]
1097:       end

Public Instance methods

Rack call interface.

[Source]

     # File lib/sinatra/base.rb, line 515
515:     def call(env)
516:       dup.call!(env)
517:     end

Forward the request to the downstream app — middleware only.

[Source]

     # File lib/sinatra/base.rb, line 583
583:     def forward
584:       fail "downstream app not set" unless @app.respond_to? :call
585:       status, headers, body = @app.call(@request.env)
586:       @response.status = status
587:       @response.body = body
588:       @response.headers.merge! headers
589:       nil
590:     end

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

[Source]

     # File lib/sinatra/base.rb, line 570
570:     def halt(*response)
571:       response = response.first if response.length == 1
572:       throw :halt, response
573:     end
options()

Alias for settings

options()

Alias for settings

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

[Source]

     # File lib/sinatra/base.rb, line 578
578:     def pass(&block)
579:       throw :pass, block
580:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 559
559:     def settings
560:       self.class.settings
561:     end

[Validate]