Class | Sinatra::Base |
In: |
lib/sinatra/base.rb
|
Parent: | Object |
CALLERS_TO_IGNORE | = | [ /\/sinatra(\/(base|main|showexceptions))?\.rb$/, # all sinatra code /lib\/tilt.*\.rb$/, # all tilt code /\(.*\)/, # generated code /custom_require\.rb$/, # rubygems require hacks /active_support/, # active_support require hacks ] |
user_agent | -> | agent |
method_override? | -> | methodoverride? |
method_override= | -> | methodoverride= |
after_filters | [R] | |
app | [RW] | |
before_filters | [R] | |
env | [RW] | |
errors | [R] | |
params | [RW] | |
request | [RW] | |
response | [RW] | |
routes | [R] | |
templates | [R] |
# File lib/sinatra/base.rb, line 978 978: def call(env) 979: synchronize { prototype.call(env) } 980: 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 1028 1028: def caller_files 1029: caller_locations. 1030: map { |file,line| file } 1031: end
# File lib/sinatra/base.rb, line 1033 1033: def caller_locations 1034: caller(1). 1035: map { |line| line.split(/:(?=\d|in )/)[0,2] }. 1036: reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } } 1037: end
# File lib/sinatra/base.rb, line 846 846: def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk end
Defining a `GET` handler also automatically defines a `HEAD` handler.
# File lib/sinatra/base.rb, line 836 836: def get(path, opts={}, &block) 837: conditions = @conditions.dup 838: route('GET', path, opts, &block) 839: 840: @conditions = conditions 841: route('HEAD', path, opts, &block) 842: end
# File lib/sinatra/base.rb, line 847 847: 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 908 908: def helpers(*extensions, &block) 909: class_eval(&block) if block_given? 910: include(*extensions) if extensions.any? 911: end
# File lib/sinatra/base.rb, line 391 391: def initialize(app=nil) 392: @app = app 393: @template_cache = Tilt::Cache.new 394: yield self if block_given? 395: 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 966 966: def new(*args, &bk) 967: builder = Rack::Builder.new 968: builder.use Rack::Session::Cookie if sessions? 969: builder.use Rack::CommonLogger if logging? 970: builder.use Rack::MethodOverride if method_override? 971: builder.use ShowExceptions if show_exceptions? 972: middleware.each { |c,a,b| builder.use(c, *a, &b) } 973: 974: builder.run super 975: builder.to_app 976: end
# File lib/sinatra/base.rb, line 845 845: def post(path, opts={}, &bk); route 'POST', path, opts, &bk end
# File lib/sinatra/base.rb, line 844 844: def put(path, opts={}, &bk); route 'PUT', path, opts, &bk end
# File lib/sinatra/base.rb, line 913 913: def register(*extensions, &block) 914: extensions << Module.new(&block) if block_given? 915: @extensions += extensions 916: extensions.each do |extension| 917: extend extension 918: extension.registered(self) if extension.respond_to?(:registered) 919: end 920: end
Run the Sinatra app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)
# File lib/sinatra/base.rb, line 940 940: def run!(options={}) 941: set options 942: handler = detect_rack_handler 943: handler_name = handler.name.gsub(/.*::/, '') 944: puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " + 945: "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i 946: handler.run self, :Host => bind, :Port => port do |server| 947: trap(:INT) do 948: ## Use thins' hard #stop! if available, otherwise just #stop 949: server.respond_to?(:stop!) ? server.stop! : server.stop 950: puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i 951: end 952: set :running, true 953: end 954: rescue Errno::EADDRINUSE => e 955: puts "== Someone is already performing on port #{port}!" 956: end
Use the specified Rack middleware
# File lib/sinatra/base.rb, line 933 933: def use(middleware, *args, &block) 934: @prototype = nil 935: @middleware << [middleware, args, block] 936: end
# File lib/sinatra/base.rb, line 404 404: def call!(env) 405: @env = env 406: @request = Request.new(env) 407: @response = Response.new 408: @params = indifferent_params(@request.params) 409: @template_cache.clear if settings.reload_templates 410: 411: invoke { dispatch! } 412: invoke { error_block!(response.status) } 413: 414: status, header, body = @response.finish 415: 416: # Never produce a body on HEAD requests. Do retain the Content-Length 417: # unless it's "0", in which case we assume it was calculated erroneously 418: # for a manual HEAD response and remove it entirely. 419: if @env['REQUEST_METHOD'] == 'HEAD' 420: body = [] 421: header.delete('Content-Length') if header['Content-Length'] == '0' 422: end 423: 424: [status, header, body] 425: end
Forward the request to the downstream app — middleware only.
# File lib/sinatra/base.rb, line 448 448: def forward 449: fail "downstream app not set" unless @app.respond_to? :call 450: status, headers, body = @app.call(@request.env) 451: @response.status = status 452: @response.body = body 453: @response.headers.merge! headers 454: nil 455: end
Exit the current block, halts any further processing of the request, and returns the specified response.
# File lib/sinatra/base.rb, line 435 435: def halt(*response) 436: response = response.first if response.length == 1 437: throw :halt, response 438: end