Class: Sinatra::Base
Overview
Base class for all Sinatra applications and middleware.
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.before(&block) ⇒ Object
-
.call(env) ⇒ Object
-
.condition(&block) ⇒ Object
-
.configure(*envs, &block) ⇒ Object
-
.delete(path, opts = {}, &bk) ⇒ Object
-
.development? ⇒ Boolean
-
.disable(*opts) ⇒ Object
-
.enable(*opts) ⇒ Object
-
.error(codes = Exception, &block) ⇒ Object
-
.get(path, opts = {}, &block) ⇒ Object
-
.head(path, opts = {}, &bk) ⇒ Object
-
.helpers(*extensions, &block) ⇒ Object
-
.layout(name = :layout, &block) ⇒ Object
-
.media_type(type) ⇒ Object
Look up a media type by file extension in Rack’s mime registry.
-
.not_found(&block) ⇒ Object
-
.post(path, opts = {}, &bk) ⇒ Object
-
.production? ⇒ Boolean
-
.put(path, opts = {}, &bk) ⇒ Object
-
.register(*extensions, &block) ⇒ Object
-
.reload! ⇒ Object
-
.run!(options = {}) ⇒ Object
-
.set(option, value = self) ⇒ Object
-
.template(name, &block) ⇒ Object
-
.test? ⇒ Boolean
-
.use(middleware, *args, &block) ⇒ Object
-
.use_in_file_templates! ⇒ Object
Instance Method Summary
collapse
Methods included from Templates
#builder, #erb, #haml, #sass
Methods included from Helpers
#attachment, #back, #body, #content_type, #error, #etag, #last_modified, #media_type, #not_found, #redirect, #send_file, #session, #status
Constructor Details
#initialize(app = nil) {|_self| ... } ⇒ Base
Returns a new instance of Base.
327
328
329
330
|
# File 'lib/sinatra/base.rb', line 327
def initialize(app=nil)
@app = app
yield self if block_given?
end
|
Class Attribute Details
.conditions ⇒ Object
Returns the value of attribute conditions.
549
550
551
|
# File 'lib/sinatra/base.rb', line 549
def conditions
@conditions
end
|
.errors ⇒ Object
Returns the value of attribute errors.
549
550
551
|
# File 'lib/sinatra/base.rb', line 549
def errors
@errors
end
|
.filters ⇒ Object
Returns the value of attribute filters.
549
550
551
|
# File 'lib/sinatra/base.rb', line 549
def filters
@filters
end
|
.middleware ⇒ Object
Returns the value of attribute middleware.
549
550
551
|
# File 'lib/sinatra/base.rb', line 549
def middleware
@middleware
end
|
.routes ⇒ Object
Returns the value of attribute routes.
549
550
551
|
# File 'lib/sinatra/base.rb', line 549
def routes
@routes
end
|
.templates ⇒ Object
Returns the value of attribute templates.
549
550
551
|
# File 'lib/sinatra/base.rb', line 549
def templates
@templates
end
|
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
325
326
327
|
# File 'lib/sinatra/base.rb', line 325
def app
@app
end
|
#env ⇒ Object
Returns the value of attribute env.
337
338
339
|
# File 'lib/sinatra/base.rb', line 337
def env
@env
end
|
#params ⇒ Object
Returns the value of attribute params.
337
338
339
|
# File 'lib/sinatra/base.rb', line 337
def params
@params
end
|
#request ⇒ Object
Returns the value of attribute request.
337
338
339
|
# File 'lib/sinatra/base.rb', line 337
def request
@request
end
|
#response ⇒ Object
Returns the value of attribute response.
337
338
339
|
# File 'lib/sinatra/base.rb', line 337
def response
@response
end
|
Class Method Details
.before(&block) ⇒ Object
621
622
623
|
# File 'lib/sinatra/base.rb', line 621
def before(&block)
@filters << block
end
|
.call(env) ⇒ Object
763
764
765
766
767
768
769
|
# File 'lib/sinatra/base.rb', line 763
def call(env)
synchronize do
reload! if reload?
construct_middleware if @callsite.nil?
@callsite.call(env)
end
end
|
.condition(&block) ⇒ Object
625
626
627
|
# File 'lib/sinatra/base.rb', line 625
def condition(&block)
@conditions << block
end
|
736
737
738
739
|
# File 'lib/sinatra/base.rb', line 736
def configure(*envs, &block)
return if reloading?
yield if envs.empty? || envs.include?(environment.to_sym)
end
|
.delete(path, opts = {}, &bk) ⇒ Object
671
|
# File 'lib/sinatra/base.rb', line 671
def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk; end
|
.development? ⇒ Boolean
732
|
# File 'lib/sinatra/base.rb', line 732
def development? ; environment == :development ; end
|
.disable(*opts) ⇒ Object
572
573
574
|
# File 'lib/sinatra/base.rb', line 572
def disable(*opts)
opts.each { |key| set(key, false) }
end
|
.enable(*opts) ⇒ Object
568
569
570
|
# File 'lib/sinatra/base.rb', line 568
def enable(*opts)
opts.each { |key| set(key, true) }
end
|
.error(codes = Exception, &block) ⇒ Object
576
577
578
579
580
581
582
|
# File 'lib/sinatra/base.rb', line 576
def error(codes=Exception, &block)
if codes.respond_to? :each
codes.each { |err| error(err, &block) }
else
@errors[codes] = block
end
end
|
.get(path, opts = {}, &block) ⇒ Object
661
662
663
664
665
666
667
|
# File 'lib/sinatra/base.rb', line 661
def get(path, opts={}, &block)
conditions = @conditions.dup
route('GET', path, opts, &block)
@conditions = conditions
route('HEAD', path, opts, &block)
end
|
.head(path, opts = {}, &bk) ⇒ Object
672
|
# File 'lib/sinatra/base.rb', line 672
def head(path, opts={}, &bk); route 'HEAD', path, opts, &bk; end
|
.helpers(*extensions, &block) ⇒ Object
722
723
724
725
|
# File 'lib/sinatra/base.rb', line 722
def helpers(*extensions, &block)
class_eval(&block) if block_given?
include *extensions
end
|
.layout(name = :layout, &block) ⇒ Object
592
593
594
|
# File 'lib/sinatra/base.rb', line 592
def layout(name=:layout, &block)
template name, &block
end
|
Look up a media type by file extension in Rack’s mime registry.
615
616
617
618
619
|
# File 'lib/sinatra/base.rb', line 615
def media_type(type)
return type if type.nil? || type.to_s.include?('/')
type = ".#{type}" unless type.to_s[0] == ?.
Rack::Mime.mime_type(type, nil)
end
|
.not_found(&block) ⇒ Object
584
585
586
|
# File 'lib/sinatra/base.rb', line 584
def not_found(&block)
error 404, &block
end
|
.post(path, opts = {}, &bk) ⇒ Object
670
|
# File 'lib/sinatra/base.rb', line 670
def post(path, opts={}, &bk); route 'POST', path, opts, &bk; end
|
.production? ⇒ Boolean
734
|
# File 'lib/sinatra/base.rb', line 734
def production? ; environment == :production ; end
|
.put(path, opts = {}, &bk) ⇒ Object
669
|
# File 'lib/sinatra/base.rb', line 669
def put(path, opts={}, &bk); route 'PUT', path, opts, &bk; end
|
.register(*extensions, &block) ⇒ Object
727
728
729
730
|
# File 'lib/sinatra/base.rb', line 727
def register(*extensions, &block)
extensions << Module.new(&block) if block
extend *extensions
end
|
.reload! ⇒ Object
771
772
773
774
775
776
777
|
# File 'lib/sinatra/base.rb', line 771
def reload!
@reloading = true
superclass.send :reset!, self
$LOADED_FEATURES.delete("sinatra.rb")
::Kernel.load app_file
@reloading = false
end
|
.run!(options = {}) ⇒ Object
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
# File 'lib/sinatra/base.rb', line 746
def run!(options={})
set options
handler = detect_rack_handler
handler_name = handler.name.gsub(/.*::/, '')
puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
"on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i
handler.run self, :Host => host, :Port => port do |server|
trap(:INT) do
server.respond_to?(:stop!) ? server.stop! : server.stop
puts "\n== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
end
end
rescue Errno::EADDRINUSE => e
puts "== Someone is already performing on port #{port}!"
end
|
.set(option, value = self) ⇒ Object
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
# File 'lib/sinatra/base.rb', line 553
def set(option, value=self)
if value.kind_of?(Proc)
metadef(option, &value)
metadef("#{option}?") { !!__send__(option) }
metadef("#{option}=") { |val| set(option, Proc.new{val}) }
elsif value == self && option.respond_to?(:to_hash)
option.to_hash.each { |k,v| set(k, v) }
elsif respond_to?("#{option}=")
__send__ "#{option}=", value
else
set option, Proc.new{value}
end
self
end
|
.template(name, &block) ⇒ Object
588
589
590
|
# File 'lib/sinatra/base.rb', line 588
def template(name, &block)
templates[name] = block
end
|
.test? ⇒ Boolean
733
|
# File 'lib/sinatra/base.rb', line 733
def test? ; environment == :test ; end
|
.use(middleware, *args, &block) ⇒ Object
741
742
743
744
|
# File 'lib/sinatra/base.rb', line 741
def use(middleware, *args, &block)
reset_middleware
@middleware << [middleware, args, block]
end
|
.use_in_file_templates! ⇒ Object
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
# File 'lib/sinatra/base.rb', line 596
def use_in_file_templates!
ignore = [/lib\/sinatra.*\.rb/, /\(.*\)/, /rubygems\/custom_require\.rb/]
file = caller.
map { |line| line.sub(/:\d+.*$/, '') }.
find { |line| ignore.all? { |pattern| line !~ pattern } }
if data = ::IO.read(file).split('__END__')[1]
data.gsub!(/\r\n/, "\n")
template = nil
data.each_line do |line|
if line =~ /^@@\s*(.*)/
template = templates[$1.to_sym] = ''
elsif template
template << line
end
end
end
end
|
Instance Method Details
#call(env) ⇒ Object
333
334
335
|
# File 'lib/sinatra/base.rb', line 333
def call(env)
dup.call!(env)
end
|
#call!(env) ⇒ Object
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/sinatra/base.rb', line 339
def call!(env)
@env = env
@request = Request.new(env)
@response = Response.new
@params = nil
invoke { dispatch! }
invoke { error_block!(response.status) }
@response.body = [] if @env['REQUEST_METHOD'] == 'HEAD'
@response.finish
end
|
#forward ⇒ Object
Forward the request to the downstream app – middleware only.
371
372
373
374
375
376
377
378
|
# File 'lib/sinatra/base.rb', line 371
def forward
fail "downstream app not set" unless @app.respond_to? :call
status, , body = @app.call(@request.env)
@response.status = status
@response.body = body
.each { |k, v| @response[k] = v }
nil
end
|
#halt(*response) ⇒ Object
Exit the current block and halt the response.
360
361
362
363
|
# File 'lib/sinatra/base.rb', line 360
def halt(*response)
response = response.first if response.length == 1
throw :halt, response
end
|
#options ⇒ Object
Access options defined with Base.set.
355
356
357
|
# File 'lib/sinatra/base.rb', line 355
def options
self.class
end
|
#pass ⇒ Object
Pass control to the next matching route.
366
367
368
|
# File 'lib/sinatra/base.rb', line 366
def pass
throw :pass
end
|