Class: Sinatra::Base

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils, Helpers, Templates
Defined in:
lib/sinatra/base.rb

Direct Known Subclasses

Default

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Templates

#builder, #erb, #haml, #lookup_layout, #lookup_template, #render, #render_builder, #render_erb, #render_haml, #render_sass, #sass, #template_path

Methods included from Helpers

#attachment, #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.

Yields:

  • (_self)

Yield Parameters:

  • _self (Sinatra::Base)

    the object that the method was called on



297
298
299
300
# File 'lib/sinatra/base.rb', line 297

def initialize(app=nil)
  @app = app
  yield self if block_given?
end

Class Attribute Details

.conditionsObject

Returns the value of attribute conditions.



454
455
456
# File 'lib/sinatra/base.rb', line 454

def conditions
  @conditions
end

.errorsObject

Returns the value of attribute errors.



454
455
456
# File 'lib/sinatra/base.rb', line 454

def errors
  @errors
end

.filtersObject

Returns the value of attribute filters.



454
455
456
# File 'lib/sinatra/base.rb', line 454

def filters
  @filters
end

.middlewareObject

Returns the value of attribute middleware.



454
455
456
# File 'lib/sinatra/base.rb', line 454

def middleware
  @middleware
end

.routesObject

Returns the value of attribute routes.



454
455
456
# File 'lib/sinatra/base.rb', line 454

def routes
  @routes
end

.templatesObject

Returns the value of attribute templates.



454
455
456
# File 'lib/sinatra/base.rb', line 454

def templates
  @templates
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



295
296
297
# File 'lib/sinatra/base.rb', line 295

def app
  @app
end

#envObject

Returns the value of attribute env.



306
307
308
# File 'lib/sinatra/base.rb', line 306

def env
  @env
end

#paramsObject

Returns the value of attribute params.



306
307
308
# File 'lib/sinatra/base.rb', line 306

def params
  @params
end

#requestObject

Returns the value of attribute request.



306
307
308
# File 'lib/sinatra/base.rb', line 306

def request
  @request
end

#responseObject

Returns the value of attribute response.



306
307
308
# File 'lib/sinatra/base.rb', line 306

def response
  @response
end

Class Method Details

.accept_mime_types(types) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/sinatra/base.rb', line 552

def accept_mime_types(types)
  types = [types] unless types.kind_of? Array
  types.map!{|t| media_type(t)}

  condition {
    matching_types = (request.accept & types)
    unless matching_types.empty?
      response.headers['Content-Type'] = matching_types.first
      true
    else
      false
    end
  }
end

.before(&block) ⇒ Object



529
530
531
# File 'lib/sinatra/base.rb', line 529

def before(&block)
  @filters << block
end

.call(env) ⇒ Object



649
650
651
652
# File 'lib/sinatra/base.rb', line 649

def call(env)
  construct_middleware if @callsite.nil?
  @callsite.call(env)
end

.condition(&block) ⇒ Object



533
534
535
# File 'lib/sinatra/base.rb', line 533

def condition(&block)
  @conditions << block
end

.configure(*envs, &block) ⇒ Object



623
624
625
# File 'lib/sinatra/base.rb', line 623

def configure(*envs, &block)
  yield if envs.empty? || envs.include?(environment.to_sym)
end

.delete(path, opts = {}, &bk) ⇒ Object



577
# File 'lib/sinatra/base.rb', line 577

def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk; end

.development?Boolean

Returns:

  • (Boolean)


619
# File 'lib/sinatra/base.rb', line 619

def development? ; environment == :development ; end

.disable(*opts) ⇒ Object



476
477
478
# File 'lib/sinatra/base.rb', line 476

def disable(*opts)
  opts.each { |key| set(key, false) }
end

.enable(*opts) ⇒ Object



472
473
474
# File 'lib/sinatra/base.rb', line 472

def enable(*opts)
  opts.each { |key| set(key, true) }
end

.error(codes = Exception, &block) ⇒ Object



480
481
482
483
484
485
486
# File 'lib/sinatra/base.rb', line 480

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



567
568
569
570
571
572
573
# File 'lib/sinatra/base.rb', line 567

