Method: AGI#init

Defined in:
lib/AGI.rb

#initObject

Initializes the channel by getting all variables provided by Asterisk as it initiates the connection. These values are then stored in the instance variable @channel_params, a Hash object. While initializing the channel, the IO object(s) provided to new as :input and :output are set to operate synchronously.

Note, a channel can only be initialized once. If the AGI is being initialized a second time, this will throw an AGIInitializeError. If this is desired functionality, please see the reinit method.



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/AGI.rb', line 428

def init
  if @initialized
    raise AGIInitializeError.new(nil, "Tried to init previously initialized channel. If this is desired, use reinit()")      
  end
  begin
    @input.sync = true
    @output.sync = true
    while( line = @input.gets.chomp )
      if line =~ %r{^\s*$} then
        break
      elsif line =~ %r{^agi_(\w+)\:\s+(.+)$} then
        if @channel_params.has_key?($1) then
          @logger.warn{"AGI Got Duplicate Channel Parameter for #{$1} (was \"#{@channel_params[$1]}\" reset to \"#{$2})\""}
        end
        @channel_params[$1] = $2          
        @logger.debug{"AGI Got Channel Parameter #{$1} = #{$2}"}
      end
    end
  rescue NoMethodError => error
    if error.to_s =~ %r{chomp} then
      raise AGIHangupError.new(nil, "Channel Hungup during init")
    else
      raise
    end
  end
  @initialized = true
end