Handler class to process response messages for API unit usage and statistics information.

Methods
N
O
P
Class Public methods
new(parent)

Constructor for ResponseHandler.

Args:

# File lib/adwords4r.rb, line 509
    def initialize(parent)
      @parent = parent
    end
Instance Public methods
on_callback(method_name, endpoint, envelope, params, fault = false, fault_msg = nil)

Handles the callback method. Logs the request data and tracks unit usage.

Args:

  • method_name: name for the operation that was invoked
  • endpoint: the enodpoint URL the request was sent to
  • envelope: the envelope for the SOAP response that was received
  • params: the parameters that were passed to the method
  • fault: whether the request resulted in a fault or not
  • fault_msg: the fault message in case of a fault (nil if none)
# File lib/adwords4r.rb, line 524
    def on_callback(method_name, endpoint, envelope, params, fault = false,
        fault_msg = nil)
      units = nil
      operations = nil
      response_time = nil
      request_id = nil
      operators = nil
      operator_count = nil

      if params and params[0] and params[0].class.to_s =~ /.*::Mutate/
        if params[0].is_a?(Array) and params[0].size >= 1
          operators = Hash.new(0)
          params[0].each do |operation|
            if operation.is_a? Hash and operation[:operator]
              operators[operation[:operator].to_s] += 1
            elsif operation.is_a? Hash and operation['operator']
              operators[operation['operator'].to_s] += 1
            elsif operation.respond_to? 'operator'
              operators[operation.operator.to_s] += 1
            else
              operators['?'] += 1
            end
          end
        else
          if params[0].is_a? Hash and params[0][:operator]
            operators[params[0][:operator].to_s] += 1
          elsif params[0].is_a? Hash and params[0]['operator']
            operators[params[0]['operator'].to_s] += 1
          elsif params[0].respond_to? 'operator'
            operators[params[0].operator.to_s] += 1
          end
        end
      end

      if operators
        operator_count = operators.map { |op, num| "#{op}: #{num}" }.join(', ')
      end

      header = envelope.header if envelope
      if header and header.key?('ResponseHeader')
        header = header['ResponseHeader'].element
      end

      if header
        @parent.mutex.synchronize do
          units = parse_header(header['units'])
          unless units.nil?
            @parent.last_units = units.to_i
            @parent.total_units = @parent.total_units + units.to_i
          end

          operations = parse_header(header['operations'])
          response_time = parse_header(header['responseTime'])
          request_id = parse_header(header['requestId'])
        end
      end

      host = URI.parse(endpoint).host

      data = "host=#{host} method=#{method_name} " +
        "responseTime=#{response_time} operations=#{operations} "

      data += "operators={#{operator_count}} " if operator_count

      data += "units=#{units} requestId=#{request_id} "

      data += "isFault=#{(!!fault).to_s} "

      if fault_msg
        data += "faultMessage=\"#{fault_msg}\""
      else
        data += "faultMessage=none"
      end

      @parent.unit_logger << data
    end
parse_header(header)

Parses the value contained in a SOAP response header.

Args:

  • header: an object representing a SOAP header

Returns: The value contained in the header as a string, or nil if the header is nil

# File lib/adwords4r.rb, line 609
    def parse_header(header)
      if header.nil?
        return nil
      end

      header_element = header
      if header.instance_variable_defined?('@element')
        header_element = header.element
      end

      return header_element.text
    end