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 1172
1172:       def call(env)
1173:         synchronize { prototype.call(env) }
1174:       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 1224
1224:       def caller_files
1225:         caller_locations.
1226:           map { |file,line| file }
1227:       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 1231
1231:       def caller_locations
1232:         caller(1).
1233:           map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
1234:           reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1235:       end

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

[Source]

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

[Source]

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

[Source]

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

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

[Source]

      # File lib/sinatra/base.rb, line 1023
1023:       def get(path, opts={}, &block)
1024:         conditions = @conditions.dup
1025:         route('GET', path, opts, &block)
1026: 
1027:         @conditions = conditions
1028:         route('HEAD', path, opts, &block)
1029:       end

[Source]

      # File lib/sinatra/base.rb, line 1034
1034:       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 1098
1098:       def helpers(*extensions, &block)
1099:         class_eval(&block)  if block_given?
1100:         include(*extensions) if extensions.any?
1101:       end

[Source]

     # File lib/sinatra/base.rb, line 536
536:     def initialize(app=nil)
537:       @app = app
538:       @template_cache = Tilt::Cache.new
539:       yield self if block_given?
540:     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 1160
1160:       def new(*args, &bk)
1161:         builder = Rack::Builder.new
1162:         builder.use Rack::Session::Cookie if sessions?
1163:         builder.use Rack::CommonLogger    if logging?
1164:         builder.use Rack::MethodOverride  if method_override?
1165:         builder.use ShowExceptions        if show_exceptions?
1166:         middleware.each { |c,a,b| builder.use(c, *a, &b) }
1167: 
1168:         builder.run super
1169:         builder.to_app
1170:       end

[Source]

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

[Source]

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

The prototype instance used to process requests.

[Source]

      # File lib/sinatra/base.rb, line 1153
1153:       def prototype
1154:         @prototype ||= new
1155:       end

[Source]

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

[Source]

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

Access settings defined with Base.set.

[Source]

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

[Source]

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

Use the specified Rack middleware

[Source]

      # File lib/sinatra/base.rb, line 1125
1125:       def use(middleware, *args, &block)
1126:         @prototype = nil
1127:         @middleware << [middleware, args, block]
1128:       end

Public Instance methods

Rack call interface.

[Source]

     # File lib/sinatra/base.rb, line 543
543:     def call(env)
544:       dup.call!(env)
545:     end

Forward the request to the downstream app — middleware only.

[Source]

     # File lib/sinatra/base.rb, line 612
612:     def forward
613:       fail "downstream app not set" unless @app.respond_to? :call
614:       status, headers, body = @app.call(@request.env)
615:       @response.status = status
616:       @response.body = body
617:       @response.headers.merge! headers
618:       nil
619:     end

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

[Source]

     # File lib/sinatra/base.rb, line 599
599:     def halt(*response)
600:       response = response.first if response.length == 1
601:       throw :halt, response
602:     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 607
607:     def pass(&block)
608:       throw :pass, block
609:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 588
588:     def settings
589:       self.class.settings
590:     end

[Validate]