Parent

Included Modules

Sinatra::Base

Base class for all Sinatra applications and middleware.

Constants

CALLERS_TO_IGNORE

Attributes

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

Public Class Methods

call(env) click to toggle source
# File lib/sinatra/base.rb, line 978
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 1028
def caller_files
  caller_locations.
    map { |file,line| file }
end
caller_locations() click to toggle source
# File lib/sinatra/base.rb, line 1033
def caller_locations
  caller(1).
    map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
    reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
end
configure(*envs, &block) 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 928
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 846
def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk end
development?() click to toggle source
# File lib/sinatra/base.rb, line 922
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 836
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 847
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 908
def helpers(*extensions, &block)
  class_eval(&block)  if block_given?
  include(*extensions) if extensions.any?
end
new(app=nil) click to toggle source
# File lib/sinatra/base.rb, line 391
def initialize(app=nil)
  @app = app
  @template_cache = Tilt::Cache.new
  yield self if block_given?
end
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 966
def new(*args, &bk)
  builder = Rack::Builder.new
  builder.use Rack::Session::Cookie if sessions?
  builder.use Rack::CommonLogger    if logging?
  builder.use Rack::MethodOverride  if method_override?
  builder.use ShowExceptions        if show_exceptions?
  middleware.each { |c,a,b| builder.use(c, *a, &b) }

  builder.run super
  builder.to_app
end
post(path, opts={}, &bk) click to toggle source
# File lib/sinatra/base.rb, line 845
def post(path, opts={}, &bk);   route 'POST',   path, opts, &bk end
production?() click to toggle source
# File lib/sinatra/base.rb, line 923
def production?;  environment == :production  end
prototype() click to toggle source

The prototype instance used to process requests.

# File lib/sinatra/base.rb, line 959
def prototype
  @prototype ||= new
end
put(path, opts={}, &bk) click to toggle source
# File lib/sinatra/base.rb, line 844
def put(path, opts={}, &bk);    route 'PUT',    path, opts, &bk end
register(*extensions, &block) click to toggle source
# File lib/sinatra/base.rb, line 913
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={}) click to toggle source

Run the Sinatra app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)

# File lib/sinatra/base.rb, line 940
def run!(options={})
  set options
  handler      = detect_rack_handler
  handler_name = handler.name.gsub(/.*::/, '')
  puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
    "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/
  handler.run self, :Host => bind, :Port => port do |server|
    trap(:INT) do
      ## Use thins' hard #stop! if available, otherwise just #stop
      server.respond_to?(:stop!) ? server.stop! : server.stop
      puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/
    end
    set :running, true
  end
rescue Errno::EADDRINUSE => e
  puts "== Someone is already performing on port #{port}!"
end
test?() click to toggle source
# File lib/sinatra/base.rb, line 924
def test?;        environment == :test        end
use(middleware, *args, &block) click to toggle source

Use the specified Rack middleware

# File lib/sinatra/base.rb, line 933
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 398
def call(env)
  dup.call!(env)
end
call!(env) click to toggle source
# File lib/sinatra/base.rb, line 404
def call!(env)
  @env      = env
  @request  = Request.new(env)
  @response = Response.new
  @params   = indifferent_params(@request.params)
  @template_cache.clear if settings.reload_templates

  invoke { dispatch! }
  invoke { error_block!(response.status) }

  status, header, body = @response.finish

  # Never produce a body on HEAD requests. Do retain the Content-Length
  # unless it's "0", in which case we assume it was calculated erroneously
  # for a manual HEAD response and remove it entirely.
  if @env['REQUEST_METHOD'] == 'HEAD'
    body = []
    header.delete('Content-Length') if header['Content-Length'] == '0'
  end

  [status, header, body]
end
forward() click to toggle source

Forward the request to the downstream app – middleware only.

# File lib/sinatra/base.rb, line 448
def forward
  fail "downstream app not set" unless @app.respond_to? :call
  status, headers, body = @app.call(@request.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 435
def halt(*response)
  response = response.first if response.length == 1
  throw :halt, response
end
options() click to toggle source
Alias for: settings
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 443
def pass(&block)
  throw :pass, block
end
settings() click to toggle source

Access settings defined with Base.set.

# File lib/sinatra/base.rb, line 428
def settings
  self.class
end
Also aliased as: options

[Validate]

Generated with the Darkfish Rdoc Generator 2.