Class: Sinatra::Application
- Inherits:
-
Object
- Object
- Sinatra::Application
- Defined in:
- lib/codebutler/sinatra.rb
Instance Attribute Summary collapse
-
#clearables ⇒ Object
readonly
Returns the value of attribute clearables.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
- #options ⇒ Object
-
#reloading ⇒ Object
readonly
Returns the value of attribute reloading.
-
#templates ⇒ Object
readonly
Returns the value of attribute templates.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
- #default_options ⇒ Object
- #define_error(code, options = {}, &b) ⇒ Object
- #define_event(method, path, options = {}, &b) ⇒ Object
- #define_filter(type, &b) ⇒ Object
- #define_template(name = :layout, &b) ⇒ Object
- #development? ⇒ Boolean
-
#initialize ⇒ Application
constructor
A new instance of Application.
-
#load_default_events! ⇒ Object
Called immediately after the application is initialized or reloaded to register default events.
-
#load_options! ⇒ Object
Load all options given on the command line NOTE: Ignores –name so unit/spec tests can run individually.
-
#lookup(request) ⇒ Object
Visits and invokes each handler registered for the
request_method
in definition order until a Result response is produced. - #mutex ⇒ Object
- #reload! ⇒ Object
- #run_safely ⇒ Object
Constructor Details
#initialize ⇒ Application
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_default_events! end |
Instance Attribute Details
#clearables ⇒ Object (readonly)
Returns the value of attribute clearables.
614 615 616 |
# File 'lib/codebutler/sinatra.rb', line 614 def clearables @clearables end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
613 614 615 |
# File 'lib/codebutler/sinatra.rb', line 613 def errors @errors end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
613 614 615 |
# File 'lib/codebutler/sinatra.rb', line 613 def events @events end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
613 614 615 |
# File 'lib/codebutler/sinatra.rb', line 613 def filters @filters end |
#options ⇒ Object
696 697 698 |
# File 'lib/codebutler/sinatra.rb', line 696 def @options ||= OpenStruct.new() end |
#reloading ⇒ Object (readonly)
Returns the value of attribute reloading.
614 615 616 |
# File 'lib/codebutler/sinatra.rb', line 614 def reloading @reloading end |
#templates ⇒ Object (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_options ⇒ Object
618 619 620 621 622 623 624 625 626 627 |
# File 'lib/codebutler/sinatra.rb', line 618 def self. @@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_options ⇒ Object
629 630 631 |
# File 'lib/codebutler/sinatra.rb', line 629 def self.class. end |
#define_error(code, options = {}, &b) ⇒ Object
673 674 675 |
# File 'lib/codebutler/sinatra.rb', line 673 def define_error(code, = {}, &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, = {}, &b) events[method] << event = Event.new(path, , &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
700 701 702 |
# File 'lib/codebutler/sinatra.rb', line 700 def development? .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 require 'optparse' OptionParser.new do |op| op.on('-p port') { |port| [:port] = port } op.on('-e env') { |env| [:env] = env } op.on('-x') { |env| [: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 |
#mutex ⇒ Object
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_safely ⇒ Object
717 718 719 720 721 722 723 |
# File 'lib/codebutler/sinatra.rb', line 717 def run_safely if .mutex mutex.synchronize { yield } else yield end end |