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 1282
1282:       def build(*args, &bk)
1283:         builder = Rack::Builder.new
1284:         builder.use Rack::MethodOverride if method_override?
1285:         builder.use ShowExceptions       if show_exceptions?
1286:         builder.use Rack::CommonLogger   if logging?
1287:         builder.use Rack::Head
1288:         setup_sessions builder
1289:         middleware.each { |c,a,b| builder.use(c, *a, &b) }
1290:         builder.run new!(*args, &bk)
1291:         builder
1292:       end

[Source]

      # File lib/sinatra/base.rb, line 1294
1294:       def call(env)
1295:         synchronize { prototype.call(env) }
1296:       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 1351
1351:       def caller_files
1352:         caller_locations.
1353:           map { |file,line| file }
1354:       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 1358
1358:       def caller_locations
1359:         caller(1).
1360:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
1361:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1362:       end

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

[Source]

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

[Source]

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

[Source]

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

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

[Source]

      # File lib/sinatra/base.rb, line 1132
1132:       def get(path, opts={}, &block)
1133:         conditions = @conditions.dup
1134:         route('GET', path, opts, &block)
1135: 
1136:         @conditions = conditions
1137:         route('HEAD', path, opts, &block)
1138:       end

[Source]

      # File lib/sinatra/base.rb, line 1143
1143:       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 1211
1211:       def helpers(*extensions, &block)
1212:         class_eval(&block)   if block_given?
1213:         include(*extensions) if extensions.any?
1214:       end

[Source]

     # File lib/sinatra/base.rb, line 635
635:     def initialize(app=nil)
636:       super()
637:       @app = app
638:       @template_cache = Tilt::Cache.new
639:       yield self if block_given?
640:     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 1276
1276:       def new(*args, &bk)
1277:         build(*args, &bk).to_app
1278:       end

[Source]

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

[Source]

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

[Source]

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

The prototype instance used to process requests.

[Source]

      # File lib/sinatra/base.rb, line 1266
1266:       def prototype
1267:         @prototype ||= new
1268:       end

[Source]

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

[Source]

      # File lib/sinatra/base.rb, line 1243
1243:       def quit!(server, handler_name)
1244:         # Use Thin's hard #stop! if available, otherwise just #stop.
1245:         server.respond_to?(:stop!) ? server.stop! : server.stop
1246:         $stderr.puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
1247:       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 1218
1218:       def register(*extensions, &block)
1219:         extensions << Module.new(&block) if block_given?
1220:         @extensions += extensions
1221:         extensions.each do |extension|
1222:           extend extension
1223:           extension.registered(self) if extension.respond_to?(:registered)
1224:         end
1225:       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 1251
1251:       def run!(options={})
1252:         set options
1253:         handler      = detect_rack_handler
1254:         handler_name = handler.name.gsub(/.*::/, '')
1255:         $stderr.puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
1256:           "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
1257:         handler.run self, :Host => bind, :Port => port do |server|
1258:           [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
1259:           set :running, true
1260:         end
1261:       rescue Errno::EADDRINUSE => e
1262:         $stderr.puts "== Someone is already performing on port #{port}!"
1263:       end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 673
673:     def self.settings
674:       self
675:     end

[Source]

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

Use the specified Rack middleware

[Source]

      # File lib/sinatra/base.rb, line 1238
1238:       def use(middleware, *args, &block)
1239:         @prototype = nil
1240:         @middleware << [middleware, args, block]
1241:       end

Public Instance methods

Rack call interface.

[Source]

     # File lib/sinatra/base.rb, line 643
643:     def call(env)
644:       dup.call!(env)
645:     end

Forward the request to the downstream app — middleware only.

[Source]

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

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

[Source]

     # File lib/sinatra/base.rb, line 689
689:     def halt(*response)
690:       response = response.first if response.length == 1
691:       throw :halt, response
692:     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 697
697:     def pass(&block)
698:       throw :pass, block
699:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 678
678:     def settings
679:       self.class.settings
680:     end

[Validate]