google-adwords-api 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. data/ChangeLog +7 -0
  2. data/README +4 -4
  3. data/examples/v201109_1/account_management/create_account.rb +80 -0
  4. data/examples/v201109_1/account_management/get_account_alerts.rb +111 -0
  5. data/examples/v201109_1/account_management/get_account_changes.rb +137 -0
  6. data/examples/v201109_1/account_management/get_account_hierarchy.rb +93 -0
  7. data/examples/v201109_1/account_management/get_client_customer_id.rb +85 -0
  8. data/examples/v201109_1/account_management/get_client_unit_usage.rb +87 -0
  9. data/examples/v201109_1/basic_operations/add_ad_groups.rb +111 -0
  10. data/examples/v201109_1/basic_operations/add_campaigns.rb +140 -0
  11. data/examples/v201109_1/basic_operations/add_keywords.rb +108 -0
  12. data/examples/v201109_1/basic_operations/add_text_ads.rb +105 -0
  13. data/examples/v201109_1/basic_operations/delete_ad.rb +85 -0
  14. data/examples/v201109_1/basic_operations/delete_ad_group.rb +102 -0
  15. data/examples/v201109_1/basic_operations/delete_campaign.rb +103 -0
  16. data/examples/v201109_1/basic_operations/delete_keyword.rb +90 -0
  17. data/examples/v201109_1/basic_operations/get_ad_groups.rb +98 -0
  18. data/examples/v201109_1/basic_operations/get_campaigns.rb +93 -0
  19. data/examples/v201109_1/basic_operations/get_keywords.rb +104 -0
  20. data/examples/v201109_1/basic_operations/get_text_ads.rb +106 -0
  21. data/examples/v201109_1/basic_operations/pause_ad.rb +84 -0
  22. data/examples/v201109_1/basic_operations/update_ad_group.rb +81 -0
  23. data/examples/v201109_1/basic_operations/update_campaign.rb +84 -0
  24. data/examples/v201109_1/basic_operations/update_keyword.rb +99 -0
  25. data/examples/v201109_1/campaign_management/add_experiment.rb +158 -0
  26. data/examples/v201109_1/campaign_management/add_keywords_in_bulk.rb +150 -0
  27. data/examples/v201109_1/campaign_management/add_location_extension.rb +117 -0
  28. data/examples/v201109_1/campaign_management/add_location_extension_override.rb +83 -0
  29. data/examples/v201109_1/campaign_management/get_all_disapproved_ads.rb +93 -0
  30. data/examples/v201109_1/campaign_management/promote_experiment.rb +77 -0
  31. data/examples/v201109_1/campaign_management/set_ad_parameters.rb +114 -0
  32. data/examples/v201109_1/campaign_management/validate_text_ad.rb +106 -0
  33. data/examples/v201109_1/error_handling/handle_captcha_challenge.rb +93 -0
  34. data/examples/v201109_1/error_handling/handle_partial_failures.rb +126 -0
  35. data/examples/v201109_1/error_handling/handle_policy_violation_error.rb +137 -0
  36. data/examples/v201109_1/error_handling/handle_two_factor_authorization_error.rb +87 -0
  37. data/examples/v201109_1/misc/get_all_images_and_videos.rb +101 -0
  38. data/examples/v201109_1/misc/upload_image.rb +90 -0
  39. data/examples/v201109_1/misc/use_oauth.rb +97 -0
  40. data/examples/v201109_1/optimization/estimate_keyword_traffic.rb +129 -0
  41. data/examples/v201109_1/optimization/get_keyword_bid_simulations.rb +92 -0
  42. data/examples/v201109_1/optimization/get_keyword_ideas.rb +113 -0
  43. data/examples/v201109_1/optimization/get_placement_ideas.rb +106 -0
  44. data/examples/v201109_1/remarketing/add_audience.rb +115 -0
  45. data/examples/v201109_1/remarketing/add_conversion_tracker.rb +97 -0
  46. data/examples/v201109_1/reporting/download_criteria_report.rb +79 -0
  47. data/examples/v201109_1/reporting/get_campaign_stats.rb +105 -0
  48. data/examples/v201109_1/reporting/get_defined_reports.rb +75 -0
  49. data/examples/{v201109/reporting/download_defined_report.rb → v201109_1/reporting/get_report_fields.rb} +15 -11
  50. data/examples/v201109_1/reporting/parallel_report_download.rb +159 -0
  51. data/examples/v201109_1/targeting/add_campaign_targeting_criteria.rb +118 -0
  52. data/examples/v201109_1/targeting/get_campaign_targeting_criteria.rb +102 -0
  53. data/examples/v201109_1/targeting/get_targetable_languages_and_carriers.rb +86 -0
  54. data/examples/v201109_1/targeting/lookup_location.rb +103 -0
  55. data/lib/adwords_api.rb +15 -88
  56. data/lib/adwords_api/api_config.rb +75 -33
  57. data/lib/adwords_api/client_login_header_handler.rb +17 -26
  58. data/lib/adwords_api/credential_handler.rb +29 -65
  59. data/lib/adwords_api/report_header_handler.rb +65 -0
  60. data/lib/adwords_api/report_utils.rb +12 -36
  61. data/lib/adwords_api/v201109/ad_extension_override_service.rb +3 -3
  62. data/lib/adwords_api/v201109/ad_extension_override_service_registry.rb +2 -2
  63. data/lib/adwords_api/v201109/ad_group_ad_service.rb +3 -3
  64. data/lib/adwords_api/v201109/ad_group_ad_service_registry.rb +1 -1
  65. data/lib/adwords_api/v201109/ad_group_criterion_service.rb +3 -3
  66. data/lib/adwords_api/v201109/ad_group_criterion_service_registry.rb +1 -1
  67. data/lib/adwords_api/v201109/ad_group_service.rb +3 -3
  68. data/lib/adwords_api/v201109/ad_group_service_registry.rb +1 -1
  69. data/lib/adwords_api/v201109/ad_param_service.rb +3 -3
  70. data/lib/adwords_api/v201109/ad_param_service_registry.rb +1 -1
  71. data/lib/adwords_api/v201109/alert_service.rb +3 -3
  72. data/lib/adwords_api/v201109/alert_service_registry.rb +1 -1
  73. data/lib/adwords_api/v201109/budget_order_service.rb +42 -0
  74. data/lib/adwords_api/v201109/budget_order_service_registry.rb +46 -0
  75. data/lib/adwords_api/v201109/bulk_mutate_job_service.rb +3 -3
  76. data/lib/adwords_api/v201109/bulk_mutate_job_service_registry.rb +1 -1
  77. data/lib/adwords_api/v201109/campaign_ad_extension_service.rb +3 -3
  78. data/lib/adwords_api/v201109/campaign_ad_extension_service_registry.rb +2 -2
  79. data/lib/adwords_api/v201109/campaign_criterion_service.rb +3 -3
  80. data/lib/adwords_api/v201109/campaign_criterion_service_registry.rb +1 -1
  81. data/lib/adwords_api/v201109/campaign_service.rb +3 -3
  82. data/lib/adwords_api/v201109/campaign_service_registry.rb +1 -1
  83. data/lib/adwords_api/v201109/campaign_target_service.rb +3 -3
  84. data/lib/adwords_api/v201109/campaign_target_service_registry.rb +1 -1
  85. data/lib/adwords_api/v201109/constant_data_service.rb +3 -3
  86. data/lib/adwords_api/v201109/constant_data_service_registry.rb +1 -1
  87. data/lib/adwords_api/v201109/conversion_tracker_service.rb +3 -3
  88. data/lib/adwords_api/v201109/conversion_tracker_service_registry.rb +1 -1
  89. data/lib/adwords_api/v201109/create_account_service.rb +3 -3
  90. data/lib/adwords_api/v201109/create_account_service_registry.rb +1 -1
  91. data/lib/adwords_api/v201109/customer_sync_service.rb +3 -3
  92. data/lib/adwords_api/v201109/customer_sync_service_registry.rb +1 -1
  93. data/lib/adwords_api/v201109/data_service.rb +3 -3
  94. data/lib/adwords_api/v201109/data_service_registry.rb +1 -1
  95. data/lib/adwords_api/v201109/experiment_service.rb +3 -3
  96. data/lib/adwords_api/v201109/experiment_service_registry.rb +1 -1
  97. data/lib/adwords_api/v201109/geo_location_service.rb +3 -3
  98. data/lib/adwords_api/v201109/geo_location_service_registry.rb +1 -1
  99. data/lib/adwords_api/v201109/info_service.rb +3 -3
  100. data/lib/adwords_api/v201109/info_service_registry.rb +1 -1
  101. data/lib/adwords_api/v201109/location_criterion_service.rb +3 -3
  102. data/lib/adwords_api/v201109/location_criterion_service_registry.rb +1 -1
  103. data/lib/adwords_api/v201109/media_service.rb +3 -3
  104. data/lib/adwords_api/v201109/media_service_registry.rb +1 -1
  105. data/lib/adwords_api/v201109/mutate_job_service.rb +3 -3
  106. data/lib/adwords_api/v201109/mutate_job_service_registry.rb +1 -1
  107. data/lib/adwords_api/v201109/report_definition_service.rb +3 -12
  108. data/lib/adwords_api/v201109/report_definition_service_registry.rb +1 -1
  109. data/lib/adwords_api/v201109/serviced_account_service.rb +3 -3
  110. data/lib/adwords_api/v201109/serviced_account_service_registry.rb +1 -1
  111. data/lib/adwords_api/v201109/targeting_idea_service.rb +3 -3
  112. data/lib/adwords_api/v201109/targeting_idea_service_registry.rb +1 -1
  113. data/lib/adwords_api/v201109/traffic_estimator_service.rb +3 -3
  114. data/lib/adwords_api/v201109/traffic_estimator_service_registry.rb +1 -1
  115. data/lib/adwords_api/v201109/user_list_service.rb +3 -3
  116. data/lib/adwords_api/v201109/user_list_service_registry.rb +1 -1
  117. data/lib/adwords_api/v201109_1/ad_extension_override_service.rb +38 -0
  118. data/lib/adwords_api/v201109_1/ad_extension_override_service_registry.rb +46 -0
  119. data/lib/adwords_api/v201109_1/ad_group_ad_service.rb +38 -0
  120. data/lib/adwords_api/v201109_1/ad_group_ad_service_registry.rb +46 -0
  121. data/lib/adwords_api/v201109_1/ad_group_criterion_service.rb +38 -0
  122. data/lib/adwords_api/v201109_1/ad_group_criterion_service_registry.rb +46 -0
  123. data/lib/adwords_api/v201109_1/ad_group_service.rb +38 -0
  124. data/lib/adwords_api/v201109_1/ad_group_service_registry.rb +46 -0
  125. data/lib/adwords_api/v201109_1/ad_param_service.rb +38 -0
  126. data/lib/adwords_api/v201109_1/ad_param_service_registry.rb +46 -0
  127. data/lib/adwords_api/v201109_1/alert_service.rb +34 -0
  128. data/lib/adwords_api/v201109_1/alert_service_registry.rb +46 -0
  129. data/lib/adwords_api/v201109_1/budget_order_service.rb +42 -0
  130. data/lib/adwords_api/v201109_1/budget_order_service_registry.rb +46 -0
  131. data/lib/adwords_api/v201109_1/bulk_mutate_job_service.rb +38 -0
  132. data/lib/adwords_api/v201109_1/bulk_mutate_job_service_registry.rb +46 -0
  133. data/lib/adwords_api/v201109_1/campaign_ad_extension_service.rb +38 -0
  134. data/lib/adwords_api/v201109_1/campaign_ad_extension_service_registry.rb +46 -0
  135. data/lib/adwords_api/v201109_1/campaign_criterion_service.rb +38 -0
  136. data/lib/adwords_api/v201109_1/campaign_criterion_service_registry.rb +46 -0
  137. data/lib/adwords_api/v201109_1/campaign_service.rb +38 -0
  138. data/lib/adwords_api/v201109_1/campaign_service_registry.rb +46 -0
  139. data/lib/adwords_api/v201109_1/campaign_target_service.rb +38 -0
  140. data/lib/adwords_api/v201109_1/campaign_target_service_registry.rb +46 -0
  141. data/lib/adwords_api/v201109_1/constant_data_service.rb +38 -0
  142. data/lib/adwords_api/v201109_1/constant_data_service_registry.rb +46 -0
  143. data/lib/adwords_api/v201109_1/conversion_tracker_service.rb +38 -0
  144. data/lib/adwords_api/v201109_1/conversion_tracker_service_registry.rb +46 -0
  145. data/lib/adwords_api/v201109_1/create_account_service.rb +34 -0
  146. data/lib/adwords_api/v201109_1/create_account_service_registry.rb +47 -0
  147. data/lib/adwords_api/v201109_1/customer_sync_service.rb +34 -0
  148. data/lib/adwords_api/v201109_1/customer_sync_service_registry.rb +47 -0
  149. data/lib/adwords_api/v201109_1/data_service.rb +38 -0
  150. data/lib/adwords_api/v201109_1/data_service_registry.rb +46 -0
  151. data/lib/adwords_api/v201109_1/experiment_service.rb +38 -0
  152. data/lib/adwords_api/v201109_1/experiment_service_registry.rb +46 -0
  153. data/lib/adwords_api/v201109_1/geo_location_service.rb +34 -0
  154. data/lib/adwords_api/v201109_1/geo_location_service_registry.rb +46 -0
  155. data/lib/adwords_api/v201109_1/info_service.rb +34 -0
  156. data/lib/adwords_api/v201109_1/info_service_registry.rb +46 -0
  157. data/lib/adwords_api/v201109_1/location_criterion_service.rb +34 -0
  158. data/lib/adwords_api/v201109_1/location_criterion_service_registry.rb +46 -0
  159. data/lib/adwords_api/v201109_1/media_service.rb +38 -0
  160. data/lib/adwords_api/v201109_1/media_service_registry.rb +46 -0
  161. data/lib/adwords_api/v201109_1/mutate_job_service.rb +42 -0
  162. data/lib/adwords_api/v201109_1/mutate_job_service_registry.rb +46 -0
  163. data/lib/adwords_api/v201109_1/report_definition_service.rb +38 -0
  164. data/lib/adwords_api/v201109_1/report_definition_service_registry.rb +46 -0
  165. data/lib/adwords_api/v201109_1/serviced_account_service.rb +34 -0
  166. data/lib/adwords_api/v201109_1/serviced_account_service_registry.rb +46 -0
  167. data/lib/adwords_api/v201109_1/targeting_idea_service.rb +38 -0
  168. data/lib/adwords_api/v201109_1/targeting_idea_service_registry.rb +46 -0
  169. data/lib/adwords_api/v201109_1/traffic_estimator_service.rb +34 -0
  170. data/lib/adwords_api/v201109_1/traffic_estimator_service_registry.rb +46 -0
  171. data/lib/adwords_api/v201109_1/user_list_service.rb +38 -0
  172. data/lib/adwords_api/v201109_1/user_list_service_registry.rb +46 -0
  173. data/lib/adwords_api/version.rb +1 -1
  174. data/test/adwords_api/test_api_config.rb +24 -8
  175. data/test/adwords_api/test_credential_handler.rb +32 -52
  176. data/test/coverage.rb +38 -0
  177. data/test/examples/v201109/test_reporting.rb +0 -12
  178. data/test/examples/v201109_1/test_account_management.rb +60 -0
  179. data/test/examples/v201109_1/test_basic_operations.rb +172 -0
  180. data/test/examples/v201109_1/test_campaign_management.rb +116 -0
  181. data/test/examples/v201109_1/test_error_handling.rb +63 -0
  182. data/{lib/adwords_api/v13_login_handler.rb → test/examples/v201109_1/test_misc.rb} +24 -10
  183. data/test/examples/v201109_1/test_optimization.rb +63 -0
  184. data/test/{suite_exampletests.rb → examples/v201109_1/test_remarketing.rb} +16 -8
  185. data/test/examples/v201109_1/test_reporting.rb +77 -0
  186. data/test/examples/v201109_1/test_targeting.rb +60 -0
  187. data/test/examples/v201109_1/utils.rb +239 -0
  188. data/test/suite_exampletests_v201109.rb +37 -0
  189. data/test/suite_exampletests_v201109_1.rb +37 -0
  190. data/test/suite_unittests.rb +7 -5
  191. metadata +136 -11
  192. data/lib/adwords_api/extensions.rb +0 -269
