Class | Sinatra::Helpers::Stream::Base |
In: |
lib/sinatra/base.rb
|
Parent: | Object |
CALLERS_TO_IGNORE | = | [ # :nodoc: /\/sinatra(\/(base|main|showexceptions))?\.rb$/, # all sinatra code /lib\/tilt.*\.rb$/, # all tilt code /^\(.*\)$/, # generated code /rubygems\/custom_require\.rb$/, # rubygems require hacks /active_support/, # active_support require hacks /bundler(\/runtime)?\.rb/, # bundler require hacks /<internal:/, # internal in ruby >= 1.9.2 /src\/kernel\/bootstrap\/[A-Z]/ |
user_agent | -> | agent |
new | -> | new! |
Create a new instance without middleware in front of it. | ||
method_override? | -> | methodoverride? |
method_override= | -> | methodoverride= |
app | [RW] | |
env | [RW] | |
errors | [R] | |
filters | [R] | |
params | [RW] | |
request | [RW] | |
response | [RW] | |
routes | [R] | |
template_cache | [R] | |
templates | [R] |
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 1326: def build(builder, *args, &bk) 1327: setup_default_middleware builder 1328: setup_middleware builder 1329: builder.run new!(*args, &bk) 1330: builder 1331: end
# File lib/sinatra/base.rb, line 1333 1333: def call(env) 1334: synchronize { prototype.call(env) } 1335: 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 1437 1437: def caller_files 1438: cleaned_caller(1).flatten 1439: 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 1443 1443: def caller_locations 1444: cleaned_caller 2 1445: end
# File lib/sinatra/base.rb, line 1176 1176: 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 1166 1166: def get(path, opts={}, &block) 1167: conditions = @conditions.dup 1168: route('GET', path, opts, &block) 1169: 1170: @conditions = conditions 1171: route('HEAD', path, opts, &block) 1172: end
# File lib/sinatra/base.rb, line 1177 1177: 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 1250 1250: def helpers(*extensions, &block) 1251: class_eval(&block) if block_given? 1252: include(*extensions) if extensions.any? 1253: end
# File lib/sinatra/base.rb, line 696 696: def initialize(app=nil) 697: super() 698: @app = app 699: @template_cache = Tilt::Cache.new 700: yield self if block_given? 701: end
# File lib/sinatra/base.rb, line 1178 1178: def options(path, opts={}, &bk) route 'OPTIONS', path, opts, &bk end
# File lib/sinatra/base.rb, line 1179 1179: def patch(path, opts={}, &bk) route 'PATCH', path, opts, &bk end
# File lib/sinatra/base.rb, line 1175 1175: def post(path, opts={}, &bk) route 'POST', path, opts, &bk end
# File lib/sinatra/base.rb, line 1174 1174: def put(path, opts={}, &bk) route 'PUT', path, opts, &bk end
# File lib/sinatra/base.rb, line 1282 1282: def quit!(server, handler_name) 1283: # Use Thin's hard #stop! if available, otherwise just #stop. 1284: server.respond_to?(:stop!) ? server.stop! : server.stop 1285: $stderr.puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i 1286: 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 1257 1257: def register(*extensions, &block) 1258: extensions << Module.new(&block) if block_given? 1259: @extensions += extensions 1260: extensions.each do |extension| 1261: extend extension 1262: extension.registered(self) if extension.respond_to?(:registered) 1263: end 1264: end
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 1291: def run!(options={}) 1292: set options 1293: handler = detect_rack_handler 1294: handler_name = handler.name.gsub(/.*::/, '') 1295: handler.run self, :Host => bind, :Port => port do |server| 1296: unless handler_name =~ /cgi/i 1297: $stderr.puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " + 1298: "on #{port} for #{environment} with backup from #{handler_name}" 1299: end 1300: [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } } 1301: server.threaded = settings.threaded if server.respond_to? :threaded= 1302: set :running, true 1303: yield server if block_given? 1304: end 1305: rescue Errno::EADDRINUSE => e 1306: $stderr.puts "== Someone is already performing on port #{port}!" 1307: end
Use the specified Rack middleware
# File lib/sinatra/base.rb, line 1277 1277: def use(middleware, *args, &block) 1278: @prototype = nil 1279: @middleware << [middleware, args, block] 1280: end
Forward the request to the downstream app — middleware only.
# File lib/sinatra/base.rb, line 764 764: def forward 765: fail "downstream app not set" unless @app.respond_to? :call 766: status, headers, body = @app.call env 767: @response.status = status 768: @response.body = body 769: @response.headers.merge! headers 770: nil 771: end
Exit the current block, halts any further processing of the request, and returns the specified response.
# File lib/sinatra/base.rb, line 751 751: def halt(*response) 752: response = response.first if response.length == 1 753: throw :halt, response 754: end
# File lib/sinatra/base.rb, line 743 743: def options 744: warn "Sinatra::Base#options is deprecated and will be removed, " \ 745: "use #settings instead." 746: settings 747: end