Class | Sinatra::Base |
In: |
lib/sinatra/base.rb
|
Parent: | Object |
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 793 793: def call(env) 794: synchronize do 795: reload! if reload? 796: prototype.call(env) 797: end 798: end
# File lib/sinatra/base.rb, line 748 748: def configure(*envs, &block) 749: return if reloading? 750: yield if envs.empty? || envs.include?(environment.to_sym) 751: end
# File lib/sinatra/base.rb, line 680 680: def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk; end
# File lib/sinatra/base.rb, line 581 581: def disable(*opts) 582: opts.each { |key| set(key, false) } 583: end
# File lib/sinatra/base.rb, line 577 577: def enable(*opts) 578: opts.each { |key| set(key, true) } 579: end
# File lib/sinatra/base.rb, line 585 585: def error(codes=Exception, &block) 586: if codes.respond_to? :each 587: codes.each { |err| error(err, &block) } 588: else 589: @errors[codes] = block 590: end 591: end
# File lib/sinatra/base.rb, line 670 670: def get(path, opts={}, &block) 671: conditions = @conditions.dup 672: route('GET', path, opts, &block) 673: 674: @conditions = conditions 675: route('HEAD', path, opts, &block) 676: end
# File lib/sinatra/base.rb, line 681 681: def head(path, opts={}, &bk); route 'HEAD', path, opts, &bk; end
# File lib/sinatra/base.rb, line 731 731: def helpers(*extensions, &block) 732: class_eval(&block) if block_given? 733: include *extensions if extensions.any? 734: end
# File lib/sinatra/base.rb, line 601 601: def layout(name=:layout, &block) 602: template name, &block 603: end
Look up a media type by file extension in Rack‘s mime registry.
# File lib/sinatra/base.rb, line 624 624: def media_type(type) 625: return type if type.nil? || type.to_s.include?('/') 626: type = ".#{type}" unless type.to_s[0] == ?. 627: Rack::Mime.mime_type(type, nil) 628: 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 783 783: def new(*args, &bk) 784: builder = Rack::Builder.new 785: builder.use Rack::Session::Cookie if sessions? && !test? 786: builder.use Rack::CommonLogger if logging? 787: builder.use Rack::MethodOverride if methodoverride? 788: @middleware.each { |c, args, bk| builder.use(c, *args, &bk) } 789: builder.run super 790: builder.to_app 791: end
# File lib/sinatra/base.rb, line 332 332: def initialize(app=nil) 333: @app = app 334: yield self if block_given? 335: end
# File lib/sinatra/base.rb, line 679 679: def post(path, opts={}, &bk); route 'POST', path, opts, &bk; end
# File lib/sinatra/base.rb, line 678 678: def put(path, opts={}, &bk); route 'PUT', path, opts, &bk; end
# File lib/sinatra/base.rb, line 736 736: def register(*extensions, &block) 737: extensions << Module.new(&block) if block_given? 738: extensions.each do |extension| 739: extend extension 740: extension.registered(self) if extension.respond_to?(:registered) 741: end 742: end
# File lib/sinatra/base.rb, line 804 804: def reload! 805: @reloading = true 806: reset! 807: $LOADED_FEATURES.delete("sinatra.rb") 808: ::Kernel.load app_file 809: @reloading = false 810: end
# File lib/sinatra/base.rb, line 812 812: def reset!(base=superclass) 813: @routes = base.dupe_routes 814: @templates = base.templates.dup 815: @conditions = [] 816: @filters = base.filters.dup 817: @errors = base.errors.dup 818: @middleware = base.middleware.dup 819: @prototype = nil 820: end
# File lib/sinatra/base.rb, line 758 758: def run!(options={}) 759: set options 760: handler = detect_rack_handler 761: handler_name = handler.name.gsub(/.*::/, '') 762: puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " + 763: "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i 764: handler.run self, :Host => host, :Port => port do |server| 765: trap(:INT) do 766: ## Use thins' hard #stop! if available, otherwise just #stop 767: server.respond_to?(:stop!) ? server.stop! : server.stop 768: puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i 769: end 770: end 771: rescue Errno::EADDRINUSE => e 772: puts "== Someone is already performing on port #{port}!" 773: end
# File lib/sinatra/base.rb, line 562 562: def set(option, value=self) 563: if value.kind_of?(Proc) 564: metadef(option, &value) 565: metadef("#{option}?") { !!__send__(option) } 566: metadef("#{option}=") { |val| set(option, Proc.new{val}) } 567: elsif value == self && option.respond_to?(:to_hash) 568: option.to_hash.each { |k,v| set(k, v) } 569: elsif respond_to?("#{option}=") 570: __send__ "#{option}=", value 571: else 572: set option, Proc.new{value} 573: end 574: self 575: end
# File lib/sinatra/base.rb, line 597 597: def template(name, &block) 598: templates[name] = block 599: end
# File lib/sinatra/base.rb, line 753 753: def use(middleware, *args, &block) 754: @prototype = nil 755: @middleware << [middleware, args, block] 756: end
# File lib/sinatra/base.rb, line 605 605: def use_in_file_templates! 606: ignore = [/lib\/sinatra.*\.rb/, /\(.*\)/, /rubygems\/custom_require\.rb/] 607: file = caller. 608: map { |line| line.sub(/:\d+.*$/, '') }. 609: find { |line| ignore.all? { |pattern| line !~ pattern } } 610: if data = ::IO.read(file).split('__END__')[1] 611: data.gsub!(/\r\n/, "\n") 612: template = nil 613: data.each_line do |line| 614: if line =~ /^@@\s*(.*)/ 615: template = templates[$1.to_sym] = '' 616: elsif template 617: template << line 618: end 619: end 620: end 621: end
# File lib/sinatra/base.rb, line 823 823: def dupe_routes 824: routes.inject({}) do |hash,(request_method,routes)| 825: hash[request_method] = routes.dup 826: hash 827: end 828: end
# File lib/sinatra/base.rb, line 344 344: def call!(env) 345: @env = env 346: @request = Request.new(env) 347: @response = Response.new 348: @params = nil 349: 350: invoke { dispatch! } 351: invoke { error_block!(response.status) } 352: 353: status, header, body = @response.finish 354: 355: # Never produce a body on HEAD requests. Do retain the Content-Length 356: # unless it's "0", in which case we assume it was calculated erroneously 357: # for a manual HEAD response and remove it entirely. 358: if @env['REQUEST_METHOD'] == 'HEAD' 359: body = [] 360: header.delete('Content-Length') if header['Content-Length'] == '0' 361: end 362: 363: [status, header, body] 364: end
Forward the request to the downstream app — middleware only.
# File lib/sinatra/base.rb, line 383 383: def forward 384: fail "downstream app not set" unless @app.respond_to? :call 385: status, headers, body = @app.call(@request.env) 386: @response.status = status 387: @response.body = body 388: @response.headers.merge! headers 389: nil 390: end