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

Base class for all Sinatra applications and middleware.

Methods

build   call   call   caller_files   caller_locations   configure   delete   development?   forward   get   halt   head   helpers   new   new   options   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 /bundler(\/runtime)?\.rb/, # bundler require hacks /<internal:/

External Aliases

user_agent -> agent
new -> new!
  Create a new instance without middleware in front of it.
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

Creates a Rack::Builder instance with all the middleware set up and an instance of this class as end point.

[Source]

      # File lib/sinatra/base.rb, line 1270
1270:       def build(*args, &bk)
1271:         builder = Rack::Builder.new
1272:         builder.use Rack::MethodOverride if method_override?
1273:         builder.use ShowExceptions       if show_exceptions?
1274:         builder.use Rack::CommonLogger   if logging?
1275:         setup_sessions builder
1276:         middleware.each { |c,a,b| builder.use(c, *a, &b) }
1277:         builder.run new!(*args, &bk)
1278:         builder
1279:       end

[Source]

      # File lib/sinatra/base.rb, line 1281
1281:       def call(env)
1282:         synchronize { prototype.call(env) }
1283:       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 1338
1338:       def caller_files
1339:         caller_locations.
1340:           map { |file,line| file }
1341:       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 1345
1345:       def caller_locations
1346:         caller(1).
1347:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
1348:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1349:       end

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

[Source]

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

[Source]

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

[Source]

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

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

[Source]

      # File lib/sinatra/base.rb, line 1120
1120:       def get(path, opts={}, &block)
1121:         conditions = @conditions.dup
1122:         route('GET', path, opts, &block)
1123: 
1124:         @conditions = conditions
1125:         route('HEAD', path, opts, &block)
1126:       end

[Source]

      # File lib/sinatra/base.rb, line 1131
1131:       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 1199
1199:       def helpers(*extensions, &block)
1200:         class_eval(&block)  if block_given?
1201:         include(*extensions) if extensions.any?
1202:       end

[Source]

     # File lib/sinatra/base.rb, line 623
623:     def initialize(app=nil)
624:       @app = app
625:       @template_cache = Tilt::Cache.new
626:       yield self if block_given?
627:     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 1264
1264:       def new(*args, &bk)
1265:         build(*args, &bk).to_app
1266:       end

[Source]

      # File lib/sinatra/base.rb, line 1132
1132:       def options(path, opts={}, &bk) route 'OPTIONS', path, opts, &bk end

[Source]

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

[Source]

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

The prototype instance used to process requests.

[Source]

      # File lib/sinatra/base.rb, line 1254
1254:       def prototype
1255:         @prototype ||= new
1256:       end

[Source]

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

[Source]

      # File lib/sinatra/base.rb, line 1231
1231:       def quit!(server, handler_name)
1232:         # Use Thin's hard #stop! if available, otherwise just #stop.
1233:         server.respond_to?(:stop!) ? server.stop! : server.stop
1234:         puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
1235:       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 1206
1206:       def register(*extensions, &block)
1207:         extensions << Module.new(&block) if block_given?
1208:         @extensions += extensions
1209:         extensions.each do |extension|
1210:           extend extension
1211:           extension.registered(self) if extension.respond_to?(:registered)
1212:         end
1213:       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 1239
1239:       def run!(options={})
1240:         set options
1241:         handler      = detect_rack_handler
1242:         handler_name = handler.name.gsub(/.*::/, '')
1243:         puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
1244:           "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
1245:         handler.run self, :Host => bind, :Port => port do |server|
1246:           [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
1247:           set :running, true
1248:         end
1249:       rescue Errno::EADDRINUSE => e
1250:         puts "== Someone is already performing on port #{port}!"
1251:       end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 670
670:     def self.settings
671:       self
672:     end

[Source]

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

Use the specified Rack middleware

[Source]

      # File lib/sinatra/base.rb, line 1226
1226:       def use(middleware, *args, &block)
1227:         @prototype = nil
1228:         @middleware << [middleware, args, block]
1229:       end

Public Instance methods

Rack call interface.

[Source]

     # File lib/sinatra/base.rb, line 630
630:     def call(env)
631:       dup.call!(env)
632:     end

Forward the request to the downstream app — middleware only.

[Source]

     # File lib/sinatra/base.rb, line 699
699:     def forward
700:       fail "downstream app not set" unless @app.respond_to? :call
701:       status, headers, body = @app.call(@request.env)
702:       @response.status = status
703:       @response.body = body
704:       @response.headers.merge! headers
705:       nil
706:     end

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

[Source]

     # File lib/sinatra/base.rb, line 686
686:     def halt(*response)
687:       response = response.first if response.length == 1
688:       throw :halt, response
689:     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 694
694:     def pass(&block)
695:       throw :pass, block
696:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 675
675:     def settings
676:       self.class.settings
677:     end

[Validate]