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 1233
1233:       def build(*args, &bk)
1234:         builder = Rack::Builder.new
1235:         setup_sessions builder
1236:         builder.use Rack::CommonLogger    if logging?
1237:         builder.use Rack::MethodOverride  if method_override?
1238:         builder.use ShowExceptions        if show_exceptions?
1239:         middleware.each { |c,a,b| builder.use(c, *a, &b) }
1240:         builder.run new!(*args, &bk)
1241:         builder
1242:       end

[Source]

      # File lib/sinatra/base.rb, line 1244
1244:       def call(env)
1245:         synchronize { prototype.call(env) }
1246:       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 1301
1301:       def caller_files
1302:         caller_locations.
1303:           map { |file,line| file }
1304:       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 1308
1308:       def caller_locations
1309:         caller(1).
1310:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
1311:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1312:       end

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

[Source]

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

[Source]

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

[Source]

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

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

[Source]

      # File lib/sinatra/base.rb, line 1084
1084:       def get(path, opts={}, &block)
1085:         conditions = @conditions.dup
1086:         route('GET', path, opts, &block)
1087: 
1088:         @conditions = conditions
1089:         route('HEAD', path, opts, &block)
1090:       end

[Source]

      # File lib/sinatra/base.rb, line 1095
1095:       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 1162
1162:       def helpers(*extensions, &block)
1163:         class_eval(&block)  if block_given?
1164:         include(*extensions) if extensions.any?
1165:       end

[Source]

     # File lib/sinatra/base.rb, line 596
596:     def initialize(app=nil)
597:       @app = app
598:       @template_cache = Tilt::Cache.new
599:       yield self if block_given?
600:     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 1227
1227:       def new(*args, &bk)
1228:         build(*args, &bk).to_app
1229:       end

[Source]

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

[Source]

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

[Source]

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

The prototype instance used to process requests.

[Source]

      # File lib/sinatra/base.rb, line 1217
1217:       def prototype
1218:         @prototype ||= new
1219:       end

[Source]

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

[Source]

      # File lib/sinatra/base.rb, line 1194
1194:       def quit!(server, handler_name)
1195:         # Use Thin's hard #stop! if available, otherwise just #stop.
1196:         server.respond_to?(:stop!) ? server.stop! : server.stop
1197:         puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
1198:       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 1169
1169:       def register(*extensions, &block)
1170:         extensions << Module.new(&block) if block_given?
1171:         @extensions += extensions
1172:         extensions.each do |extension|
1173:           extend extension
1174:           extension.registered(self) if extension.respond_to?(:registered)
1175:         end
1176:       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 1202
1202:       def run!(options={})
1203:         set options
1204:         handler      = detect_rack_handler
1205:         handler_name = handler.name.gsub(/.*::/, '')
1206:         puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
1207:           "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
1208:         handler.run self, :Host => bind, :Port => port do |server|
1209:           [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
1210:           set :running, true
1211:         end
1212:       rescue Errno::EADDRINUSE => e
1213:         puts "== Someone is already performing on port #{port}!"
1214:       end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 643
643:     def self.settings
644:       self
645:     end

[Source]

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

Use the specified Rack middleware

[Source]

      # File lib/sinatra/base.rb, line 1189
1189:       def use(middleware, *args, &block)
1190:         @prototype = nil
1191:         @middleware << [middleware, args, block]
1192:       end

Public Instance methods

Rack call interface.

[Source]

     # File lib/sinatra/base.rb, line 603
603:     def call(env)
604:       dup.call!(env)
605:     end

Forward the request to the downstream app — middleware only.

[Source]

     # File lib/sinatra/base.rb, line 672
672:     def forward
673:       fail "downstream app not set" unless @app.respond_to? :call
674:       status, headers, body = @app.call(@request.env)
675:       @response.status = status
676:       @response.body = body
677:       @response.headers.merge! headers
678:       nil
679:     end

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

[Source]

     # File lib/sinatra/base.rb, line 659
659:     def halt(*response)
660:       response = response.first if response.length == 1
661:       throw :halt, response
662:     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 667
667:     def pass(&block)
668:       throw :pass, block
669:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 648
648:     def settings
649:       self.class.settings
650:     end

[Validate]