Class: AdWords::ResponseHandler
- Inherits:
-
SOAP::RPC::CallbackHandler
- Object
- SOAP::RPC::CallbackHandler
- AdWords::ResponseHandler
- Defined in:
- lib/adwords4r.rb
Overview
Handler class to process response messages for API unit usage and statistics information.
Instance Method Summary collapse
-
#initialize(parent) ⇒ ResponseHandler
constructor
Constructor for ResponseHandler.
-
#on_callback(method_name, endpoint, envelope, params, fault = false, fault_msg = nil) ⇒ Object
Handles the callback method.
-
#parse_header(header) ⇒ Object
Parses the value contained in a SOAP response header.
Constructor Details
#initialize(parent) ⇒ ResponseHandler
Constructor for ResponseHandler.
Args:
-
parent: AdWords::API object to which the ResponseHandler should be tied
437 438 439 |
# File 'lib/adwords4r.rb', line 437 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)
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/adwords4r.rb', line 452 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
537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/adwords4r.rb', line 537 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 |