google-adwords-api 0.13.0 → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. data/ChangeLog +4 -0
  2. data/README.md +5 -5
  3. data/adwords_api.yml +1 -9
  4. data/examples/v201402/advanced_operations/add_places_location_extensions.rb +0 -0
  5. data/examples/v201406/advanced_operations/add_ad_customizer.rb +277 -0
  6. data/examples/v201406/advanced_operations/add_places_location_extensions.rb +0 -0
  7. data/examples/v201406/advanced_operations/add_text_ad_with_upgraded_urls.rb +0 -0
  8. data/examples/v201406/campaign_management/add_campaign_labels.rb +0 -0
  9. data/examples/v201406/campaign_management/get_campaigns_by_label.rb +0 -0
  10. data/examples/v201406/misc/create_ad_words_session_without_properties_file.rb +0 -0
  11. data/examples/v201406/shopping_campaigns/add_product_partition_tree.rb +0 -0
  12. data/examples/v201406/shopping_campaigns/add_product_scope.rb +0 -0
  13. data/examples/v201406/shopping_campaigns/add_shopping_campaign.rb +0 -0
  14. data/examples/v201406/shopping_campaigns/get_product_category_taxonomy.rb +0 -0
  15. data/examples/v201406/shopping_campaigns/set_product_sales_channel.rb +0 -0
  16. data/lib/adwords_api.rb +0 -7
  17. data/lib/adwords_api/api_config.rb +5 -7
  18. data/lib/adwords_api/credential_handler.rb +0 -5
  19. data/lib/adwords_api/v201402/ad_group_ad_service.rb +1 -1
  20. data/lib/adwords_api/v201402/ad_group_ad_service_registry.rb +1 -1
  21. data/lib/adwords_api/v201402/ad_group_bid_modifier_service.rb +1 -1
  22. data/lib/adwords_api/v201402/ad_group_bid_modifier_service_registry.rb +1 -1
  23. data/lib/adwords_api/v201402/ad_group_criterion_service.rb +1 -1
  24. data/lib/adwords_api/v201402/ad_group_criterion_service_registry.rb +2 -2
  25. data/lib/adwords_api/v201402/ad_group_feed_service.rb +1 -1
  26. data/lib/adwords_api/v201402/ad_group_feed_service_registry.rb +1 -1
  27. data/lib/adwords_api/v201402/ad_group_service.rb +1 -1
  28. data/lib/adwords_api/v201402/ad_group_service_registry.rb +1 -1
  29. data/lib/adwords_api/v201402/ad_param_service.rb +1 -1
  30. data/lib/adwords_api/v201402/ad_param_service_registry.rb +1 -1
  31. data/lib/adwords_api/v201402/adwords_user_list_service.rb +1 -1
  32. data/lib/adwords_api/v201402/adwords_user_list_service_registry.rb +2 -2
  33. data/lib/adwords_api/v201402/alert_service.rb +1 -1
  34. data/lib/adwords_api/v201402/alert_service_registry.rb +1 -1
  35. data/lib/adwords_api/v201402/bidding_strategy_service.rb +1 -1
  36. data/lib/adwords_api/v201402/bidding_strategy_service_registry.rb +1 -1
  37. data/lib/adwords_api/v201402/budget_order_service.rb +1 -1
  38. data/lib/adwords_api/v201402/budget_order_service_registry.rb +2 -2
  39. data/lib/adwords_api/v201402/budget_service.rb +1 -1
  40. data/lib/adwords_api/v201402/budget_service_registry.rb +1 -1
  41. data/lib/adwords_api/v201402/campaign_ad_extension_service.rb +1 -1
  42. data/lib/adwords_api/v201402/campaign_ad_extension_service_registry.rb +1 -1
  43. data/lib/adwords_api/v201402/campaign_criterion_service.rb +1 -1
  44. data/lib/adwords_api/v201402/campaign_criterion_service_registry.rb +1 -1
  45. data/lib/adwords_api/v201402/campaign_feed_service.rb +1 -1
  46. data/lib/adwords_api/v201402/campaign_feed_service_registry.rb +1 -1
  47. data/lib/adwords_api/v201402/campaign_service.rb +1 -1
  48. data/lib/adwords_api/v201402/campaign_service_registry.rb +2 -2
  49. data/lib/adwords_api/v201402/campaign_shared_set_service.rb +1 -1
  50. data/lib/adwords_api/v201402/campaign_shared_set_service_registry.rb +1 -1
  51. data/lib/adwords_api/v201402/constant_data_service.rb +1 -1
  52. data/lib/adwords_api/v201402/constant_data_service_registry.rb +1 -1
  53. data/lib/adwords_api/v201402/conversion_tracker_service.rb +1 -1
  54. data/lib/adwords_api/v201402/conversion_tracker_service_registry.rb +1 -1
  55. data/lib/adwords_api/v201402/customer_feed_service.rb +1 -1
  56. data/lib/adwords_api/v201402/customer_feed_service_registry.rb +1 -1
  57. data/lib/adwords_api/v201402/customer_service.rb +1 -1
  58. data/lib/adwords_api/v201402/customer_service_registry.rb +1 -1
  59. data/lib/adwords_api/v201402/customer_sync_service.rb +1 -1
  60. data/lib/adwords_api/v201402/customer_sync_service_registry.rb +1 -1
  61. data/lib/adwords_api/v201402/data_service.rb +1 -1
  62. data/lib/adwords_api/v201402/data_service_registry.rb +1 -1
  63. data/lib/adwords_api/v201402/experiment_service.rb +1 -1
  64. data/lib/adwords_api/v201402/experiment_service_registry.rb +1 -1
  65. data/lib/adwords_api/v201402/feed_item_service.rb +1 -1
  66. data/lib/adwords_api/v201402/feed_item_service_registry.rb +2 -2
  67. data/lib/adwords_api/v201402/feed_mapping_service.rb +1 -1
  68. data/lib/adwords_api/v201402/feed_mapping_service_registry.rb +1 -1
  69. data/lib/adwords_api/v201402/feed_service.rb +1 -1
  70. data/lib/adwords_api/v201402/feed_service_registry.rb +1 -1
  71. data/lib/adwords_api/v201402/geo_location_service.rb +1 -1
  72. data/lib/adwords_api/v201402/geo_location_service_registry.rb +1 -1
  73. data/lib/adwords_api/v201402/location_criterion_service.rb +1 -1
  74. data/lib/adwords_api/v201402/location_criterion_service_registry.rb +1 -1
  75. data/lib/adwords_api/v201402/managed_customer_service.rb +1 -1
  76. data/lib/adwords_api/v201402/managed_customer_service_registry.rb +1 -1
  77. data/lib/adwords_api/v201402/media_service.rb +1 -1
  78. data/lib/adwords_api/v201402/media_service_registry.rb +1 -1
  79. data/lib/adwords_api/v201402/mutate_job_service.rb +1 -1
  80. data/lib/adwords_api/v201402/mutate_job_service_registry.rb +2 -2
  81. data/lib/adwords_api/v201402/offline_conversion_feed_service.rb +1 -1
  82. data/lib/adwords_api/v201402/offline_conversion_feed_service_registry.rb +1 -1
  83. data/lib/adwords_api/v201402/report_definition_service.rb +1 -1
  84. data/lib/adwords_api/v201402/report_definition_service_registry.rb +1 -1
  85. data/lib/adwords_api/v201402/shared_criterion_service.rb +1 -1
  86. data/lib/adwords_api/v201402/shared_criterion_service_registry.rb +1 -1
  87. data/lib/adwords_api/v201402/shared_set_service.rb +1 -1
  88. data/lib/adwords_api/v201402/shared_set_service_registry.rb +1 -1
  89. data/lib/adwords_api/v201402/targeting_idea_service.rb +1 -1
  90. data/lib/adwords_api/v201402/targeting_idea_service_registry.rb +2 -2
  91. data/lib/adwords_api/v201402/traffic_estimator_service.rb +1 -1
  92. data/lib/adwords_api/v201402/traffic_estimator_service_registry.rb +2 -2
  93. data/lib/adwords_api/v201406/ad_group_ad_service.rb +1 -1
  94. data/lib/adwords_api/v201406/ad_group_ad_service_registry.rb +1 -1
  95. data/lib/adwords_api/v201406/ad_group_bid_modifier_service.rb +1 -1
  96. data/lib/adwords_api/v201406/ad_group_bid_modifier_service_registry.rb +1 -1
  97. data/lib/adwords_api/v201406/ad_group_criterion_service.rb +1 -1
  98. data/lib/adwords_api/v201406/ad_group_criterion_service_registry.rb +1 -1
  99. data/lib/adwords_api/v201406/ad_group_feed_service.rb +1 -1
  100. data/lib/adwords_api/v201406/ad_group_feed_service_registry.rb +1 -1
  101. data/lib/adwords_api/v201406/ad_group_service.rb +1 -1
  102. data/lib/adwords_api/v201406/ad_group_service_registry.rb +1 -1
  103. data/lib/adwords_api/v201406/ad_param_service.rb +1 -1
  104. data/lib/adwords_api/v201406/ad_param_service_registry.rb +1 -1
  105. data/lib/adwords_api/v201406/adwords_user_list_service.rb +1 -1
  106. data/lib/adwords_api/v201406/adwords_user_list_service_registry.rb +1 -1
  107. data/lib/adwords_api/v201406/alert_service.rb +1 -1
  108. data/lib/adwords_api/v201406/alert_service_registry.rb +1 -1
  109. data/lib/adwords_api/v201406/bidding_strategy_service.rb +1 -1
  110. data/lib/adwords_api/v201406/bidding_strategy_service_registry.rb +1 -1
  111. data/lib/adwords_api/v201406/budget_order_service.rb +1 -1
  112. data/lib/adwords_api/v201406/budget_order_service_registry.rb +1 -1
  113. data/lib/adwords_api/v201406/budget_service.rb +1 -1
  114. data/lib/adwords_api/v201406/budget_service_registry.rb +1 -1
  115. data/lib/adwords_api/v201406/campaign_ad_extension_service.rb +1 -1
  116. data/lib/adwords_api/v201406/campaign_ad_extension_service_registry.rb +1 -1
  117. data/lib/adwords_api/v201406/campaign_criterion_service.rb +1 -1
  118. data/lib/adwords_api/v201406/campaign_criterion_service_registry.rb +1 -1
  119. data/lib/adwords_api/v201406/campaign_feed_service.rb +1 -1
  120. data/lib/adwords_api/v201406/campaign_feed_service_registry.rb +1 -1
  121. data/lib/adwords_api/v201406/campaign_service.rb +1 -1
  122. data/lib/adwords_api/v201406/campaign_service_registry.rb +2 -2
  123. data/lib/adwords_api/v201406/campaign_shared_set_service.rb +1 -1
  124. data/lib/adwords_api/v201406/campaign_shared_set_service_registry.rb +1 -1
  125. data/lib/adwords_api/v201406/constant_data_service.rb +1 -1
  126. data/lib/adwords_api/v201406/constant_data_service_registry.rb +1 -1
  127. data/lib/adwords_api/v201406/conversion_tracker_service.rb +1 -1
  128. data/lib/adwords_api/v201406/conversion_tracker_service_registry.rb +1 -1
  129. data/lib/adwords_api/v201406/customer_feed_service.rb +1 -1
  130. data/lib/adwords_api/v201406/customer_feed_service_registry.rb +1 -1
  131. data/lib/adwords_api/v201406/customer_service.rb +1 -1
  132. data/lib/adwords_api/v201406/customer_service_registry.rb +1 -1
  133. data/lib/adwords_api/v201406/customer_sync_service.rb +1 -1
  134. data/lib/adwords_api/v201406/customer_sync_service_registry.rb +1 -1
  135. data/lib/adwords_api/v201406/data_service.rb +1 -1
  136. data/lib/adwords_api/v201406/data_service_registry.rb +1 -1
  137. data/lib/adwords_api/v201406/experiment_service.rb +1 -1
  138. data/lib/adwords_api/v201406/experiment_service_registry.rb +1 -1
  139. data/lib/adwords_api/v201406/feed_item_service.rb +1 -1
  140. data/lib/adwords_api/v201406/feed_item_service_registry.rb +1 -1
  141. data/lib/adwords_api/v201406/feed_mapping_service.rb +1 -1
  142. data/lib/adwords_api/v201406/feed_mapping_service_registry.rb +1 -1
  143. data/lib/adwords_api/v201406/feed_service.rb +1 -1
  144. data/lib/adwords_api/v201406/feed_service_registry.rb +1 -1
  145. data/lib/adwords_api/v201406/geo_location_service.rb +1 -1
  146. data/lib/adwords_api/v201406/geo_location_service_registry.rb +1 -1
  147. data/lib/adwords_api/v201406/label_service.rb +1 -1
  148. data/lib/adwords_api/v201406/label_service_registry.rb +1 -1
  149. data/lib/adwords_api/v201406/location_criterion_service.rb +1 -1
  150. data/lib/adwords_api/v201406/location_criterion_service_registry.rb +1 -1
  151. data/lib/adwords_api/v201406/managed_customer_service.rb +1 -1
  152. data/lib/adwords_api/v201406/managed_customer_service_registry.rb +1 -1
  153. data/lib/adwords_api/v201406/media_service.rb +1 -1
  154. data/lib/adwords_api/v201406/media_service_registry.rb +1 -1
  155. data/lib/adwords_api/v201406/mutate_job_service.rb +1 -1
  156. data/lib/adwords_api/v201406/mutate_job_service_registry.rb +1 -1
  157. data/lib/adwords_api/v201406/offline_conversion_feed_service.rb +1 -1
  158. data/lib/adwords_api/v201406/offline_conversion_feed_service_registry.rb +1 -1
  159. data/lib/adwords_api/v201406/report_definition_service.rb +1 -1
  160. data/lib/adwords_api/v201406/report_definition_service_registry.rb +1 -1
  161. data/lib/adwords_api/v201406/shared_criterion_service.rb +1 -1
  162. data/lib/adwords_api/v201406/shared_criterion_service_registry.rb +1 -1
  163. data/lib/adwords_api/v201406/shared_set_service.rb +1 -1
  164. data/lib/adwords_api/v201406/shared_set_service_registry.rb +1 -1
  165. data/lib/adwords_api/v201406/targeting_idea_service.rb +1 -1
  166. data/lib/adwords_api/v201406/targeting_idea_service_registry.rb +1 -1
  167. data/lib/adwords_api/v201406/traffic_estimator_service.rb +1 -1
  168. data/lib/adwords_api/v201406/traffic_estimator_service_registry.rb +1 -1
  169. data/lib/adwords_api/version.rb +1 -1
  170. metadata +167 -171
  171. checksums.yaml +0 -15
  172. data/examples/v201402/basic_operations/delete_ad_group.rb~ +0 -104
  173. data/examples/v201402/basic_operations/delete_campaign.rb~ +0 -91
  174. data/examples/v201406/basic_operations/remove_ad_group.rb~ +0 -90
  175. data/examples/v201406/basic_operations/remove_campaign.rb~ +0 -111
  176. data/lib/adwords_api/client_login_header_handler.rb +0 -37
  177. data/lib/adwords_api/report_utils.rb~ +0 -283
  178. data/lib/adwords_api/version.rb~ +0 -26
  179. data/test/adwords_api/test_adwords_api.rb~ +0 -114
  180. data/test/adwords_api/test_report_utils.rb~ +0 -216
