Class | Sinatra::Base |
In: |
lib/sinatra/base.rb
|
Parent: | Object |
CALLERS_TO_IGNORE | = | [ /\/sinatra(\/(base|main|showexceptions|compat))?\.rb$/, # all sinatra code /\(.*\)/, # generated code /custom_require\.rb$/, # rubygems require hacks /active_support/, # active_support require hacks ] unless self.const_defined?('CALLERS_TO_IGNORE') |
user_agent | -> | agent |
method_override? | -> | methodoverride? |
method_override= | -> | methodoverride= |
app | [RW] | |
conditions | [RW] | |
env | [RW] | |
errors | [RW] | |
filters | [RW] | |
middleware | [RW] | |
params | [RW] | |
request | [RW] | |
response | [RW] | |
routes | [RW] | |
templates | [RW] |
# File lib/sinatra/base.rb, line 927 927: def call(env) 928: synchronize { prototype.call(env) } 929: 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 995 995: def caller_files 996: caller_locations. 997: map { |file,line| file } 998: end
# File lib/sinatra/base.rb, line 1000 1000: def caller_locations 1001: caller(1). 1002: map { |line| line.split(/:(?=\d|in )/)[0,2] }. 1003: reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } } 1004: end
# File lib/sinatra/base.rb, line 792 792: def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk end
# File lib/sinatra/base.rb, line 859 859: def extensions 860: (@extensions + (superclass.extensions rescue [])).uniq 861: end
Defining a `GET` handler also automatically defines a `HEAD` handler.
# File lib/sinatra/base.rb, line 782 782: def get(path, opts={}, &block) 783: conditions = @conditions.dup 784: route('GET', path, opts, &block) 785: 786: @conditions = conditions 787: route('HEAD', path, opts, &block) 788: end
# File lib/sinatra/base.rb, line 793 793: 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 854 854: def helpers(*extensions, &block) 855: class_eval(&block) if block_given? 856: include(*extensions) if extensions.any? 857: end
# File lib/sinatra/base.rb, line 370 370: def initialize(app=nil) 371: @app = app 372: yield self if block_given? 373: 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 915 915: def new(*args, &bk) 916: builder = Rack::Builder.new 917: builder.use Rack::Session::Cookie if sessions? && !test? 918: builder.use Rack::CommonLogger if logging? 919: builder.use Rack::MethodOverride if method_override? 920: builder.use ShowExceptions if show_exceptions? 921: 922: @middleware.each { |c,a,b| builder.use(c, *a, &b) } 923: builder.run super 924: builder.to_app 925: end
# File lib/sinatra/base.rb, line 791 791: def post(path, opts={}, &bk); route 'POST', path, opts, &bk end
# File lib/sinatra/base.rb, line 790 790: def put(path, opts={}, &bk); route 'PUT', path, opts, &bk end
# File lib/sinatra/base.rb, line 863 863: def register(*extensions, &block) 864: extensions << Module.new(&block) if block_given? 865: @extensions += extensions 866: extensions.each do |extension| 867: extend extension 868: extension.registered(self) if extension.respond_to?(:registered) 869: end 870: end
# File lib/sinatra/base.rb, line 931 931: def reset!(base=superclass) 932: @routes = base.dupe_routes 933: @templates = base.templates.dup 934: @conditions = [] 935: @filters = base.filters.dup 936: @errors = base.errors.dup 937: @middleware = base.middleware.dup 938: @prototype = nil 939: @extensions = [] 940: end
Run the Sinatra app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)
# File lib/sinatra/base.rb, line 890 890: def run!(options={}) 891: set options 892: handler = detect_rack_handler 893: handler_name = handler.name.gsub(/.*::/, '') 894: puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " + 895: "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i 896: handler.run self, :Host => host, :Port => port do |server| 897: trap(:INT) do 898: ## Use thins' hard #stop! if available, otherwise just #stop 899: server.respond_to?(:stop!) ? server.stop! : server.stop 900: puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i 901: end 902: end 903: rescue Errno::EADDRINUSE => e 904: puts "== Someone is already performing on port #{port}!" 905: end
Use the specified Rack middleware
# File lib/sinatra/base.rb, line 883 883: def use(middleware, *args, &block) 884: @prototype = nil 885: @middleware << [middleware, args, block] 886: end
# File lib/sinatra/base.rb, line 943 943: def dupe_routes 944: routes.inject({}) do |hash,(request_method,routes)| 945: hash[request_method] = routes.dup 946: hash 947: end 948: end
# File lib/sinatra/base.rb, line 382 382: def call!(env) 383: @env = env 384: @request = Request.new(env) 385: @response = Response.new 386: @params = nil 387: 388: invoke { dispatch! } 389: invoke { error_block!(response.status) } 390: 391: status, header, body = @response.finish 392: 393: # Never produce a body on HEAD requests. Do retain the Content-Length 394: # unless it's "0", in which case we assume it was calculated erroneously 395: # for a manual HEAD response and remove it entirely. 396: if @env['REQUEST_METHOD'] == 'HEAD' 397: body = [] 398: header.delete('Content-Length') if header['Content-Length'] == '0' 399: end 400: 401: [status, header, body] 402: end
Forward the request to the downstream app — middleware only.
# File lib/sinatra/base.rb, line 425 425: def forward 426: fail "downstream app not set" unless @app.respond_to? :call 427: status, headers, body = @app.call(@request.env) 428: @response.status = status 429: @response.body = body 430: @response.headers.merge! headers 431: nil 432: end
Exit the current block, halts any further processing of the request, and returns the specified response.
# File lib/sinatra/base.rb, line 412 412: def halt(*response) 413: response = response.first if response.length == 1 414: throw :halt, response 415: end