Class: Sinatra::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/codebutler/sinatra.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



653
654
655
656
657
658
659
660
661
662
# File 'lib/codebutler/sinatra.rb', line 653

def initialize
  @clearables = [
    @events = Hash.new { |hash, key| hash[key] = [] },
    @errors = Hash.new,
    @filters = Hash.new { |hash, key| hash[key] = [] },
    @templates = Hash.new
  ]
  load_options!
  load_default_events!
end

Instance Attribute Details

#clearablesObject (readonly)

Returns the value of attribute clearables.



614
615
616
# File 'lib/codebutler/sinatra.rb', line 614

def clearables
  @clearables
end

#errorsObject (readonly)

Returns the value of attribute errors.



613
614
615
# File 'lib/codebutler/sinatra.rb', line 613

def errors
  @errors
end

#eventsObject (readonly)

Returns the value of attribute events.



613
614
615
# File 'lib/codebutler/sinatra.rb', line 613

def events
  @events
end

#filtersObject (readonly)

Returns the value of attribute filters.



613
614
615
# File 'lib/codebutler/sinatra.rb', line 613

def filters
  @filters
end

#optionsObject



696
697
698
# File 'lib/codebutler/sinatra.rb', line 696

def options
  @options ||= OpenStruct.new(default_options)
end

#reloadingObject (readonly)

Returns the value of attribute reloading.



614
615
616
# File 'lib/codebutler/sinatra.rb', line 614

def reloading
  @reloading
end

#templatesObject (readonly)

Returns the value of attribute templates.



613
614
615
# File 'lib/codebutler/sinatra.rb', line 613

def templates
  @templates
end

Class Method Details

.default_optionsObject



618
619
620
621
622
623
624
625
626
627
# File 'lib/codebutler/sinatra.rb', line 618

def self.default_options
  @@default_options ||= {
    :run => true,
    :port => 4567,
    :env => :development,
    :root => Dir.pwd,
    :views => Dir.pwd + '/views',
    :public => Dir.pwd + '/public'
  }
end

Instance Method Details

#call(env) ⇒ Object



725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/codebutler/sinatra.rb', line 725

def call(env)
  reload! if development?
  request = Rack::Request.new(env)
  result = lookup(request)
  context = EventContext.new(
    request, 
    Rack::Response.new,
    result.params
  )
  context.status(result.status)
  begin
    returned = run_safely do
      catch(:halt) do
        filters[:before].each { |f| context.instance_eval(&f) }
        [:complete, context.instance_eval(&result.block)]
      end
    end
    body = returned.to_result(context)
  rescue => e
    request.env['sinatra.error'] = e
    context.status(500)
    result = (errors[e.class] || errors[ServerError]).invoke(request)
    returned = run_safely do
      catch(:halt) do
        [:complete, context.instance_eval(&result.block)]
      end
    end
    body = returned.to_result(context)
  end
  body = '' unless body.respond_to?(:each)
  body = '' if request.request_method.upcase == 'HEAD'
  context.body = body.kind_of?(String) ? [*body] : body
  context.finish
end

#default_optionsObject



629
630
631
# File 'lib/codebutler/sinatra.rb', line 629

def default_options
  self.class.default_options
end

#define_error(code, options = {}, &b) ⇒ Object



673
674
675
# File 'lib/codebutler/sinatra.rb', line 673

def define_error(code, options = {}, &b)
  errors[code] = Error.new(code, &b)
end

#define_event(method, path, options = {}, &b) ⇒ Object



664
665
666
667
# File 'lib/codebutler/sinatra.rb', line 664

def define_event(method, path, options = {}, &b)
  events[method] << event = Event.new(path, options, &b)
  event
end

#define_filter(type, &b) ⇒ Object



677
678
679
# File 'lib/codebutler/sinatra.rb', line 677

def define_filter(type, &b)
  filters[:before] << b
end

#define_template(name = :layout, &b) ⇒ Object



669
670
671
# File 'lib/codebutler/sinatra.rb', line 669

def define_template(name=:layout, &b)
  templates[name] = b
end

#development?Boolean

Returns:

  • (Boolean)


700
701
702
# File 'lib/codebutler/sinatra.rb', line 700

def development?
  options.env == :development
end

#load_default_events!Object

Called immediately after the application is initialized or reloaded to register default events. Events added here have dibs on requests since they appear first in the list.



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

def load_default_events!
  events[:get] << Static.new
end

#load_options!Object

Load all options given on the command line NOTE: Ignores –name so unit/spec tests can run individually



637
638
639
640
641
642
643
644
# File 'lib/codebutler/sinatra.rb', line 637

def load_options!
  require 'optparse'
  OptionParser.new do |op|
    op.on('-p port') { |port| default_options[:port] = port }
    op.on('-e env') { |env| default_options[:env] = env }
    op.on('-x') { |env| default_options[:mutex] = true }
  end.parse!(ARGV.dup.select { |o| o !~ /--name/ })
end

#lookup(request) ⇒ Object

Visits and invokes each handler registered for the request_method in definition order until a Result response is produced. If no handler responds with a Result, the NotFound error handler is invoked.

When the request_method is “HEAD” and no valid Result is produced by the set of handlers registered for HEAD requests, an attempt is made to invoke the GET handlers to generate the response before resorting to the default error handler.



689
690
691
692
693
694
# File 'lib/codebutler/sinatra.rb', line 689

def lookup(request)
  method = request.request_method.downcase.to_sym
  events[method].eject(&[:invoke, request]) ||
    (events[:get].eject(&[:invoke, request]) if method == :head) ||
    errors[NotFound].invoke(request)
end

#mutexObject



713
714
715
# File 'lib/codebutler/sinatra.rb', line 713

def mutex
  @@mutex ||= Mutex.new
end

#reload!Object



704
705
706
707
708
709
710
711
# File 'lib/codebutler/sinatra.rb', line 704

def reload!
  @reloading = true
  clearables.each(&:clear)
  load_default_events!
  Kernel.load $0
  @reloading = false
  Environment.setup!
end

#run_safelyObject



717
718
719
720
721
722
723
# File 'lib/codebutler/sinatra.rb', line 717

def run_safely
  if options.mutex
    mutex.synchronize { yield }
  else
    yield
  end
end