/adx_buyer_api/examples/v201109/update_ad_group.rb

https://code.google.com/ · Ruby · 90 lines · 47 code · 11 blank · 32 comment · 8 complexity · 3a0a1e252a40919300510e4cce95b618 MD5 · raw file

  1. #!/usr/bin/ruby
  2. #
  3. # Author:: 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. # This example illustrates how to update an ad group, setting its status to
  21. # 'PAUSED'. To create an ad group, run add_ad_group.rb.
  22. #
  23. # Tags: AdGroupService.mutate
  24. require 'rubygems'
  25. gem 'google-adwords-api'
  26. require 'adwords_api'
  27. API_VERSION = :v201109
  28. def update_ad_group()
  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. # 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. ad_group_srv = adwords.service(:AdGroupService, API_VERSION)
  36. ad_group_id = 'INSERT_AD_GROUP_ID_HERE'.to_i
  37. # Prepare for updating ad group.
  38. operation = {
  39. :operator => 'SET',
  40. :operand => {
  41. :status => 'PAUSED',
  42. :id => ad_group_id
  43. }
  44. }
  45. # Update ad group.
  46. response = ad_group_srv.mutate([operation])
  47. if response and response[:value]
  48. ad_group = response[:value].first
  49. puts 'Ad group id %d was successfully updated.' % ad_group[:id]
  50. else
  51. puts 'No ad groups were updated.'
  52. end
  53. end
  54. if __FILE__ == $0
  55. begin
  56. update_ad_group()
  57. # Connection error. Likely transitory.
  58. rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
  59. puts 'Connection Error: %s' % e
  60. puts 'Source: %s' % e.backtrace.first
  61. # API Error.
  62. rescue AdwordsApi::Errors::ApiException => e
  63. puts 'API Exception caught.'
  64. puts 'Message: %s' % e.message
  65. puts 'Code: %d' % e.code if e.code
  66. puts 'Trigger: %s' % e.trigger if e.trigger
  67. puts 'Errors:'
  68. if e.errors
  69. e.errors.each_with_index do |error, index|
  70. puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
  71. error.each_pair do |field, value|
  72. if field != :xsi_type
  73. puts ' %s: %s' % [field, value]
  74. end
  75. end
  76. end
  77. end
  78. end
  79. end