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 /bundler(\/runtime)?\.rb/, # bundler require hacks /<internal:/

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 1166
1166:       def call(env)
1167:         synchronize { prototype.call(env) }
1168:       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 1218
1218:       def caller_files
1219:         caller_locations.
1220:           map { |file,line| file }
1221:       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 1225
1225:       def caller_locations
1226:         caller(1).
1227:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
1228:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1229:       end

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

[Source]

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

[Source]

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

[Source]

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

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

[Source]

      # File lib/sinatra/base.rb, line 1017
1017:       def get(path, opts={}, &block)
1018:         conditions = @conditions.dup
1019:         route('GET', path, opts, &block)
1020: 
1021:         @conditions = conditions
1022:         route('HEAD', path, opts, &block)
1023:       end

[Source]

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

[Source]

     # File lib/sinatra/base.rb, line 530
530:     def initialize(app=nil)
531:       @app = app
532:       @template_cache = Tilt::Cache.new
533:       yield self if block_given?
534:     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 1154
1154:       def new(*args, &bk)
1155:         builder = Rack::Builder.new
1156:         builder.use Rack::Session::Cookie if sessions?
1157:         builder.use Rack::CommonLogger    if logging?
1158:         builder.use Rack::MethodOverride  if method_override?
1159:         builder.use ShowExceptions        if show_exceptions?
1160:         middleware.each { |c,a,b| builder.use(c, *a, &b) }
1161: 
1162:         builder.run super
1163:         builder.to_app
1164:       end

[Source]

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

[Source]

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

The prototype instance used to process requests.

[Source]

      # File lib/sinatra/base.rb, line 1147
1147:       def prototype
1148:         @prototype ||= new
1149:       end

[Source]

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

[Source]

      # File lib/sinatra/base.rb, line 1124
1124:       def quit!(server, handler_name)
1125:         ## Use thins' hard #stop! if available, otherwise just #stop
1126:         server.respond_to?(:stop!) ? server.stop! : server.stop
1127:         puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
1128:       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 1099
1099:       def register(*extensions, &block)
1100:         extensions << Module.new(&block) if block_given?
1101:         @extensions += extensions
1102:         extensions.each do |extension|
1103:           extend extension
1104:           extension.registered(self) if extension.respond_to?(:registered)
1105:         end
1106:       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 1132
1132:       def run!(options={})
1133:         set options
1134:         handler      = detect_rack_handler
1135:         handler_name = handler.name.gsub(/.*::/, '')
1136:         puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
1137:           "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
1138:         handler.run self, :Host => bind, :Port => port do |server|
1139:           [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
1140:           set :running, true
1141:         end
1142:       rescue Errno::EADDRINUSE => e
1143:         puts "== Someone is already performing on port #{port}!"
1144:       end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 577
577:     def self.settings
578:       self
579:     end

[Source]

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

Use the specified Rack middleware

[Source]

      # File lib/sinatra/base.rb, line 1119
1119:       def use(middleware, *args, &block)
1120:         @prototype = nil
1121:         @middleware << [middleware, args, block]
1122:       end

Public Instance methods

Rack call interface.

[Source]

     # File lib/sinatra/base.rb, line 537
537:     def call(env)
538:       dup.call!(env)
539:     end

Forward the request to the downstream app — middleware only.

[Source]

     # File lib/sinatra/base.rb, line 606
606:     def forward
607:       fail "downstream app not set" unless @app.respond_to? :call
608:       status, headers, body = @app.call(@request.env)
609:       @response.status = status
610:       @response.body = body
611:       @response.headers.merge! headers
612:       nil
613:     end

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

[Source]

     # File lib/sinatra/base.rb, line 593
593:     def halt(*response)
594:       response = response.first if response.length == 1
595:       throw :halt, response
596:     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 601
601:     def pass(&block)
602:       throw :pass, block
603:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 582
582:     def settings
583:       self.class.settings
584:     end

[Validate]