class Sinatra::Helpers::Stream::Base

Base class for all Sinatra applications and middleware.

Attributes

app[RW]
env[RW]
params[RW]
request[RW]
response[RW]
template_cache[R]

Public Class Methods

build(builder, *args, &bk) click to toggle source

Creates a Rack::Builder instance with all the middleware set up and an instance of this class as end point.

# File lib/sinatra/base.rb, line 1326
def build(builder, *args, &bk)
  setup_default_middleware builder
  setup_middleware builder
  builder.run new!(*args, &bk)
  builder
end
call(env) click to toggle source
# File lib/sinatra/base.rb, line 1333
def call(env)
  synchronize { prototype.call(env) }
end
caller_files() click to toggle source

Like Kernel#caller but excluding certain magic entries and without line / method information; the resulting array contains filenames only.

# File lib/sinatra/base.rb, line 1437
def caller_files
  cleaned_caller(1).flatten
end
caller_locations() click to toggle source

Like ::caller_files, but containing Arrays rather than strings with the first element being the file, and the second being the line.

# File lib/sinatra/base.rb, line 1443
def caller_locations
  cleaned_caller 2
end
configure(*envs) { |self| ... } click to toggle source

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

# File lib/sinatra/base.rb, line 1272
def configure(*envs, &block)
  yield self if envs.empty? || envs.include?(environment.to_sym)
end
delete(path, opts={}, &bk) click to toggle source
# File lib/sinatra/base.rb, line 1176
def delete(path, opts={}, &bk)  route 'DELETE',  path, opts, &bk end
development?() click to toggle source
# File lib/sinatra/base.rb, line 1266
def development?; environment == :development end
get(path, opts={}, &block) click to toggle source

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

# File lib/sinatra/base.rb, line 1166
def get(path, opts={}, &block)
  conditions = @conditions.dup
  route('GET', path, opts, &block)

  @conditions = conditions
  route('HEAD', path, opts, &block)
end
head(path, opts={}, &bk) click to toggle source
# File lib/sinatra/base.rb, line 1177
def head(path, opts={}, &bk)    route 'HEAD',    path, opts, &bk end
helpers(*extensions, &block) click to toggle source

Makes the methods defined in the block and in the Modules given in `extensions` available to the handlers and templates

# File lib/sinatra/base.rb, line 1250
def helpers(*extensions, &block)
  class_eval(&block)   if block_given?
  include(*extensions) if extensions.any?
end
new(app=nil) { |self| ... } click to toggle source
# File lib/sinatra/base.rb, line 696
def initialize(app=nil)
  super()
  @app = app
  @template_cache = Tilt::Cache.new
  yield self if block_given?
end
Also aliased as: new!
new(*args, &bk) click to toggle source

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.

# File lib/sinatra/base.rb, line 1320
def new(*args, &bk)
  build(Rack::Builder.new, *args, &bk).to_app
end
new!(app=nil) click to toggle source

Create a new instance without middleware in front of it.

Alias for: new
options(path, opts={}, &bk) click to toggle source
# File lib/sinatra/base.rb, line 1178
def options(path, opts={}, &bk) route 'OPTIONS', path, opts, &bk end
patch(path, opts={}, &bk) click to toggle source
# File lib/sinatra/base.rb, line 1179
def patch(path, opts={}, &bk)   route 'PATCH',   path, opts, &bk end
post(path, opts={}, &bk) click to toggle source
# File lib/sinatra/base.rb, line 1175
def post(path, opts={}, &bk)    route 'POST',    path, opts, &bk end
production?() click to toggle source
# File lib/sinatra/base.rb, line 1267
def production?;  environment == :production  end
prototype() click to toggle source

The prototype instance used to process requests.

# File lib/sinatra/base.rb, line 1310
def prototype
  @prototype ||= new
end
put(path, opts={}, &bk) click to toggle source
# File lib/sinatra/base.rb, line 1174
def put(path, opts={}, &bk)     route 'PUT',     path, opts, &bk end
quit!(server, handler_name) click to toggle source
# File lib/sinatra/base.rb, line 1282
def quit!(server, handler_name)
  # Use Thin's hard #stop! if available, otherwise just #stop.
  server.respond_to?(:stop!) ? server.stop! : server.stop
  $stderr.puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~%rcgi/
end
register(*extensions, &block) click to toggle source

Register an extension. Alternatively take a block from which an extension will be created and registered on the fly.

# File lib/sinatra/base.rb, line 1257
def register(*extensions, &block)
  extensions << Module.new(&block) if block_given?
  @extensions += extensions
  extensions.each do |extension|
    extend extension
    extension.registered(self) if extension.respond_to?(:registered)
  end
end
run!(options={}) { |server| ... } click to toggle source

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.

# File lib/sinatra/base.rb, line 1291
def run!(options={})
  set options
  handler      = detect_rack_handler
  handler_name = handler.name.gsub(%r.*::/, '')
  handler.run self, :Host => bind, :Port => port do |server|
    unless handler_name =~ %rcgi/
      $stderr.puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
      "on #{port} for #{environment} with backup from #{handler_name}"
    end
    [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
    server.threaded = settings.threaded if server.respond_to? :threaded=
    set :running, true
    yield server if block_given?
  end
rescue Errno::EADDRINUSE => e
  $stderr.puts "== Someone is already performing on port #{port}!"
end
settings() click to toggle source

Access settings defined with Base.set.

# File lib/sinatra/base.rb, line 734
def self.settings
  self
end
test?() click to toggle source
# File lib/sinatra/base.rb, line 1268
def test?;        environment == :test        end
use(middleware, *args, &block) click to toggle source

Use the specified Rack middleware

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

Public Instance Methods

call(env) click to toggle source

Rack call interface.

# File lib/sinatra/base.rb, line 704
def call(env)
  dup.call!(env)
end
forward() click to toggle source

Forward the request to the downstream app -- middleware only.

# File lib/sinatra/base.rb, line 764
def forward
  fail "downstream app not set" unless @app.respond_to? :call
  status, headers, body = @app.call env
  @response.status = status
  @response.body = body
  @response.headers.merge! headers
  nil
end
halt(*response) click to toggle source

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

# File lib/sinatra/base.rb, line 751
def halt(*response)
  response = response.first if response.length == 1
  throw :halt, response
end
options() click to toggle source
# File lib/sinatra/base.rb, line 743
def options
  warn "Sinatra::Base#options is deprecated and will be removed, "          "use #settings instead."
  settings
end
pass(&block) click to toggle source

Pass control to the next matching route. If there are no more matching routes, Sinatra will return a 404 response.

# File lib/sinatra/base.rb, line 759
def pass(&block)
  throw :pass, block
end
settings() click to toggle source

Access settings defined with Base.set.

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