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 1259
1259:       def build(*args, &bk)
1260:         builder = Rack::Builder.new
1261:         builder.use Rack::MethodOverride if method_override?
1262:         builder.use ShowExceptions       if show_exceptions?
1263:         builder.use Rack::CommonLogger   if logging?
1264:         builder.use Rack::Head
1265:         setup_sessions builder
1266:         middleware.each { |c,a,b| builder.use(c, *a, &b) }
1267:         builder.run new!(*args, &bk)
1268:         builder
1269:       end

[Source]

      # File lib/sinatra/base.rb, line 1271
1271:       def call(env)
1272:         synchronize { prototype.call(env) }
1273:       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 1328
1328:       def caller_files
1329:         caller_locations.
1330:           map { |file,line| file }
1331:       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 1335
1335:       def caller_locations
1336:         caller(1).
1337:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
1338:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1339:       end

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

[Source]

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

[Source]

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

[Source]

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

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

[Source]

      # File lib/sinatra/base.rb, line 1109
1109:       def get(path, opts={}, &block)
1110:         conditions = @conditions.dup
1111:         route('GET', path, opts, &block)
1112: 
1113:         @conditions = conditions
1114:         route('HEAD', path, opts, &block)
1115:       end

[Source]

      # File lib/sinatra/base.rb, line 1120
1120:       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 1188
1188:       def helpers(*extensions, &block)
1189:         class_eval(&block)  if block_given?
1190:         include(*extensions) if extensions.any?
1191:       end

[Source]

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

[Source]

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

[Source]

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

[Source]

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

The prototype instance used to process requests.

[Source]

      # File lib/sinatra/base.rb, line 1243
1243:       def prototype
1244:         @prototype ||= new
1245:       end

[Source]

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

[Source]

      # File lib/sinatra/base.rb, line 1220
1220:       def quit!(server, handler_name)
1221:         # Use Thin's hard #stop! if available, otherwise just #stop.
1222:         server.respond_to?(:stop!) ? server.stop! : server.stop
1223:         puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
1224:       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 1195
1195:       def register(*extensions, &block)
1196:         extensions << Module.new(&block) if block_given?
1197:         @extensions += extensions
1198:         extensions.each do |extension|
1199:           extend extension
1200:           extension.registered(self) if extension.respond_to?(:registered)
1201:         end
1202:       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 1228
1228:       def run!(options={})
1229:         set options
1230:         handler      = detect_rack_handler
1231:         handler_name = handler.name.gsub(/.*::/, '')
1232:         puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
1233:           "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
1234:         handler.run self, :Host => bind, :Port => port do |server|
1235:           [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
1236:           set :running, true
1237:         end
1238:       rescue Errno::EADDRINUSE => e
1239:         puts "== Someone is already performing on port #{port}!"
1240:       end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 658
658:     def self.settings
659:       self
660:     end

[Source]

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

Use the specified Rack middleware

[Source]

      # File lib/sinatra/base.rb, line 1215
1215:       def use(middleware, *args, &block)
1216:         @prototype = nil
1217:         @middleware << [middleware, args, block]
1218:       end

Public Instance methods

Rack call interface.

[Source]

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

Forward the request to the downstream app — middleware only.

[Source]

     # File lib/sinatra/base.rb, line 687
687:     def forward
688:       fail "downstream app not set" unless @app.respond_to? :call
689:       status, headers, body = @app.call env
690:       @response.status = status
691:       @response.body = body
692:       @response.headers.merge! headers
693:       nil
694:     end

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

[Source]

     # File lib/sinatra/base.rb, line 674
674:     def halt(*response)
675:       response = response.first if response.length == 1
676:       throw :halt, response
677:     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 682
682:     def pass(&block)
683:       throw :pass, block
684:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 663
663:     def settings
664:       self.class.settings
665:     end

[Validate]