Class Sinatra::Helpers::Stream::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   pass   patch   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:/, # internal in ruby >= 1.9.2 /src\/kernel\/bootstrap\/[A-Z]/

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 1326
1326:       def build(builder, *args, &bk)
1327:         setup_default_middleware builder
1328:         setup_middleware builder
1329:         builder.run new!(*args, &bk)
1330:         builder
1331:       end

[Source]

      # File lib/sinatra/base.rb, line 1333
1333:       def call(env)
1334:         synchronize { prototype.call(env) }
1335:       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 1437
1437:       def caller_files
1438:         cleaned_caller(1).flatten
1439:       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 1443
1443:       def caller_locations
1444:         cleaned_caller 2
1445:       end

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

[Source]

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

[Source]

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

[Source]

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

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

[Source]

      # File lib/sinatra/base.rb, line 1166
1166:       def get(path, opts={}, &block)
1167:         conditions = @conditions.dup
1168:         route('GET', path, opts, &block)
1169: 
1170:         @conditions = conditions
1171:         route('HEAD', path, opts, &block)
1172:       end

[Source]

      # File lib/sinatra/base.rb, line 1177
1177:       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 1250
1250:       def helpers(*extensions, &block)
1251:         class_eval(&block)   if block_given?
1252:         include(*extensions) if extensions.any?
1253:       end

[Source]

     # File lib/sinatra/base.rb, line 696
696:     def initialize(app=nil)
697:       super()
698:       @app = app
699:       @template_cache = Tilt::Cache.new
700:       yield self if block_given?
701:     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 1320
1320:       def new(*args, &bk)
1321:         build(Rack::Builder.new, *args, &bk).to_app
1322:       end

[Source]

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

[Source]

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

[Source]

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

[Source]

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

The prototype instance used to process requests.

[Source]

      # File lib/sinatra/base.rb, line 1310
1310:       def prototype
1311:         @prototype ||= new
1312:       end

[Source]

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

[Source]

      # File lib/sinatra/base.rb, line 1282
1282:       def quit!(server, handler_name)
1283:         # Use Thin's hard #stop! if available, otherwise just #stop.
1284:         server.respond_to?(:stop!) ? server.stop! : server.stop
1285:         $stderr.puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
1286:       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 1257
1257:       def register(*extensions, &block)
1258:         extensions << Module.new(&block) if block_given?
1259:         @extensions += extensions
1260:         extensions.each do |extension|
1261:           extend extension
1262:           extension.registered(self) if extension.respond_to?(:registered)
1263:         end
1264:       end

Run the Sinatra app as a self-hosted server using Thin, Mongrel or WEBrick (in that order). If given a block, will call with the constructed handler once we have taken the stage.

[Source]

      # File lib/sinatra/base.rb, line 1291
1291:       def run!(options={})
1292:         set options
1293:         handler      = detect_rack_handler
1294:         handler_name = handler.name.gsub(/.*::/, '')
1295:         handler.run self, :Host => bind, :Port => port do |server|
1296:           unless handler_name =~ /cgi/i
1297:             $stderr.puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
1298:             "on #{port} for #{environment} with backup from #{handler_name}"
1299:           end
1300:           [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
1301:           server.threaded = settings.threaded if server.respond_to? :threaded=
1302:           set :running, true
1303:           yield server if block_given?
1304:         end
1305:       rescue Errno::EADDRINUSE => e
1306:         $stderr.puts "== Someone is already performing on port #{port}!"
1307:       end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 734
734:     def self.settings
735:       self
736:     end

[Source]

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

Use the specified Rack middleware

[Source]

      # File lib/sinatra/base.rb, line 1277
1277:       def use(middleware, *args, &block)
1278:         @prototype = nil
1279:         @middleware << [middleware, args, block]
1280:       end

Public Instance methods

Rack call interface.

[Source]

     # File lib/sinatra/base.rb, line 704
704:     def call(env)
705:       dup.call!(env)
706:     end

Forward the request to the downstream app — middleware only.

[Source]

     # File lib/sinatra/base.rb, line 764
764:     def forward
765:       fail "downstream app not set" unless @app.respond_to? :call
766:       status, headers, body = @app.call env
767:       @response.status = status
768:       @response.body = body
769:       @response.headers.merge! headers
770:       nil
771:     end

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

[Source]

     # File lib/sinatra/base.rb, line 751
751:     def halt(*response)
752:       response = response.first if response.length == 1
753:       throw :halt, response
754:     end

[Source]

     # File lib/sinatra/base.rb, line 743
743:     def options
744:       warn "Sinatra::Base#options is deprecated and will be removed, " \
745:         "use #settings instead."
746:       settings
747:     end

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 759
759:     def pass(&block)
760:       throw :pass, block
761:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 739
739:     def settings
740:       self.class.settings
741:     end

[Validate]