def get(path, opts={}, &block)
  conditions = @conditions.dup
  route('GET', path, opts, &block)

  @conditions = conditions
  head(path, opts) { invoke(block) ; [] }
end

.head(path, opts = {}, &bk) ⇒ Object



578
# File 'lib/sinatra/base.rb', line 578

def head(path, opts={}, &bk); route 'HEAD', path, opts, &bk; end

.host_name(pattern) ⇒ Object



537
538
539
# File 'lib/sinatra/base.rb', line 537

def host_name(pattern)
  condition { pattern === request.host }
end

.layout(name = :layout, &block) ⇒ Object



496
497
498
# File 'lib/sinatra/base.rb', line 496

def layout(name=:layout, &block)
  template name, &block
end

.media_type(type) ⇒ Object

Look up a media type by file extension in Rack’s mime registry.



523
524
525
526
527
# File 'lib/sinatra/base.rb', line 523

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



488
489
490
# File 'lib/sinatra/base.rb', line 488

def not_found(&block)
  error 404, &block
end

.post(path, opts = {}, &bk) ⇒ Object



576
# File 'lib/sinatra/base.rb', line 576

def post(path, opts={}, &bk); route 'POST', path, opts, &bk; end

.production?Boolean

Returns:

  • (Boolean)


621
# File 'lib/sinatra/base.rb', line 621

def production? ; environment == :production ; end

.put(path, opts = {}, &bk) ⇒ Object



575
# File 'lib/sinatra/base.rb', line 575

def put(path, opts={}, &bk); route 'PUT', path, opts, &bk; end

.run!(options = {}) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/sinatra/base.rb', line 632

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}"
  handler.run self, :Host => host, :Port => port do |server|
    trap(:INT) do
      ## Use thins' hard #stop! if available, otherwise just #stop
      server.respond_to?(:stop!) ? server.stop! : server.stop
      puts "\n== Sinatra has ended his set (crowd applauds)"
    end
  end
rescue Errno::EADDRINUSE => e
  puts "== Someone is already performing on port #{port}!"
end

.set(option, value = self) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/sinatra/base.rb', line 457

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(&method(:set))
  elsif respond_to?("#{option}=")
    __send__ "#{option}=", value
  else
    set option, Proc.new{value}
  end
  self
end

.template(name, &block) ⇒ Object



492
493
494
# File 'lib/sinatra/base.rb', line 492

def template(name, &block)
  templates[name] = block
end

.test?Boolean

Returns:

  • (Boolean)


620
# File 'lib/sinatra/base.rb', line 620

def test? ; environment == :test ; end

.use(middleware, *args, &block) ⇒ Object



627
628
629
630
# File 'lib/sinatra/base.rb', line 627

def use(middleware, *args, &block)
  reset_middleware
  @middleware << [middleware, args, block]
end

.use_in_file_templates!Object



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/sinatra/base.rb', line 500

def use_in_file_templates!
  line = caller.detect do |s|
    [
     /lib\/sinatra.*\.rb/,
     /\(.*\)/,
     /rubygems\/custom_require\.rb/
    ].all? { |x| s !~ x }
  end
  file = line.sub(/:\d+.*$/, '')
  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

.user_agent(pattern) ⇒ Object



541
542
543
544
545
546
547
548
549
550
# File 'lib/sinatra/base.rb', line 541

def user_agent(pattern)
  condition {
    if request.user_agent =~ pattern
      @params[:agent] = $~[1..-1]
      true
    else
      false
    end
  }
end

Instance Method Details

#call(env) ⇒ Object



302
303
304
# File 'lib/sinatra/base.rb', line 302

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



308
309
310
311
312
313
314
315
# File 'lib/sinatra/base.rb', line 308

def call!(env)
  @env      = env
  @request  = Request.new(env)
  @response = Response.new
  @params   = nil
  error_detection { dispatch! }
  @response.finish
end

#halt(*response) ⇒ Object



321
322
323
# File 'lib/sinatra/base.rb', line 321

def halt(*response)
  throw :halt, *response
end

#optionsObject



317
318
319
# File 'lib/sinatra/base.rb', line 317

def options
  self.class
end

#passObject



325
326
327
# File 'lib/sinatra/base.rb', line 325

def pass
  throw :pass
end