lib/punchblock/translator/asterisk/component/asterisk/agi_command.rb in punchblock-1.2.0 vs lib/punchblock/translator/asterisk/component/asterisk/agi_command.rb in punchblock-1.3.0

- old
+ new

@@ -1,8 +1,7 @@ # encoding: utf-8 -require 'uri' require 'active_support/core_ext/string/filters' module Punchblock module Translator class Asterisk @@ -14,11 +13,12 @@ def setup @action = create_action end def execute - @call.send_ami_action! @action + send_ref + @call.send_ami_action @action end def handle_ami_event(event) pb_logger.debug "Handling AMI event: #{event.inspect}" if event.name == 'AsyncAGI' @@ -27,23 +27,26 @@ send_complete_event success_reason(event) end end end - def parse_agi_result(result) - match = URI::Parser.new.unescape(result).chomp.match(/^(\d{3}) result=(-?\d*) ?(\(?.*\)?)?$/) - if match - data = match[3] ? match[3].gsub(/(^\()|(\)$)/, '') : nil - [match[1].to_i, match[2].to_i, data] + def handle_response(response) + pb_logger.debug "Handling response: #{response.inspect}" + case response + when RubyAMI::Error + set_node_response false + when RubyAMI::Response + send_ref end end private def create_action + command = current_actor RubyAMI::Action.new 'AGI', 'Channel' => @call.channel, 'Command' => agi_command, 'CommandID' => id do |response| - handle_response response + command.handle_response response end end def agi_command "#{@component_node.name} #{@component_node.params_array.map { |arg| quote_arg(arg) }.join(' ')}".squish @@ -53,22 +56,12 @@ # See parse_args in asterisk/res/res_agi.c (Asterisk 1.4.21.1) def quote_arg(arg) '"' + arg.to_s.gsub(/["\\]/) { |m| "\\#{m}" } + '"' end - def handle_response(response) - pb_logger.debug "Handling response: #{response.inspect}" - case response - when RubyAMI::Error - set_node_response false - when RubyAMI::Response - send_ref - end - end - def success_reason(event) - code, result, data = parse_agi_result event['Result'] - Punchblock::Component::Asterisk::AGI::Command::Complete::Success.new :code => code, :result => result, :data => data + parser = RubyAMI::AGIResultParser.new event['Result'] + Punchblock::Component::Asterisk::AGI::Command::Complete::Success.new :code => parser.code, :result => parser.result, :data => parser.data end end end end end