Class: AGI

Child modules and classes

Class AGI::Response

Aliases

Old nameNew nameDescription
env environment
args argv

Attributes

NameRead/write?Description
args R The arguments passed in the Asterisk AGI application, so
    _X,1,AGI(foo.agi|one|two|three)

will give agi.args as ["one","two","three"].

env RW A Hash with the initial environment. Leave off the agi_ prefix
log RW Logger object, defaults to Logger.new(STDERR). By default nothing is logged, but if you turn up the log level to DEBUG you‘ll see the behind-the-scenes communication.

Public Class methods


new (io_in=STDIN, io_out=STDOUT)

Create a new AGI object and parse the Asterisk environment. Usually you will call this without arguments, but you might have your bat-reasons to provide io_in and io_out.

Also sets up a default SIGHUP trap, logging the event and calling exit. If you want to do some cleanup on SIGHUP instead, override it, e.g.:

    trap('SIGHUP') { cleanup }
    # File lib/agi.rb, line 12
12:   def initialize(io_in=STDIN, io_out=STDOUT)
13:     @io_in = io_in
14:     @io_out = io_out
15: 
16:     # read vars
17:     @env = {}
18:     while (line = @io_in.readline.strip) != ''
19:       k,v = line.split(':')
20:       k.strip! if k
21:       v.strip! if v
22:       k = $1 if k =~ /^agi_(.*)$/
23:       @env[k] = v
24:     end
25: 
26:     @log = Logger.new(STDERR)
27: 
28:     @args = ARGV
29: 
30:     # default trap for SIGHUP, which is what Asterisk sends us when the other
31:     # end hangs up. An uncaught SIGHUP exception pollutes STDERR needlessly.
32:     trap('SIGHUP') { @log.debug('Holy SIGHUP, Batman!'); exit }
33:   end

Public Instance methods


[] (key)

Environment access shortcut. Use strings or symbols as keys.

    # File lib/agi.rb, line 51
51:   def [](key)
52:     @env[key.to_s]
53:   end

menu (audio,digits,timeout=3000) {|r| ...}

Repeat this menu followed by the timeout, with the given digits expected. Use break to exit this menu.

    # File lib/agi.rb, line 79
79:   def menu(audio,digits,timeout=3000, &block)
80:     while true # (until the block breaks)
81:       r = self.stream_file(audio,digits)
82: 
83:       if r.result == 0 
84:         # nothing was dialed yet, let's wait timeout milliseconds longer
85:         r = self.wait_for_digit(timeout) 
86:       end
87:       yield(r) # if the block breaks, we go on with life
88:     end
89:   end

method_missing (symbol, *args)

Shortcut for send. e.g.

    a.say_time(Time.now.to_i, nil)

is the same as

    a.send("SAY TIME",Time.now.to_i,'""')
    # File lib/agi.rb, line 72
72:   def method_missing(symbol, *args)
73:     cmd = symbol.to_s.upcase.tr('_',' ')
74:     send(cmd, *args)
75:   end

send (cmd, *args)

Send the given command and arguments. Converts nil and "" in args to literal empty quotes

    # File lib/agi.rb, line 57
57:   def send(cmd, *args)
58:     args.map! {|a| (a.nil? or a == '') ? '""' : a}
59:     msg = [cmd, *args].join(' ')
60:     @log.debug ">> "+msg
61:     @io_out.puts msg
62:     @io_out.flush # I'm not sure if this is necessary, but just in case
63:     resp = @io_in.readline
64:     @log.debug "<< "+resp
65:     Response.new(resp)
66:   end