@@ -0,0 +1,118 @@
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
+ # This example adds various types of targeting criteria to a campaign. To get
22
+ # campaigns list, run get_campaigns.rb.
23
+ #
24
+ # Tags: CampaignCriterionService.mutate
25
+
26
+ require 'adwords_api'
27
+
28
+ def add_campaign_targeting_criteria(campaign_id)
29
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
30
+ # when called without parameters.
31
+ adwords = AdwordsApi::Api.new
32
+
33
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
34
+ # the configuration file or provide your own logger:
35
+ # adwords.logger = Logger.new('adwords_xml.log')
36
+
37
+ campaign_criterion_srv =
38
+ adwords.service(:CampaignCriterionService, API_VERSION)
39
+
40
+ # Create campaign criteria.
41
+ campaign_criteria = [
42
+ # Location criteria. The IDs can be found in the documentation or retrieved
43
+ # with the LocationCriterionService.
44
+ {:xsi_type => 'Location', :id => 21137}, # California, USA
45
+ {:xsi_type => 'Location', :id => 2484}, # Mexico
46
+ # Language criteria. The IDs can be found in the documentation or retrieved
47
+ # with the ConstantDataService.
48
+ {:xsi_type => 'Language', :id => 1000}, # English
49
+ {:xsi_type => 'Language', :id => 1003}, # Spanish
50
+ # Platform criteria. The IDs can be found in the documentation.
51
+ {:xsi_type => 'Platform', :id => 30001}, # Mobile
52
+ {:xsi_type => 'Platform', :id => 30002} # Tablets
53
+ ]
54
+
55
+ # Create operations.
56
+ operations = campaign_criteria.map do |criterion|
57
+ {:operator => 'ADD',
58
+ :operand => {
59
+ :campaign_id => campaign_id,
60
+ :criterion => criterion}
61
+ }
62
+ end
63
+
64
+ # Add negative campaign criterion.
65
+ operations << {
66
+ :operator => 'ADD',
67
+ :operand => {
68
+ # The 'xsi_type' field allows you to specify the xsi:type of the object
69
+ # being created. It's only necessary when you must provide an explicit
70
+ # type that the client library can't infer.
71
+ :xsi_type => 'NegativeCampaignCriterion',
72
+ :campaign_id => campaign_id,
73
+ :criterion => {
74
+ :xsi_type => 'Keyword',
75
+ :text => 'jupiter cruise',
76
+ :match_type => 'BROAD'
77
+ }
78
+ }
79
+ }
80
+
81
+ response = campaign_criterion_srv.mutate(operations)
82
+
83
+ if response and response[:value]
84
+ criteria = response[:value]
85
+ criteria.each do |campaign_criterion|
86
+ criterion = campaign_criterion[:criterion]
87
+ puts ("Campaign criterion with campaign ID %d, criterion ID %d and " +
88
+ "type '%s' was added.") % [campaign_criterion[:campaign_id],
89
+ criterion[:id], criterion[:criterion_type]]
90
+ end
91
+ else
92
+ puts 'No criteria were returned.'
93
+ end
94
+ end
95
+
96
+ if __FILE__ == $0
97
+ API_VERSION = :v201109_1
98
+
99
+ begin
100
+ campaign_id = 'INSERT_CAMPAIGN_ID_HERE'
101
+ add_campaign_targeting_criteria(campaign_id)
102
+
103
+ # HTTP errors.
104
+ rescue AdsCommon::Errors::HttpError => e
105
+ puts "HTTP Error: %s" % e
106
+
107
+ # API errors.
108
+ rescue AdwordsApi::Errors::ApiException => e
109
+ puts "Message: %s" % e.message
110
+ puts 'Errors:'
111
+ e.errors.each_with_index do |error, index|
112
+ puts "\tError [%d]:" % (index + 1)
113
+ error.each do |field, value|
114
+ puts "\t\t%s: %s" % [field, value]
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,102 @@
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
+ # This example illustrates how to retrieve all the campaign targets. To set
22
+ # campaign targets, run add_campaign_targeting_criteria.rb.
23
+ #
24
+ # Tags: CampaignCriterionService.get
25
+
26
+ require 'adwords_api'
27
+
28
+ def get_campaign_targeting_criteria(campaign_id)
29
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
30
+ # when called without parameters.
31
+ adwords = AdwordsApi::Api.new
32
+
33
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
34
+ # the configuration file or provide your own logger:
35
+ # adwords.logger = Logger.new('adwords_xml.log')
36
+
37
+ campaign_criterion_srv =
38
+ adwords.service(:CampaignCriterionService, API_VERSION)
39
+
40
+ # Selector to get all the targeting for this campaign.
41
+ selector = {
42
+ :fields => ['Id', 'CriteriaType', 'KeywordText'],
43
+ :predicates => [
44
+ {:field => 'CampaignId', :operator => 'EQUALS', :values => [campaign_id]}
45
+ ],
46
+ :paging => {
47
+ :start_index => 0,
48
+ :number_results => PAGE_SIZE
49
+ }
50
+ }
51
+
52
+ # Set initial values.
53
+ offset, page = 0, {}
54
+
55
+ begin
56
+ page = campaign_criterion_srv.get(selector)
57
+ if page[:entries]
58
+ page[:entries].each do |typed_criterion|
59
+ negative = typed_criterion[:xsi_type] == 'NegativeCampaignCriterion' ?
60
+ ' (negative)' : ''
61
+ criterion = typed_criterion[:criterion]
62
+ puts ("Campaign criterion%s with ID %d, type '%s' and text '%s'" +
63
+ " was found.") %
64
+ [negative, criterion[:id], criterion[:type], criterion[:text]]
65
+ end
66
+ # Increment values to request the next page.
67
+ offset += PAGE_SIZE
68
+ selector[:paging][:start_index] = offset
69
+ end
70
+ end while page[:total_num_entries] > offset
71
+
72
+ if page.include?(:total_num_entries)
73
+ puts "\tCampaign ID %d has %d criteria." %
74
+ [campaign_id, page[:total_num_entries]]
75
+ end
76
+ end
77
+
78
+ if __FILE__ == $0
79
+ API_VERSION = :v201109_1
80
+ PAGE_SIZE = 500
81
+
82
+ begin
83
+ # Specify campaign ID to get targeting for.
84
+ campaign_id = 'INSERT_CAMPAIGN_ID_HERE'.to_i
85
+ get_campaign_targeting_criteria(campaign_id)
86
+
87
+ # HTTP errors.
88
+ rescue AdsCommon::Errors::HttpError => e
89
+ puts "HTTP Error: %s" % e
90
+
91
+ # API errors.
92
+ rescue AdwordsApi::Errors::ApiException => e
93
+ puts "Message: %s" % e.message
94
+ puts 'Errors:'
95
+ e.errors.each_with_index do |error, index|
96
+ puts "\tError [%d]:" % (index + 1)
97
+ error.each do |field, value|
98
+ puts "\t\t%s: %s" % [field, value]
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,86 @@
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
+ # This example illustrates how to retrieve all languages and carriers available
22
+ # for targeting.
23
+ #
24
+ # Tags: ConstantDataService.getLanguageCriterion
25
+ # Tags: ConstantDataService.getCarrierCriterion
26
+
27
+ require 'adwords_api'
28
+
29
+ def get_targetable_languages_and_carriers()
30
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
31
+ # when called without parameters.
32
+ adwords = AdwordsApi::Api.new
33
+
34
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
35
+ # the configuration file or provide your own logger:
36
+ # adwords.logger = Logger.new('adwords_xml.log')
37
+
38
+ constant_data_srv = adwords.service(:ConstantDataService, API_VERSION)
39
+
40
+ # Get all languages from ConstantDataService.
41
+ languages = constant_data_srv.get_language_criterion()
42
+
43
+ if languages
44
+ languages.each do |language|
45
+ puts "Language name is '%s', ID is %d and code is '%s'." %
46
+ [language[:name], language[:id], language[:code]]
47
+ end
48
+ else
49
+ puts 'No languages were found.'
50
+ end
51
+
52
+ # Get all carriers from ConstantDataService.
53
+ carriers = constant_data_srv.get_carrier_criterion()
54
+
55
+ if carriers
56
+ carriers.each do |carrier|
57
+ puts "Carrier name is '%s', ID is %d and country code is '%s'." %
58
+ [carrier[:name], carrier[:id], carrier[:country_code]]
59
+ end
60
+ else
61
+ puts 'No carriers were retrieved.'
62
+ end
63
+ end
64
+
65
+ if __FILE__ == $0
66
+ API_VERSION = :v201109_1
67
+
68
+ begin
69
+ get_targetable_languages_and_carriers()
70
+
71
+ # HTTP errors.
72
+ rescue AdsCommon::Errors::HttpError => e
73
+ puts "HTTP Error: %s" % e
74
+
75
+ # API errors.
76
+ rescue AdwordsApi::Errors::ApiException => e
77
+ puts "Message: %s" % e.message
78
+ puts 'Errors:'
79
+ e.errors.each_with_index do |error, index|
80
+ puts "\tError [%d]:" % (index + 1)
81
+ error.each do |field, value|
82
+ puts "\t\t%s: %s" % [field, value]
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,103 @@
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
+ # This example gets location criteria by name.
22
+ #
23
+ # Tags: LocationCriterionService.get
24
+
25
+ require 'adwords_api'
26
+
27
+ def lookup_location()
28
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
29
+ # when called without parameters.
30
+ adwords = AdwordsApi::Api.new
31
+
32
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
33
+ # the configuration file or provide your own logger:
34
+ # adwords.logger = Logger.new('adwords_xml.log')
35
+
36
+ location_criterion_srv =
37
+ adwords.service(:LocationCriterionService, API_VERSION)
38
+
39
+ # List of locations to look up.
40
+ location_names = ['Paris', 'Quebec', 'Spain', 'Deutschland']
41
+ # Locale to retrieve names in.
42
+ locale = 'en'
43
+
44
+ # Get the criteria by names.
45
+ selector = {
46
+ :fields => ['Id', 'LocationName', 'CanonicalName', 'DisplayType',
47
+ 'ParentLocations', 'Reach'],
48
+ :predicates => [
49
+ # Location names must match exactly, only EQUALS and IN are supported.
50
+ {:field => 'LocationName',
51
+ :operator => 'IN',
52
+ :values => location_names},
53
+ # Set the locale of the returned location names.
54
+ {:field => 'Locale', :operator => 'EQUALS', :values => [locale]}
55
+ ]
56
+ }
57
+ criteria = location_criterion_srv.get(selector)
58
+
59
+ if criteria
60
+ criteria.each do |criterion|
61
+ # Extract all parent location names as one comma-separated string.
62
+ parent_location = if criterion[:location][:parent_locations] and
63
+ !criterion[:location][:parent_locations].empty?
64
+ locations_array = criterion[:location][:parent_locations].map do |loc|
65
+ loc[:location_name]
66
+ end
67
+ locations_array.join(', ')
68
+ else
69
+ 'N/A'
70
+ end
71
+ puts ("The search term '%s' returned the location '%s' of type '%s' " +
72
+ "with ID %d, parent locations '%s' and reach %d") %
73
+ [criterion[:search_term], criterion[:location][:location_name],
74
+ criterion[:location][:criterion_type], criterion[:location][:id],
75
+ parent_location, criterion[:reach]]
76
+ end
77
+ else
78
+ puts 'No criteria were returned.'
79
+ end
80
+ end
81
+
82
+ if __FILE__ == $0
83
+ API_VERSION = :v201109_1
84
+
85
+ begin
86
+ lookup_location()
87
+
88
+ # HTTP errors.
89
+ rescue AdsCommon::Errors::HttpError => e
90
+ puts "HTTP Error: %s" % e
91
+
92
+ # API errors.
93
+ rescue AdwordsApi::Errors::ApiException => e
94
+ puts "Message: %s" % e.message
95
+ puts 'Errors:'
96
+ e.errors.each_with_index do |error, index|
97
+ puts "\tError [%d]:" % (index + 1)
98
+ error.each do |field, value|
99
+ puts "\t\t%s: %s" % [field, value]
100
+ end
101
+ end
102
+ end
103
+ end
data/lib/adwords_api.rb CHANGED
@@ -20,19 +20,15 @@
20
20
  #