@@ -1,283 +0,0 @@
1
- # Encoding: utf-8
2
- #
3
- # Author:: api.dklimkin@gmail.com (Danial Klimkin)
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
- # Contains utility methods specific to reporting.
21
-
22
- require 'cgi'
23
- require 'gyoku'
24
- require 'nori'
25
-
26
- require 'ads_common/http'
27
- require 'adwords_api/errors'
28
- require 'adwords_api/report_header_handler'
29
-
30
- module AdwordsApi
31
- class ReportUtils
32
- # Default constructor.
33
- #
34
- # Args:
35
- # - api: AdwordsApi object
36
- # - version: API version to use
37
- #
38
- def initialize(api, version)
39
- @api, @version = api, version
40
- end
41
-
42
- # Downloads and returns a report.
43
- #
44
- # Args:
45
- # - report_definition: definition of the report in XML text or hash
46
- # - cid: optional customer ID to run against
47
- #
48
- # Returns:
49
- # - report body
50
- #
51
- # Raises:
52
- # - AdwordsApi::Errors::InvalidReportDefinitionError if the report
53
- # definition is invalid
54
- # - AdwordsApi::Errors::ReportError if server-side error occurred
55
- #
56
- def download_report(report_definition, cid = nil)
57
- return get_report_response(report_definition, cid).body
58
- end
59
-
60
- # Downloads a report and saves it to a file.
61
- #
62
- # Args:
63
- # - report_definition: definition of the report in XML text or hash
64
- # - path: path to save report to
65
- # - cid: optional customer ID to run against
66
- #
67
- # Returns:
68
- # - nil
69
- #
70
- # Raises:
71
- # - AdwordsApi::Errors::InvalidReportDefinitionError if the report
72
- # definition is invalid
73
- # - AdwordsApi::Errors::ReportError if server-side error occurred
74
- #
75
- def download_report_as_file(report_definition, path, cid = nil)
76
- report_body = download_report(report_definition, cid)
77
- save_to_file(report_body, path)
78
- return nil
79
- end
80
-
81
- # Downloads and returns a report with AWQL.
82
- #
83
- # Args:
84
- # - report_query: query for the report as string
85
- # - format: format for the report as string
86
- # - cid: optional customer ID to run report against
87
- #
88
- # Returns:
89
- # - report body
90
- #
91
- # Raises:
92
- # - AdwordsApi::Errors::ReportError if a server-side error has occurred
93
- #
94
- def download_report_with_awql(report_query, format, cid = nil)
95
- return get_report_response_with_awql(report_query, format, cid).body
96
- end
97
-
98
- # Downloads a report with AWQL and saves it to a file.
99
- #
100
- # Args:
101
- # - report_query: query for the report as string
102
- # - format: format for the report as string
103
- # - path: path to save report to
104
- # - cid: optional customer ID to run report against
105
- #
106
- # Returns:
107
- # - nil
108
- #
109
- # Raises:
110
- # - AdwordsApi::Errors::ReportError if server-side error occurred
111
- #
112
- def download_report_as_file_with_awql(report_query, format, path, cid = nil)
113
- report_body = download_report_with_awql(report_query, format, cid)
114
- save_to_file(report_body, path)
115
- return nil
116
- end
117
-
118
- private
119
-
120
- # Minimal set of required fields for report definition.
121
- REQUIRED_FIELDS = [:selector, :report_name, :report_type, :date_range_type]
122
-
123
- # Definition fields have to be in particular order in the XML. Here is its
124
- # specification.
125
- REPORT_DEFINITION_ORDER = {
126
- :root => [:selector, :report_name, :report_type, :date_range_type,
127
- :download_format, :include_zero_impressions],
128
- :selector => [:fields, :predicates, :date_range, :ordering, :paging],
129
- :predicates => [:field, :operator, :values],
130
- :ordering => [:field, :sort_order],
131
- :paging => [:start_index, :number_results],
132
- :date_range => [:min, :max]
133
- }
134
-
135
- # Send POST request for a report and returns Response object.
136
- def get_report_response(report_definition, cid)
137
- definition_text = get_report_definition_text(report_definition)
138
- data = '__rdxml=%s' % CGI.escape(definition_text)
139
- return make_adhoc_request(data, cid)
140
- end
141
-
142
- # Send POST request for a report with AWQL and returns Response object.
143
- def get_report_response_with_awql(report_query, format, cid)
144
- data = '__rdquery=%s&__fmt=%s' %
145
- [CGI.escape(report_query), CGI.escape(format)]
146
- return make_adhoc_request(data, cid)
147
- end
148
-
149
- # Makes request and AdHoc service and returns response.
150
- def make_adhoc_request(data, cid)
151
- url = @api.api_config.adhoc_report_download_url(
152
- @api.config.read('service.environment'), @version)
153
- headers = get_report_request_headers(url, cid)
154
- log_request(url, headers, data)
155
- response = AdsCommon::Http.post_response(url, data, @api.config, headers)
156
- check_for_errors(response)
157
- return response
158
- end
159
-
160
- # Converts passed object to XML text. Currently support String (no changes)
161
- # and Hash (renders XML).
162
- def get_report_definition_text(report_definition)
163
- return case report_definition
164
- when String then report_definition
165
- when Hash then report_definition_to_xml(report_definition)
166
- else
167
- raise AdwordsApi::Errors::InvalidReportDefinitionError,
168
- 'Unknown object for report definition: %s' %
169
- report_definition.class
170
- end
171
- end
172
-
173
- # Prepares headers for report request.
174
- def get_report_request_headers(url, cid)
175
- @header_handler ||= AdwordsApi::ReportHeaderHandler.new(
176
- @api.credential_handler, @api.get_auth_handler(), @api.config)
177
- return @header_handler.headers(url, cid)
178
- end
179
-
180
- # Saves raw data to a file.
181
- def save_to_file(data, path)
182
- open(path, 'wb') { |file| file.write(data) } if path
183
- end
184
-
185
- # Logs the request on debug level.
186
- def log_request(url, headers, body)
187
- logger = @api.logger
188
- logger.debug("Report request to: '%s'" % url)
189
- logger.debug('HTTP headers: [%s]' %
190
- (headers.map { |k, v| [k, v].join(': ') }.join(', ')))
191
- logger.debug(body)
192
- end
193
-
194
- # Checks downloaded data for error signature. Raises ReportError if it
195
- # detects an error.
196
- def check_for_errors(response)
197
- # Check for error code.
198
- if response.code != 200
199
- # Check for error in body.
200
- report_body = response.body
201
- check_for_xml_error(report_body, response.code)
202
- # No XML error found nor raised, falling back to a default message.
203
- raise AdwordsApi::Errors::ReportError.new(response.code,
204
- 'HTTP code: %d, body: %s' % [response.code, response.body])
205
- end
206
- return nil
207
- end
208
-
209
- # Checks for an XML error in the response body and raises an exception if
210
- # it was found.
211
- def check_for_xml_error(report_body, response_code)
212
- error_response = Nori.parse(report_body)
213
- if error_response.include?(:report_download_error) and
214
- error_response[:report_download_error].include?(:api_error)
215
- api_error = error_response[:report_download_error][:api_error]
216
- raise AdwordsApi::Errors::ReportXmlError.new(response_code,
217
- api_error[:type], api_error[:trigger], api_error[:field_path])
218
- end
219
- end
220
-
221
- # Renders a report definition hash into XML text.
222
- def report_definition_to_xml(report_definition)
223
- check_report_definition_hash(report_definition)
224
- add_report_definition_hash_order(report_definition)
225
- return Gyoku.xml({:report_definition => report_definition})
226
- end
227
-
228
- # Checks if the report definition looks correct.
229
- def check_report_definition_hash(report_definition)
230
- # Minimal set of fields required.
231
- REQUIRED_FIELDS.each do |field|
232
- unless report_definition.include?(field)
233
- raise AdwordsApi::Errors::InvalidReportDefinitionError,
234
- "Required field '%s' is missing in the definition" % field
235
- end
236
- end
237
- # Fields list is also required.
238
- unless report_definition[:selector].include?(:fields)
239
- raise AdwordsApi::Errors::InvalidReportDefinitionError,
240
- 'Fields list is required'
241
- end
242
- # 'Fields' must be an Array.
243
- unless report_definition[:selector][:fields].kind_of?(Array)
244
- raise AdwordsApi::Errors::InvalidReportDefinitionError,
245
- 'Fields list must be an array'
246
- end
247
- # We should request at least one field.
248
- if report_definition[:selector][:fields].empty?
249
- raise AdwordsApi::Errors::InvalidReportDefinitionError,
250
- 'At least one field needs to be requested'
251
- end
252
- # returnMoneyInMicros option is no longer supported as of v201406
253
- if report_definition.include?(:return_money_in_micros) &&
254
- @version != :v201402
255
- raise AdwordsApi::Errors::InvalidReportDefinitionError,
256
- 'returnMoneyInMicros was sunset as of v201406. ' +
257
- 'See the migration guide for info:' +
258
- 'https://developers.google.com/adwords/api/docs/' +
259
- 'guides/migration/v201406'
260
- end
261
- end
262
-
263
- # Adds fields order hint to generator based on specification.
264
- def add_report_definition_hash_order(node, name = :root)
265
- def_order = REPORT_DEFINITION_ORDER[name]
266
- var_order = def_order.reject { |field| !node.include?(field) }
267
- node.keys.each do |key|
268
- if REPORT_DEFINITION_ORDER.include?(key)
269
- case node[key]
270
- when Hash
271
- add_report_definition_hash_order(node[key], key)
272
- when Array
273
- node[key].each do |item|
274
- add_report_definition_hash_order(item, key)
275
- end
276
- end
277
- end
278
- end
279
- node[:order!] = var_order
280
- return nil
281
- end
282
- end
283
- end
@@ -1,26 +0,0 @@
1
- # Encoding: utf-8
2
- #
3
- # Authors:: api.dklimkin@gmail.com (Danial Klimkin)
4
- #
5
- # Copyright:: Copyright 2012, 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
- # Module to keep the current library version.
21
-
22
- module AdwordsApi
23
- module ApiConfig
24
- CLIENT_LIB_VERSION = '0.12.1'
25
- end
26
- end
@@ -1,114 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
- #
6
- # Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
7
- #
8
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
9
- # you may not use this file except in compliance with the License.
10
- # You may obtain a copy of the License at
11
- #
12
- # http://www.apache.org/licenses/LICENSE-2.0
13
- #
14
- # Unless required by applicable law or agreed to in writing, software
15
- # distributed under the License is distributed on an "AS IS" BASIS,
16
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17
- # implied.
18
- # See the License for the specific language governing permissions and
19
- # limitations under the License.
20
- #
21
- # Tests the AdWords API class features.
22
-
23
- require 'test/unit'
24
-
25
- require 'ads_common/config'
26
- require 'ads_common/api_config'
27
- require 'adwords_api'
28
-
29
-
30
- class LoggerStub
31
- attr_reader :last_warning
32
- def warn(message)
33
- @last_warning = message
34
- end
35
- end
36
-
37
- class TestAdwordsApi < Test::Unit::TestCase
38
-
39
- API_VERSION = :v201406
40
-
41
- def setup()
42
- @logger = LoggerStub.new
43
- end
44
-
45
- def test_initialize()
46
- assert_nothing_raised do
47
- adwords_api = AdwordsApi::Api.new({})
48
- end
49
- end
50
-
51
- def test_api_config()
52
- assert_nothing_raised do
53
- adwords_api = AdwordsApi::Api.new({})
54
- api_config = adwords_api.api_config()
55
- assert_not_nil(api_config)
56
- assert_kind_of(AdsCommon::ApiConfig, api_config)
57
- end
58
- end
59
-
60
- def test_use_mcc()
61
- adwords_api = AdwordsApi::Api.new({})
62
- adwords_api.use_mcc = false
63
- assert(!adwords_api.use_mcc)
64
- adwords_api.use_mcc do
65
- assert(adwords_api.use_mcc)
66
- end
67
- end
68
-
69
- def test_validate_only()
70
- adwords_api = AdwordsApi::Api.new({})
71
- adwords_api.validate_only = false
72
- assert(!adwords_api.validate_only)
73
- adwords_api.validate_only do
74
- assert(adwords_api.validate_only)
75
- end
76
- end
77
-
78
- def test_partial_failure()
79
- adwords_api = AdwordsApi::Api.new({})
80
- adwords_api.partial_failure = false
81
- assert(!adwords_api.partial_failure)
82
- adwords_api.partial_failure do
83
- assert(adwords_api.partial_failure)
84
- end
85
- end
86
-
87
- def test_no_sandbox_env()
88
- adwords_api = AdwordsApi::Api.new({
89
- :service => {:environment => 'SANDBOX'}
90
- })
91
- assert_raise(AdsCommon::Errors::Error) do
92
- service = adwords_api.service(:ManagedCustomerService, API_VERSION)
93
- end
94
- end
95
-
96
- def test_prod_env()
97
- adwords_api = AdwordsApi::Api.new({
98
- :library => {:logger => @logger},
99
- :service => {:environment => 'PRODUCTION'}
100
- })
101
- service = adwords_api.service(:ManagedCustomerService, API_VERSION)
102
- end
103
-
104
- def test_clientlogin_error()
105
- adwords_api = AdwordsApi::Api.new({
106
- :library => {:logger => @logger},
107
- :authentication => {:method => 'ClientLogin'},
108
- :service => {:environment => 'PRODUCTION'}
109
- })
110
- assert_raise do
111
- service = adwords_api.service(:CampaignService, API_VERSION)
112
- end
113
- end
114
- end
@@ -1,216 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
- #
6
- # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
7
- #
8
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
9
- # you may not use this file except in compliance with the License.
10
- # You may obtain a copy of the License at
11
- #
12
- # http://www.apache.org/licenses/LICENSE-2.0
13
- #
14
- # Unless required by applicable law or agreed to in writing, software
15
- # distributed under the License is distributed on an "AS IS" BASIS,
16
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17
- # implied.
18
- # See the License for the specific language governing permissions and
19
- # limitations under the License.
20
- #
21
- # Tests report utils.
22
-
23
- require 'test/unit'
24
-
25
- require 'adwords_api'
26
-
27
- # Overriding default access levels to public for tests.
28
- module AdwordsApi
29
- class ReportUtils
30
- public :check_for_errors
31
- public :check_for_xml_error
32
- public :add_report_definition_hash_order
33
- public :check_report_definition_hash
34
- end
35
- end
36
-
37
- # Stub class for HTTP response.
38
- class ResponseStub
39
- attr_reader :code
40
- attr_reader :body
41
-
42
- def initialize(code, body)
43
- @code, @body = code, body
44
- end
45
- end
46
-
47
- XML_REPLY = {
48
- :reply => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><reportDownloadError><ApiError><type>ReportDefinitionError.INVALID_FIELD_NAME_FOR_REPORT</type><trigger>foo</trigger><fieldPath>bar</fieldPath></ApiError></reportDownloadError>',
49
- :type => 'ReportDefinitionError.INVALID_FIELD_NAME_FOR_REPORT',
50
- :trigger => 'foo',
51
- :field_path => 'bar'
52
- }
53
-
54
- VALID_REPORT = '"Custom ADGROUP_PERFORMANCE_REPORT (Oct 20, 2011-Oct 26, 2011)"\nCampaign ID,Ad group ID,Impressions,Clicks,Cost\nTotal, --,0,0,0.00'
55
- GZIPPED_REPORT = "\x1F\x8B\b\x00\x00\x00\x00\x00\x00\x00Sr.-.\xC9\xCFUptq\x0F\xF2\x0F\r\x88\x0Fp\rr\xF3\x0F\xF2u\xF4sv\x8D\x0Fr\r\xF0\x0F\nQ\xD0pIMV0\xD2Q0204\xD4\x05\xB1- lM%.\xE7\xC4\xDC\x82\xC4\xCC\xF4<\x05O\x17\x1D\xC7\x14\x85\xF4\xA2\xFC\xD2\x02\x10\xDB3\xB7\xA0(\xB5\xB883?\xAFX\xC79'39\eH\xE5\x17\x97p\x85\xE4\x97$\xE6\xE8(\xE8\xEA\xEA\x18\x80\xA0\x9E\x81\x01\x17\x00\xBE\x1D\xBE\xAD\x81\x00\x00\x00"
56
-
57
- class TestReportUtils < Test::Unit::TestCase
58
-
59
- API_VERSION = :v201406
60
-
61
- # Initialize tests.
62
- def setup()
63
- @api = AdwordsApi::Api.new
64
- @report_utils = @api.report_utils(API_VERSION)
65
- end
66
-
67
- # Testing HTTP code 400.
68
- def test_check_for_errors_400()
69
- begin
70
- response = ResponseStub.new(400, XML_REPLY[:reply])
71
- @report_utils.check_for_errors(response)
72
- assert(false, 'No exception thrown for code 400')
73
- rescue AdwordsApi::Errors::ReportXmlError => e
74
- assert_equal(400, e.http_code)
75
- assert_equal(XML_REPLY[:type], e.type)
76
- assert_equal(XML_REPLY[:trigger], e.trigger)
77
- assert_equal(XML_REPLY[:field_path], e.field_path)
78
- assert_not_nil(e.message)
79
- end
80
- end
81
-
82
- # Testing HTTP code 500.
83
- def test_check_for_errors_500()
84
- begin
85
- response = ResponseStub.new(500, nil)
86
- @report_utils.check_for_errors(response)
87
- assert(false, 'No exception thrown for code 500')
88
- rescue AdwordsApi::Errors::ReportError => e
89
- assert_equal(500, e.http_code)
90
- end
91
- end
92
-
93
- # Testing HTTP code 200 with success.
94
- def test_check_for_errors_200_success()
95
- response = ResponseStub.new(200, VALID_REPORT)
96
- assert_nothing_raised do
97
- @report_utils.check_for_errors(response)
98
- end
99
- end
100
-
101
- # Testing correct gzipped reply.
102
- def test_gzipped_data()
103
- report = (RUBY_VERSION >= '1.9.1') ?
104
- GZIPPED_REPORT.force_encoding('UTF-8') : GZIPPED_REPORT
105
- response = ResponseStub.new(200, report)
106
- assert_nothing_raised do
107
- @report_utils.check_for_errors(response)
108
- end
109
- end
110
-
111
- # Tests generated hash order for root (complete set).
112
- def test_add_report_definition_hash_order_root1()
113
- node = {
114
- :include_zero_impressions => false,
115
- :download_format => 'CSV',
116
- :report_type => 'CRITERIA_PERFORMANCE_REPORT',
117
- :selector => {},
118
- :report_name => 'report_name',
119
- :date_range_type => 'LAST_7_DAYS'
120
- }
121
- expected = [:selector, :report_name, :report_type, :date_range_type,
122
- :download_format, :include_zero_impressions]
123
- @report_utils.add_report_definition_hash_order(node)
124
- assert_not_nil(node[:order!])
125
- assert_equal(expected, node[:order!])
126
- end
127
-
128
- # Tests generated hash order for root (incomplete set).
129
- def test_add_report_definition_hash_order_root2()
130
- node = {
131
- :download_format => 'CSV',
132
- :report_type => 'CRITERIA_PERFORMANCE_REPORT',
133
- :selector => {},
134
- :report_name => 'report_name',
135
- :date_range_type => 'LAST_7_DAYS'
136
- }
137
- expected = [:selector, :report_name, :report_type, :date_range_type,
138
- :download_format]
139
- @report_utils.add_report_definition_hash_order(node)
140
- assert_not_nil(node[:order!])
141
- assert_equal(expected, node[:order!])
142
- end
143
-
144
- # Tests generated hash order for whole structure.
145
- def test_add_report_definition_hash_order_deep()
146
- node = {
147
- :report_name => 'report_name',
148
- :report_type => 'CRITERIA_PERFORMANCE_REPORT',
149
- :selector => {
150
- :date_range => {:max => '20120405', :min => '20120405'},
151
- :predicates => {:operator => 'IN', :field => 'S', :values => ['A']},
152
- :fields => ['CampaignId']
153
- },
154
- :include_zero_impressions => false,
155
- :download_format => 'CSV',
156
- :date_range_type => 'LAST_7_DAYS'
157
- }
158
- expected1 = [:selector, :report_name, :report_type, :date_range_type,
159
- :download_format, :include_zero_impressions]
160
- expected2 = [:fields, :predicates, :date_range]
161
- expected3 = [:min, :max]
162
- expected4 = [:field, :operator, :values]
163
-
164
- @report_utils.add_report_definition_hash_order(node)
165
- assert_not_nil(node[:order!])
166
- assert_not_nil(node[:selector][:order!])
167
- assert_not_nil(node[:selector][:date_range][:order!])
168
- assert_not_nil(node[:selector][:predicates][:order!])
169
-
170
- assert_equal(expected1, node[:order!])
171
- assert_equal(expected2, node[:selector][:order!])
172
- assert_equal(expected3, node[:selector][:date_range][:order!])
173
- assert_equal(expected4, node[:selector][:predicates][:order!])
174
- end
175
-
176
- # Testing check_for_xml_error.
177
- def check_for_xml_error()
178
- begin
179
- @report_utils.check_for_xml_error(XML_REPLY[:reply], 42)
180
- assert(false, 'No exception thrown for code 42')
181
- rescue AdwordsApi::Errors::ReportXmlError => e
182
- assert_equal(42, e.http_code)
183
- assert_equal(XML_REPLY[:type], e.type)
184
- assert_equal(XML_REPLY[:trigger], e.trigger)
185
- assert_equal(XML_REPLY[:field_path], e.field_path)
186
- assert_not_nil(e.message)
187
- end
188
- end
189
-
190
- def test_return_money_in_micros_removal()
191
- node = {
192
- :report_name => 'report_name',
193
- :report_type => 'CAMPAIGN_PERFORMANCE_REPORT',
194
- :selector => {
195
- :date_range => {:max=> '20120405', :min => '20120405'},
196
- :predicates => {:operator => 'IN', :field => 'S', :values => ['A']},
197
- :fields => ['CampaignId']
198
- },
199
- :download_format => 'CSV',
200
- :date_range_type => 'LAST_7_DAYS'
201
- }
202
- assert_nothing_raised do
203
- @report_utils.check_report_definition_hash(node)
204
- end
205
-
206
- node[:return_money_in_micros] = true
207
- assert_raise AdwordsApi::Errors::InvalidReportDefinitionError do
208
- @report_utils.check_report_definition_hash(node)
209
- end
210
-
211
- node[:return_money_in_micros] = false
212
- assert_raise AdwordsApi::Errors::InvalidReportDefinitionError do
213
- @report_utils.check_report_definition_hash(node)
214
- end
215
- end
216
- end