lib/punchblock/translator/asterisk/component/asterisk/agi_command.rb in punchblock-0.7.1 vs lib/punchblock/translator/asterisk/component/asterisk/agi_command.rb in punchblock-0.7.2

- old
+ new

@@ -1,20 +1,18 @@ require 'uri' +require 'active_support/core_ext/string/filters' module Punchblock module Translator class Asterisk module Component module Asterisk class AGICommand < Component attr_reader :action - def initialize(component_node, call) - @component_node, @call = component_node, call - @id = UUIDTools::UUID.random_create.to_s + def setup @action = create_action - pb_logger.debug "Starting up..." end def execute @call.send_ami_action! @action end @@ -38,44 +36,37 @@ end private def create_action - RubyAMI::Action.new 'AGI', 'Channel' => @call.channel, 'Command' => @component_node.name, 'CommandID' => id do |response| + RubyAMI::Action.new 'AGI', 'Channel' => @call.channel, 'Command' => agi_command, 'CommandID' => id do |response| handle_response response end end + def agi_command + "#{@component_node.name} #{@component_node.params_array.map { |arg| quote_arg(arg) }.join(' ')}".squish + end + + # Arguments surrounded by quotes; quotes backslash-escaped. + # 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 - set_node_response Ref.new :id => id + send_ref end end - def set_node_response(value) - pb_logger.debug "Setting response on component node to #{value}" - @component_node.response = value - 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 - end - - def complete_event(reason) - Punchblock::Event::Complete.new.tap do |c| - c.reason = reason - end - end - - def send_event(event) - event.component_id = id - pb_logger.debug "Sending event #{event.inspect}" - @component_node.add_event event end end end end end