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 440
440:     def initialize(parent)
441:       @parent = parent
442:     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 455
455:     def on_callback(method_name, endpoint, envelope, params, fault = false,
456:         fault_msg = nil)
457:       units = nil
458:       operations = nil
459:       response_time = nil
460:       request_id = nil
461:       operators = nil
462:       operator_count = nil
463: 
464:       if params and params[0] and params[0].class.to_s =~ /.*::Mutate/
465:         if params[0].is_a?(Array) and params[0].size >= 1
466:           operators = Hash.new(0)
467:           params[0].each do |operation|
468:             if operation.is_a? Hash and operation[:operator]
469:               operators[operation[:operator].to_s] += 1
470:             elsif operation.is_a? Hash and operation['operator']
471:               operators[operation['operator'].to_s] += 1
472:             elsif operation.respond_to? 'operator'
473:               operators[operation.operator.to_s] += 1
474:             else
475:               operators['?'] += 1
476:             end
477:           end
478:         else
479:           if params[0].is_a? Hash and params[0][:operator]
480:             operators[params[0][:operator].to_s] += 1
481:           elsif params[0].is_a? Hash and params[0]['operator']
482:             operators[params[0]['operator'].to_s] += 1
483:           elsif params[0].respond_to? 'operator'
484:             operators[params[0].operator.to_s] += 1
485:           end
486:         end
487:       end
488: 
489:       if operators
490:         operator_count = operators.map { |op, num| "#{op}: #{num}" }.join(', ')
491:       end
492: 
493:       header = envelope.header if envelope
494:       if header and header.key?('ResponseHeader')
495:         header = header['ResponseHeader'].element
496:       end
497: 
498:       if header
499:         @parent.mutex.synchronize do
500:           units = parse_header(header['units'])
501:           unless units.nil?
502:             @parent.last_units = units.to_i
503:             @parent.total_units = @parent.total_units + units.to_i
504:           end
505: 
506:           operations = parse_header(header['operations'])
507:           response_time = parse_header(header['responseTime'])
508:           request_id = parse_header(header['requestId'])
509:         end
510:       end
511: 
512:       host = URI.parse(endpoint).host
513: 
514:       data = "host=#{host} method=#{method_name} " +
515:         "responseTime=#{response_time} operations=#{operations} "
516: 
517:       data += "operators={#{operator_count}} " if operator_count
518: 
519:       data += "units=#{units} requestId=#{request_id} "
520: 
521:       data += "isFault=#{(!!fault).to_s} "
522: 
523:       if fault_msg
524:         data += "faultMessage=\"#{fault_msg}\""
525:       else
526:         data += "faultMessage=none"
527:       end
528: 
529:       @parent.unit_logger << data
530:     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 540
540:     def parse_header(header)
541:       if header.nil?
542:         return nil
543:       end
544: 
545:       header_element = header
546:       if header.instance_variable_defined?('@element')
547:         header_element = header.element
548:       end
549: 
550:       return header_element.text
551:     end