21
21
  # Contains the main classes for the client library.
22
22
 
23
- gem 'google-ads-common', '~>0.6.3'
23
+ gem 'google-ads-common', '~>0.7.0'
24
24
 
25
25
  require 'ads_common/api'
26
- require 'ads_common/auth/oauth_handler'
27
- require 'ads_common/savon_service'
28
26
  require 'ads_common/savon_headers/oauth_header_handler'
29
- require 'ads_common/savon_headers/simple_header_handler'
30
27
  require 'adwords_api/api_config'
31
28
  require 'adwords_api/client_login_header_handler'
32
29
  require 'adwords_api/credential_handler'
33
30
  require 'adwords_api/errors'
34
31
  require 'adwords_api/report_utils'
35
- require 'adwords_api/v13_login_handler'
36
32
 
37
33
  # Main namespace for all the client library's modules and classes.
38
34
  module AdwordsApi
@@ -42,6 +38,7 @@ module AdwordsApi
42
38
  # Holds all the services, as well as login credentials.
43
39
  #
44
40
  class Api < AdsCommon::Api
41
+
45
42
  # Constructor for API.
46
43
  def initialize(provided_config = nil)
47
44
  super(provided_config)
@@ -49,80 +46,34 @@ module AdwordsApi
49
46
  end
