Class: Goat::App

Inherits:
Object show all
Includes:
AppHelpers, FlashHelper
Defined in:
lib/goat.rb

Direct Known Subclasses

BasicApp

Constant Summary collapse

@@error_handler =
nil
@@not_found_handler =
nil
@@around_handlers =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FlashHelper

#flash

Methods included from AppHelpers

#halt, #redirect, #session

Constructor Details

#initialize(req, meth = nil) ⇒ App

Returns a new instance of App.



654
655
656
657
658
659
# File 'lib/goat.rb', line 654

def initialize(req, meth=nil)
  @req = req
  @meth = meth
  @response = Rack::Response.new
  @params = IndifferentHash.from_hash(req.params)
end

Class Method Details

.after(&blk) ⇒ Object



548
549
550
# File 'lib/goat.rb', line 548

def after(&blk)
  around_handlers << [:after, blk]
end

.around_handlersObject



542
# File 'lib/goat.rb', line 542

def around_handlers; @@around_handlers; end

.before(&blk) ⇒ Object



544
545
546
# File 'lib/goat.rb', line 544

def before(&blk)
  around_handlers << [:before, blk]
end

.bind(hook, name = nil, for_response = true) ⇒ Object



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/goat.rb', line 633

def self.bind(hook, name=nil, for_response=true)
  mname = name || String.random

  lambda do |app, *args|
    kls = app.class

    unless kls.instance_methods.include?(mname)
      Goat.logd "defining #{mname} on #{kls}"
      kls.send(:define_method, mname, hook)
      hook = kls.instance_method(mname)
    end

    if for_response
      # sets the body
      app.respond_with_hook(mname, *args)
    else
      app.send(mname, *args)
    end
  end
end

.call(env) ⇒ Object

end class << self



625
626
627
628
# File 'lib/goat.rb', line 625

def self.call(env)
  self.req_handler.app_class = self
  self.req_handler.handle_request(env)
end

.error(&blk) ⇒ Object



611
612
613
# File 'lib/goat.rb', line 611

def error(&blk)
  @@error_handler = blk
end

.error_handlerObject



615
# File 'lib/goat.rb', line 615

def error_handler; @@error_handler; end

.get(path, hook = nil, &proc) ⇒ Object



528
529
530
531
# File 'lib/goat.rb', line 528

def get(path, hook=nil, &proc)
  hook = proc unless proc.nil?
  req_handler.add_mapping(:method => :get, :path => path, :hook => hook)
end

.handle_rpc(app) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/goat.rb', line 555

def handle_rpc(app)
  req = app.request

  cls = req['cls']
  cid = req['id']
  rpc = req['rpc']
  txn = req['rttxn']
  pgid = req['pgid']
  reqargs = JSON.load(req['args'])

  have_handler = false
  resp = nil

  Goat::NotificationCenter.notify(
    'type' => 'txn_start',
    'txn' => txn,
    'pgid' => pgid
  )

  if comp = Goat.rpc_handlers[cls]
    if comp.include?(rpc)
      Dynamic.let(:txn => txn, :txn_pgid => pgid) do
        have_handler = true
        opts = comp[rpc]
        args = []
        if opts[:live]
          if skel = StateSrvClient.fetch_component(cid)
            component = Kernel.fetch_class(cls).from_skel(skel)
            component.deserialize(skel.live_state)

            args << component
          else
            return respond_failed
          end
        end

        args << IndifferentHash.from_hash(reqargs)

        resp = app.respond_with_hook("rpc_#{rpc}", *args)
        resp = opts[:is_get] ? resp.to_json : [200, {}, '']

        if opts[:live]
          component.update
        end
      end
    end
  end

  Goat::NotificationCenter.notify(
    'type' => 'txn_complete',
    'txn' => txn
  )

  have_handler ? resp : [500, {}, {'success' => false}.to_json]
end

.map(opts) ⇒ Object



538
539
540
# File 'lib/goat.rb', line 538

def map(opts)
  req_handler.add_mapping(opts)
end

.not_found(&blk) ⇒ Object



617
618
619
# File 'lib/goat.rb', line 617

def not_found(&blk)
  @@not_found_handler = blk
end

.not_found_handlerObject



621
# File 'lib/goat.rb', line 621

def not_found_handler; @@not_found_handler; end

.post(path, hook = nil, &proc) ⇒ Object



533
534
535
536
# File 'lib/goat.rb', line 533

def post(path, hook=nil, &proc)
  hook = proc unless proc.nil?
  req_handler.add_mapping(:method => :post, :path => path, :hook => hook)
end

.req_handlerObject



524
525
526
# File 'lib/goat.rb', line 524

def req_handler
  @@reqhandler ||= ReqHandler.new
end

.respond_failedObject



553
# File 'lib/goat.rb', line 553

def respond_failed; [500, {}, ['failed']]; end

.respond_successObject



552
# File 'lib/goat.rb', line 552

def respond_success; [200, {}, ['ok']]; end

Instance Method Details

#erb(name, opts = {}, &blk) ⇒ Object



670
671
672
673
674
# File 'lib/goat.rb', line 670

def erb(name, opts={}, &blk)
  e = ERBRunner.new(@req, @response, @params)
  e.delegate = self
  e.erb(name, {:locals => {:page => nil}}.merge(opts), &blk)
end

#paramsObject



663
# File 'lib/goat.rb', line 663

def params; @params; end

#requestObject



662
# File 'lib/goat.rb', line 662

def request; @req; end

#respond_with_hook(hook, *args) ⇒ Object



665
666
667
668
# File 'lib/goat.rb', line 665

def respond_with_hook(hook, *args)
  response.body = self.send(hook, *args) || ''
  response.finish
end

#responseObject



661
# File 'lib/goat.rb', line 661

def response; @response; end