lib/adhearsion/outbound_call.rb in adhearsion-2.0.1 vs lib/adhearsion/outbound_call.rb in adhearsion-2.1.0

- old
+ new

@@ -5,13 +5,13 @@ attr_reader :dial_command delegate :to, :from, :to => :dial_command, :allow_nil => true class << self - def originate(to, opts = {}) + def originate(to, opts = {}, &controller_block) new.tap do |call| - call.run_router_on_answer + call.execute_controller_or_router_on_answer opts.delete(:controller), &controller_block call.dial to, opts end end end @@ -31,11 +31,12 @@ def reject(*args) end def dial(to, options = {}) - options.merge! :to => to + options = options.dup + options[:to] = to if options[:timeout] wait_timeout = options[:timeout] options[:timeout] = options[:timeout] * 1000 else wait_timeout = 60 @@ -53,19 +54,28 @@ dispatcher.call current_actor end end def run_router_on_answer - register_event_handler :class => Punchblock::Event::Answered do |event| + register_event_handler Punchblock::Event::Answered do |event| run_router throw :pass end end def on_answer(&block) - register_event_handler :class => Punchblock::Event::Answered do |event| + register_event_handler Punchblock::Event::Answered do |event| block.call event throw :pass + end + end + + def execute_controller_or_router_on_answer(controller, &controller_block) + if controller || controller_block + route = Router::Route.new 'inbound', controller, &controller_block + on_answer { route.dispatch current_actor } + else + run_router_on_answer end end end end