Sha256: 1069a386eddd8b0cc0fea5fda2f813d4b6850150120a76eb93c888a9c51a1678

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

# encoding: utf-8

require 'active_support/core_ext/string/filters'
require 'adhearsion/translator/asterisk/ami_error_converter'

module Adhearsion
  module Translator
    class Asterisk
      class AGICommand
        ARG_QUOTER = /["\\]/.freeze

        attr_reader :id

        def initialize(id, channel, command, *params)
          @id, @channel, @command, @params = id, channel, command, params
        end

        # @raises RubyAMI::Error, ChannelGoneError
        def execute(ami_client)
          AMIErrorConverter.convert { ami_client.send_action 'AGI', 'Channel' => @channel, 'Command' => agi_command, 'CommandID' => id }
        end

        def parse_result(event)
          parser = RubyAMI::AGIResultParser.new event['Result']
          {code: parser.code, result: parser.result, data: parser.data}
        rescue ArgumentError => e
          logger.warn "Illegal message received from Asterisk: #{e.message}"
          {code: -1, result: nil, data: nil}
        end

        private

        def agi_command
          "#{@command} #{@params.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(ARG_QUOTER) { |m| "\\#{m}" } + '"'
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
adhearsion-3.0.0.rc1 lib/adhearsion/translator/asterisk/agi_command.rb
adhearsion-3.0.0.beta2 lib/adhearsion/translator/asterisk/agi_command.rb
adhearsion-3.0.0.beta1 lib/adhearsion/translator/asterisk/agi_command.rb