50
47
 
51
48
  # Getter for the API service configurations
52
- def api_config
49
+ def api_config()
53
50
  AdwordsApi::ApiConfig
54
51
  end
55
52
 
56
- # Sets the logger to use.
57
- def logger=(logger)
58
- super(logger)
59
- AdsCommon::SavonService.logger = logger
60
- end
61
-
62
- # Retrieve single NestedHeaderHandler for v20xx and one SingleHeaderHandler
63
- # per credential for v13.
53
+ # Retrieve correct soap_header_handler.
64
54
  #
65
55
  # Args:
66
56
  # - auth_handler: instance of an AdsCommon::Auth::BaseHandler subclass to
67
57
  # handle authentication
68
- # - header_list: the list of headers to be handled
69
58
  # - version: intended API version
70
59
  # - namespace: namespace to use as default for body
71
60
  #
72
61
  # Returns:
73
62
  # - a list of SOAP header handlers; one per provided header
74
63
  #
75
- def soap_header_handlers(auth_handler, header_list, version, namespace)
64
+ def soap_header_handler(auth_handler, version, namespace)
76
65
  auth_method = @config.read('authentication.method', :CLIENTLOGIN)
77
-
78
- handlers = []
79
-
80
- if version == :v13
81
- if auth_method == :OAUTH
82
- # v13 has no OAuth support, trying to fallback to ClientLogin.
83
- auth_handler = client_login_handler()
84
- end
85
- handlers = header_list.map do |header|
86
- AdsCommon::SavonHeaders::SimpleHeaderHandler.new(
87
- @credential_handler, auth_handler, header, namespace, version)
88
- end
89
- else
90
- handlers = case auth_method
91
- when :CLIENTLOGIN
92
- auth_ns = api_config.headers_config[:AUTH_NAMESPACE_PREAMBLE] +
93
- version.to_s
94
- [AdwordsApi::ClientLoginHeaderHandler.new(
95
- @credential_handler, auth_handler,
96
- api_config.headers_config[:REQUEST_HEADER], namespace, auth_ns,
97
- version)]
98
- when :OAUTH
99
- [AdsCommon::SavonHeaders::OAuthHeaderHandler.new(
100
- @credential_handler, auth_handler,
101
- api_config.headers_config[:REQUEST_HEADER], namespace, version)]
102
- end
66
+ handler = case auth_method
67
+ when :CLIENTLOGIN
68
+ auth_ns = api_config.client_login_config(:AUTH_NAMESPACE_PREAMBLE) +
69
+ version.to_s
70
+ AdwordsApi::ClientLoginHeaderHandler.new(
71
+ @credential_handler, auth_handler, namespace, auth_ns, version)
72
+ when :OAUTH
73
+ AdsCommon::SavonHeaders::OAuthHeaderHandler.new(
74
+ @credential_handler, auth_handler, namespace, version)
103
75
  end
