lib/adhearsion/voip/asterisk/commands.rb in adhearsion-0.8.6 vs lib/adhearsion/voip/asterisk/commands.rb in adhearsion-1.0.0

- old
+ new

@@ -48,32 +48,34 @@ to_pbx.print(message + "\n") end # Utility method to read from pbx. Hangup if nil. def read - returning from_pbx.gets do |message| + from_pbx.gets.tap do |message| # AGI has many conditions that might indicate a hangup raise Hangup if message.nil? ahn_log.agi.debug "<<< #{message}" code, rest = *message.split(' ', 2) - - if code == "511" - # '511' Command Not Permitted on a dead channel - ahn_log.agi.debug "AGI 500 error. Raising hangup" + + case code.to_i + when 510 + # This error is non-fatal for the call + ahn_log.agi.warn "510: Invalid or unknown AGI command" + when 511 + # 511 Command Not Permitted on a dead channel + ahn_log.agi.debug "511: Dead channel. Raising Hangup" raise Hangup + when 520 + # This error is non-fatal for the call + ahn_log.agi.warn "520: Invalid command syntax" + when (500..599) + # Assume this error is non-fatal for the call and try to keep running + ahn_log.agi.warn "#{code}: Unknown AGI protocol error." end - if (500..599) === code.to_i - # 500 AGI protocol error. Catches (at least): - # 520 Invalid command syntax. - # 510 Invalid or unknown command - # If we have hit this then something bad has happened. - ahn_log.agi.warn "AGI 500 error encountered. This may be a bug in Adhearsion. Please report it at http://adhearsion.lighthouseapp.com" - end - # If the message starts with HANGUP it's a silly 1.6 OOB message case message when /^HANGUP/, /^HANGUP\n?$/i, /^HANGUP\s?\d{3}/i ahn_log.agi.debug "AGI HANGUP. Raising hangup" raise Hangup @@ -677,11 +679,37 @@ # # @see http://www.voip-info.org/wiki/view/set+variable Asterisk Set Variable def set_variable(variable_name, value) response("SET VARIABLE", variable_name, value) == "200 result=1" end - + + # Issue the command to add a custom SIP header to the current call channel + # example use: sip_add_header("x-ahn-test", "rubyrox") + # + # @param[String] the name of the SIP header + # @param[String] the value of the SIP header + # + # @return [String] the Asterisk response + # + # @see http://www.voip-info.org/wiki/index.php?page=Asterisk+cmd+SIPAddHeader Asterisk SIPAddHeader + def sip_add_header(header, value) + execute("SIPAddHeader", "#{header}: #{value}") == "200 result=1" + end + + # Issue the command to fetch a SIP header from the current call channel + # example use: sip_get_header("x-ahn-test") + # + # @param[String] the name of the SIP header to get + # + # @return [String] the Asterisk response + # + # @see http://www.voip-info.org/wiki/index.php?page=Asterisk+cmd+SIPGetHeader Asterisk SIPGetHeader + def sip_get_header(header) + get_variable("SIP_HEADER(#{header})") + end + alias :sip_header :sip_get_header + # Allows you to either set or get a channel variable from Asterisk. # The method takes a hash key/value pair if you would like to set a variable # Or a single string with the variable to get from Asterisk def variable(*args) if args.last.kind_of? Hash @@ -1046,11 +1074,11 @@ def from_pbx io end def validate_digits(digits) - returning digits.to_s do |digits_as_string| + digits.to_s.tap do |digits_as_string| raise ArgumentError, "Can only be called with valid digits!" unless digits_as_string =~ /^[0-9*#-]+$/ end end def error?(result) @@ -1224,10 +1252,10 @@ # @param [String] QUEUESTATUS variable from Asterisk # @return [Symbol] Symbolized version of QUEUESTATUS # @raise QueueDoesNotExistError def normalize_queue_status_variable(variable) variable = "UNKNOWN" if variable.nil? - returning variable.downcase.to_sym do |queue_status| + variable.downcase.to_sym.tap do |queue_status| raise QueueDoesNotExistError.new(name) if queue_status == :unknown end end class QueueAgentsListProxy