google-adwords-api 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,121 +0,0 @@
1
- #!/usr/bin/ruby
2
- #
3
- # Authors:: api.sgomes@gmail.com (Sérgio Gomes)
4
- #
5
- # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
6
- #
7
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
8
- # you may not use this file except in compliance with the License.
9
- # You may obtain a copy of the License at
10
- #
11
- # http://www.apache.org/licenses/LICENSE-2.0
12
- #
13
- # Unless required by applicable law or agreed to in writing, software
14
- # distributed under the License is distributed on an "AS IS" BASIS,
15
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16
- # implied.
17
- # See the License for the specific language governing permissions and
18
- # limitations under the License.
19
- #
20
- # Handles callbacks for the AdWords API, so that it's possible to log unit
21
- # consumption and operations performed.
22
-
23
- require 'ads_common/soap4r_response_handler'
24
- require 'uri'
25
-
26
- module AdwordsApi
27
-
28
- # Handler class to process response messages for API unit usage and statistics
29
- # information.
30
- class Soap4rResponseHandler < AdsCommon::Soap4rResponseHandler
31
-
32
- # Handles the callback method.
33
- # Logs the request data and tracks unit usage.
34
- #
35
- # Args:
36
- # - method_name: name for the operation that was invoked
37
- # - endpoint: the enodpoint URL the request was sent to
38
- # - envelope: the envelope for the SOAP response that was received
39
- # - params: the parameters that were passed to the method
40
- # - fault: whether the request resulted in a fault or not
41
- # - fault_msg: the fault message in case of a fault (nil if none)
42
- #
43
- def on_callback(method_name, endpoint, envelope, params, fault = false,
44
- fault_msg = nil)
45
- units = nil
46
- operations = nil
47
- response_time = nil
48
- request_id = nil
49
- operators = nil
50
- operator_count = nil
51
-
52
- # Create a hash with the count per operator used in the request
53
- if params and params[0] and params[0].class.to_s =~ /.*::Mutate/
54
- if params[0].is_a?(Array) and params[0].size >= 1
55
- operators = Hash.new(0)
56
- params[0].each do |operation|
57
- if operation.is_a? Hash and operation[:operator]
58
- operators[operation[:operator].to_s] += 1
59
- elsif operation.is_a? Hash and operation['operator']
60
- operators[operation['operator'].to_s] += 1
61
- elsif operation.respond_to? 'operator'
62
- operators[operation.operator.to_s] += 1
63
- else
64
- operators['?'] += 1
65
- end
66
- end
67
- else
68
- if params[0].is_a? Hash and params[0][:operator]
69
- operators[params[0][:operator].to_s] += 1
70
- elsif params[0].is_a? Hash and params[0]['operator']
71
- operators[params[0]['operator'].to_s] += 1
72
- elsif params[0].respond_to? 'operator'
73
- operators[params[0].operator.to_s] += 1
74
- end
75
- end
76
- end
77
-
78
- if operators
79
- operator_count = operators.map { |op, num| "#{op}: #{num}" }.join(', ')
80
- end
81
-
82
- header = envelope.header if envelope
83
- if header and header.key?('ResponseHeader')
84
- header = header['ResponseHeader'].element
85
- end
86
-
87
- if header
88
- @parent.mutex.synchronize do
89
- units = parse_header(header['units'])
90
- unless units.nil?
91
- @parent.last_units = units.to_i
92
- @parent.total_units = @parent.total_units + units.to_i
93
- end
94
-
95
- operations = parse_header(header['operations'])
96
- response_time = parse_header(header['responseTime'])
97
- request_id = parse_header(header['requestId'])
98
- end
99
- end
100
-
101
- host = URI.parse(endpoint).host
102
-
103
- data = "host=#{host} method=#{method_name} " +
104
- "responseTime=#{response_time} operations=#{operations} "
105
-
106
- data += "operators={#{operator_count}} " if operator_count
107
-
108
- data += "units=#{units} requestId=#{request_id} "
109
-
110
- data += "isFault=#{(!!fault).to_s} "
111
-
112
- if fault_msg
113
- data += "faultMessage=\"#{fault_msg}\""
114
- else
115
- data += "faultMessage=none"
116
- end
117
-
118
- @parent.logger.info(data)
119
- end
120
- end
121
- end