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 1272 def build(*args, &bk) builder = Rack::Builder.new builder.use Rack::MethodOverride if method_override? builder.use ShowExceptions if show_exceptions? builder.use Rack::CommonLogger if logging? setup_sessions builder middleware.each { |c,a,b| builder.use(c, *a, &b) } builder.run new!(*args, &bk) builder end
# File lib/sinatra/base.rb, line 1283 def call(env) synchronize { prototype.call(env) } end
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 1340 def caller_files caller_locations. map { |file,line| file } end
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 1347 def caller_locations caller(1). map { |line| line.split(/:(?=\d|in )/)[0,2] }. reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } } end
Set configuration options for Sinatra and/or the app. Allows scoping of settings for certain environments.
# File lib/sinatra/base.rb, line 1223 def configure(*envs, &block) yield self if envs.empty? || envs.include?(environment.to_sym) end
# File lib/sinatra/base.rb, line 1132 def delete(path, opts={}, &bk) route 'DELETE', path, opts, &bk end
# File lib/sinatra/base.rb, line 1217 def development?; environment == :development end
Defining a `GET` handler also automatically defines a `HEAD` handler.
# File lib/sinatra/base.rb, line 1122 def get(path, opts={}, &block) conditions = @conditions.dup route('GET', path, opts, &block) @conditions = conditions route('HEAD', path, opts, &block) end
# File lib/sinatra/base.rb, line 1133 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
# File lib/sinatra/base.rb, line 1201 def helpers(*extensions, &block) class_eval(&block) if block_given? include(*extensions) if extensions.any? 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.
# File lib/sinatra/base.rb, line 1266 def new(*args, &bk) build(*args, &bk).to_app end
# File lib/sinatra/base.rb, line 625 def initialize(app=nil) @app = app @template_cache = Tilt::Cache.new yield self if block_given? end
Create a new instance without middleware in front of it.
# File lib/sinatra/base.rb, line 1131 def post(path, opts={}, &bk) route 'POST', path, opts, &bk end
# File lib/sinatra/base.rb, line 1218 def production?; environment == :production end
The prototype instance used to process requests.
# File lib/sinatra/base.rb, line 1256 def prototype @prototype ||= new end
# File lib/sinatra/base.rb, line 1130 def put(path, opts={}, &bk) route 'PUT', path, opts, &bk end
# File lib/sinatra/base.rb, line 1233 def quit!(server, handler_name) # Use Thin's 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
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 1208 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 the Sinatra app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)
# File lib/sinatra/base.rb, line 1241 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| [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } } set :running, true end rescue Errno::EADDRINUSE => e puts "== Someone is already performing on port #{port}!" end
Access settings defined with Base.set.
# File lib/sinatra/base.rb, line 672 def self.settings self end
Rack call interface.
# File lib/sinatra/base.rb, line 632 def call(env) dup.call!(env) end
Forward the request to the downstream app – middleware only.
# File lib/sinatra/base.rb, line 701 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
Exit the current block, halts any further processing of the request, and returns the specified response.
# File lib/sinatra/base.rb, line 688 def halt(*response) response = response.first if response.length == 1 throw :halt, response end
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 696 def pass(&block) throw :pass, block end
Access settings defined with Base.set.
# File lib/sinatra/base.rb, line 677 def settings self.class.settings end
Generated with the Darkfish Rdoc Generator 2.