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 1424
1424:       def caller_files
1425:         cleaned_caller(1).flatten
1426:       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 1430
1430:       def caller_locations
1431:         cleaned_caller 2
1432:       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 683
683:     def initialize(app=nil)
684:       super()
685:       @app = app
686:       @template_cache = Tilt::Cache.new
687:       yield self if block_given?
688:     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 721
721:     def self.settings
722:       self
723:     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 691
691:     def call(env)
692:       dup.call!(env)
693:     end

Forward the request to the downstream app — middleware only.

[Source]

     # File lib/sinatra/base.rb, line 751
751:     def forward
752:       fail "downstream app not set" unless @app.respond_to? :call
753:       status, headers, body = @app.call env
754:       @response.status = status
755:       @response.body = body
756:       @response.headers.merge! headers
757:       nil
758:     end

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

[Source]

     # File lib/sinatra/base.rb, line 738
738:     def halt(*response)
739:       response = response.first if response.length == 1
740:       throw :halt, response
741:     end

[Source]

     # File lib/sinatra/base.rb, line 730
730:     def options
731:       warn "Sinatra::Base#options is deprecated and will be removed, " \
732:         "use #settings instead."
733:       settings
734:     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 746
746:     def pass(&block)
747:       throw :pass, block
748:     end

Access settings defined with Base.set.

[Source]

     # File lib/sinatra/base.rb, line 726
726:     def settings
727:       self.class.settings
728:     end

[Validate]