Sha256: 5337702d5730e2184701d20667619eff3e72489673c5bc0d19ffff6e6835238b

Contents?: true

Size: 1.2 KB

Versions: 19

Compression:

Stored size: 1.2 KB

Contents

# encoding: utf-8

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

module Punchblock
  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}
        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

19 entries across 19 versions & 1 rubygems

Version Path
punchblock-2.6.0 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.5.3 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.5.2 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.5.1 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.5.0 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.4.2 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.4.0 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.3.1 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.3.0 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.2.2 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.2.1 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.2.0 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.1.1 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.1.0 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.0.2 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.0.1 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.0.0 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.0.0.beta2 lib/punchblock/translator/asterisk/agi_command.rb
punchblock-2.0.0.beta1 lib/punchblock/translator/asterisk/agi_command.rb