Class: AdWords::ResponseHandler

Inherits:
SOAP::RPC::CallbackHandler show all
Defined in:
lib/adwords4r.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ ResponseHandler

Constructor for ResponseHandler.

Args:

  • parent: AdWords::API object to which the ResponseHandler should be tied



509
510
511
# File 'lib/adwords4r.rb', line 509

def initialize(parent)
  @parent = parent
end

Instance Method Details

#on_callback(method_name, endpoint, envelope, params, fault = false, fault_msg = nil) ⇒ Object

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)



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# 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) ⇒ Object

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



609
610
611
612
613
614
615
616
617
618
619
620
# 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