| Class | Sinatra::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:/ |
| 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 1272
1272: def build(*args, &bk)
1273: builder = Rack::Builder.new
1274: builder.use Rack::MethodOverride if method_override?
1275: builder.use ShowExceptions if show_exceptions?
1276: builder.use Rack::CommonLogger if logging?
1277: setup_sessions builder
1278: middleware.each { |c,a,b| builder.use(c, *a, &b) }
1279: builder.run new!(*args, &bk)
1280: builder
1281: end
# File lib/sinatra/base.rb, line 1283
1283: def call(env)
1284: synchronize { prototype.call(env) }
1285: 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
1340: def caller_files
1341: caller_locations.
1342: map { |file,line| file }
1343: 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
1347: def caller_locations
1348: caller(1).
1349: map { |line| line.split(/:(?=\d|in )/)[0,2] }.
1350: reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
1351: end
# File lib/sinatra/base.rb, line 1132
1132: 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 1122
1122: def get(path, opts={}, &block)
1123: conditions = @conditions.dup
1124: route('GET', path, opts, &block)
1125:
1126: @conditions = conditions
1127: route('HEAD', path, opts, &block)
1128: end
# File lib/sinatra/base.rb, line 1133
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
1201: def helpers(*extensions, &block)
1202: class_eval(&block) if block_given?
1203: include(*extensions) if extensions.any?
1204: end
# File lib/sinatra/base.rb, line 625
625: def initialize(app=nil)
626: @app = app
627: @template_cache = Tilt::Cache.new
628: yield self if block_given?
629: end
# File lib/sinatra/base.rb, line 1134
1134: def options(path, opts={}, &bk) route 'OPTIONS', path, opts, &bk end
# File lib/sinatra/base.rb, line 1131
1131: def post(path, opts={}, &bk) route 'POST', path, opts, &bk end
# File lib/sinatra/base.rb, line 1130
1130: def put(path, opts={}, &bk) route 'PUT', path, opts, &bk end
# File lib/sinatra/base.rb, line 1233
1233: def quit!(server, handler_name)
1234: # Use Thin's hard #stop! if available, otherwise just #stop.
1235: server.respond_to?(:stop!) ? server.stop! : server.stop
1236: puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
1237: 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
1208: def register(*extensions, &block)
1209: extensions << Module.new(&block) if block_given?
1210: @extensions += extensions
1211: extensions.each do |extension|
1212: extend extension
1213: extension.registered(self) if extension.respond_to?(:registered)
1214: end
1215: 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
1241: def run!(options={})
1242: set options
1243: handler = detect_rack_handler
1244: handler_name = handler.name.gsub(/.*::/, '')
1245: puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
1246: "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
1247: handler.run self, :Host => bind, :Port => port do |server|
1248: [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } }
1249: set :running, true
1250: end
1251: rescue Errno::EADDRINUSE => e
1252: puts "== Someone is already performing on port #{port}!"
1253: end
Use the specified Rack middleware
# File lib/sinatra/base.rb, line 1228
1228: def use(middleware, *args, &block)
1229: @prototype = nil
1230: @middleware << [middleware, args, block]
1231: end
Forward the request to the downstream app — middleware only.
# File lib/sinatra/base.rb, line 701
701: def forward
702: fail "downstream app not set" unless @app.respond_to? :call
703: status, headers, body = @app.call(@request.env)
704: @response.status = status
705: @response.body = body
706: @response.headers.merge! headers
707: nil
708: end
Exit the current block, halts any further processing of the request, and returns the specified response.
# File lib/sinatra/base.rb, line 688
688: def halt(*response)
689: response = response.first if response.length == 1
690: throw :halt, response
691: end