104
- return handlers
105
- end
106
-
107
- # Accessor for client login handler, so that we can access the token.
108
- # This is a temporary solution for v13 and for v2009+ until full OAuth
109
- # support is available.
110
- def client_login_handler()
111
- if @client_login_handler.nil?
112
- auth_method = @config.read('authentication.method', :CLIENTLOGIN)
113
- environment = @config.read('service.environment')
114
- if auth_method == :CLIENTLOGIN
115
- # We use client login, so we can reuse general AuthHandler.
116
- @client_login_handler = get_auth_handler(environment)
117
- else
118
- # We are using OAuth or something else and need to generate a handler.
119
- auth_server = api_config.auth_server(environment)
120
- @client_login_handler = AdsCommon::Auth::ClientLoginHandler.new(
121
- @config, auth_server,
122
- api_config.headers_config[:LOGIN_SERVICE_NAME])
123
- end
124
- end
125
- return @client_login_handler
76
+ return handler
126
77
  end
127
78
 
128
79
  # Helper method to provide a simple way of doing an MCC-level operation
@@ -226,32 +177,8 @@ module AdwordsApi
226
177
  return AdwordsApi::ReportUtils.new(self, version)
227
178
  end
228
179
 
229
- # Overrides AdsCommon::Api.get_auth_handler to allow version-specific
230
- # handlers.
231
- def get_auth_handler(environment, version = nil)
232
- if @auth_handler.nil?
233
- @auth_handler = create_auth_handler(environment, version)
234
- end
235
- return @auth_handler
236
- end
237
-
238
180
  private
239
181
 
240
- # Creates an appropriate authentication handler for each service (reuses the
241
- # ClientLogin one to avoid generating multiple tokens unnecessarily).
242
- #
243
- # Args:
244
- # - environment: the current working environment (production, sandbox, etc.)
245
- # - version: intended API version
246
- #
247
- # Returns:
248
- # - auth handler
249
- #
250
- def create_auth_handler(environment, version = nil)
251
- return (version == :v13) ?
252
- AdwordsApi::V13LoginHandler.new(@config) : super(environment)
253
- end
254
-
255
182
  # Executes block with a temporary flag set to a given value. Returns block
256
183
  # result.
257
184
  def run_with_temporary_flag(flag_name, flag_value, block)