/adx_buyer_api/examples/v201109/get_all_images.rb

https://code.google.com/ · Ruby · 90 lines · 50 code · 9 blank · 31 comment · 8 complexity · fa7962561abad396d28eb43078ca0101 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 retrieve all images. To upload an image, run
  21. # upload_image.rb.
  22. #
  23. # Tags: MediaService.get
  24. require 'rubygems'
  25. gem 'google-adwords-api'
  26. require 'adwords_api'
  27. API_VERSION = :v201109
  28. def get_all_images()
  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. media_srv = adwords.service(:MediaService, API_VERSION)
  36. # Get all the images.
  37. selector = {
  38. :fields => ['MediaId', 'Height', 'Width', 'MimeType', 'Urls'],
  39. :ordering => [{:field => 'MediaId', :sort_order => 'ASCENDING'}],
  40. :predicates => [{:field => 'Type', :operator => 'IN', :values => ['IMAGE']}]
  41. }
  42. response = media_srv.get(selector)
  43. if response and response[:entries]
  44. images = response[:entries]
  45. puts "#{images.length} images(s) found."
  46. images.each do |image|
  47. dimensions = AdwordsApi::Utils.map(image[:dimensions])
  48. puts " Image id is #{image[:media_id]}, dimensions are " +
  49. "#{dimensions['FULL'][:height]}x#{dimensions['FULL'][:width]} " +
  50. "and MIME type is \"#{image[:mime_type]}\"."
  51. end
  52. else
  53. puts "No images found."
  54. end
  55. end
  56. if __FILE__ == $0
  57. begin
  58. get_all_images()
  59. # Connection error. Likely transitory.
  60. rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
  61. puts 'Connection Error: %s' % e
  62. puts 'Source: %s' % e.backtrace.first
  63. # API Error.
  64. rescue AdwordsApi::Errors::ApiException => e
  65. puts 'API Exception caught.'
  66. puts 'Message: %s' % e.message
  67. puts 'Code: %d' % e.code if e.code
  68. puts 'Trigger: %s' % e.trigger if e.trigger
  69. puts 'Errors:'
  70. if e.errors
  71. e.errors.each_with_index do |error, index|
  72. puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
  73. error.each_pair do |field, value|
  74. if field != :xsi_type
  75. puts ' %s: %s' % [field, value]
  76. end
  77. end
  78. end
  79. end
  80. end
  81. end