| Class | Sinatra::Base |
| In: |
lib/sinatra/base.rb
|
| Parent: | Object |
| CALLERS_TO_IGNORE | = | [ /lib\/sinatra.*\.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 |
| 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 897
897: def call(env)
898: synchronize { prototype.call(env) }
899: 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 965
965: def caller_files
966: caller_locations.
967: map { |file,line| file }
968: end
# File lib/sinatra/base.rb, line 970
970: def caller_locations
971: caller(1).
972: map { |line| line.split(/:(?=\d|in )/)[0,2] }.
973: reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
974: end
# File lib/sinatra/base.rb, line 762
762: def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk end
# File lib/sinatra/base.rb, line 829
829: def extensions
830: (@extensions + (superclass.extensions rescue [])).uniq
831: end
Defining a `GET` handler also automatically defines a `HEAD` handler.
# File lib/sinatra/base.rb, line 752
752: def get(path, opts={}, &block)
753: conditions = @conditions.dup
754: route('GET', path, opts, &block)
755:
756: @conditions = conditions
757: route('HEAD', path, opts, &block)
758: end
# File lib/sinatra/base.rb, line 763
763: 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 824
824: def helpers(*extensions, &block)
825: class_eval(&block) if block_given?
826: include(*extensions) if extensions.any?
827: 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 885
885: def new(*args, &bk)
886: builder = Rack::Builder.new
887: builder.use Rack::Session::Cookie if sessions? && !test?
888: builder.use Rack::CommonLogger if logging?
889: builder.use Rack::MethodOverride if methodoverride?
890: builder.use ShowExceptions if show_exceptions?
891:
892: @middleware.each { |c,a,b| builder.use(c, *a, &b) }
893: builder.run super
894: builder.to_app
895: end
# File lib/sinatra/base.rb, line 357
357: def initialize(app=nil)
358: @app = app
359: yield self if block_given?
360: end
# File lib/sinatra/base.rb, line 761
761: def post(path, opts={}, &bk); route 'POST', path, opts, &bk end
# File lib/sinatra/base.rb, line 760
760: def put(path, opts={}, &bk); route 'PUT', path, opts, &bk end
# File lib/sinatra/base.rb, line 833
833: def register(*extensions, &block)
834: extensions << Module.new(&block) if block_given?
835: @extensions += extensions
836: extensions.each do |extension|
837: extend extension
838: extension.registered(self) if extension.respond_to?(:registered)
839: end
840: end
# File lib/sinatra/base.rb, line 901
901: def reset!(base=superclass)
902: @routes = base.dupe_routes
903: @templates = base.templates.dup
904: @conditions = []
905: @filters = base.filters.dup
906: @errors = base.errors.dup
907: @middleware = base.middleware.dup
908: @prototype = nil
909: @extensions = []
910: end
Run the Sinatra app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)
# File lib/sinatra/base.rb, line 860
860: def run!(options={})
861: set options
862: handler = detect_rack_handler
863: handler_name = handler.name.gsub(/.*::/, '')
864: puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
865: "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
866: handler.run self, :Host => host, :Port => port do |server|
867: trap(:INT) do
868: ## Use thins' hard #stop! if available, otherwise just #stop
869: server.respond_to?(:stop!) ? server.stop! : server.stop
870: puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
871: end
872: end
873: rescue Errno::EADDRINUSE => e
874: puts "== Someone is already performing on port #{port}!"
875: end
Use the specified Rack middleware
# File lib/sinatra/base.rb, line 853
853: def use(middleware, *args, &block)
854: @prototype = nil
855: @middleware << [middleware, args, block]
856: end
# File lib/sinatra/base.rb, line 913
913: def dupe_routes
914: routes.inject({}) do |hash,(request_method,routes)|
915: hash[request_method] = routes.dup
916: hash
917: end
918: end
# File lib/sinatra/base.rb, line 369
369: def call!(env)
370: @env = env
371: @request = Request.new(env)
372: @response = Response.new
373: @params = nil
374:
375: invoke { dispatch! }
376: invoke { error_block!(response.status) }
377:
378: status, header, body = @response.finish
379:
380: # Never produce a body on HEAD requests. Do retain the Content-Length
381: # unless it's "0", in which case we assume it was calculated erroneously
382: # for a manual HEAD response and remove it entirely.
383: if @env['REQUEST_METHOD'] == 'HEAD'
384: body = []
385: header.delete('Content-Length') if header['Content-Length'] == '0'
386: end
387:
388: [status, header, body]
389: end
Forward the request to the downstream app — middleware only.
# File lib/sinatra/base.rb, line 411
411: def forward
412: fail "downstream app not set" unless @app.respond_to? :call
413: status, headers, body = @app.call(@request.env)
414: @response.status = status
415: @response.body = body
416: @response.headers.merge! headers
417: nil
418: end
Exit the current block, halts any further processing of the request, and returns the specified response.
# File lib/sinatra/base.rb, line 398
398: def halt(*response)
399: response = response.first if response.length == 1
400: throw :halt, response
401: end