Created
September 17, 2015 14:15
-
-
Save bcg/ba110ca3310918589da0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'dfp_api' | |
require 'dfp_api_statement' | |
require 'pp' | |
API_VERSION = :v201508 | |
def get_all_ad_units() | |
dfp = DfpApi::Api.new | |
inventory_service = dfp.service(:InventoryService, API_VERSION) | |
statement = DfpApiStatement::FilterStatement.new('ORDER BY id ASC') | |
begin | |
page = inventory_service.get_ad_units_by_statement(statement.toStatement()) | |
if page[:results] | |
page[:results].each_with_index do |ad_unit, index| | |
#pp ad_unit | |
puts "#{ad_unit[:id]}\t#{ad_unit[:name]}\t#{ad_unit[:ad_unit_code]}" | |
ad_unit[:ad_unit_sizes].each do |size| | |
puts " #{size[:environment_type]} #{size[:full_display_string]}" | |
end if ad_unit[:ad_unit_sizes] | |
end | |
end | |
statement.offset += DfpApiStatement::SUGGESTED_PAGE_LIMIT | |
end while statement.offset < page[:total_result_set_size] | |
# Print a footer. | |
if page.include?(:total_result_set_size) | |
puts "Total number of ad units: %d" % page[:total_result_set_size] | |
end | |
end | |
if __FILE__ == $0 | |
begin | |
get_all_ad_units() | |
# HTTP errors. | |
rescue AdsCommon::Errors::HttpError => e | |
puts "HTTP Error: %s" % e | |
# API errors. | |
rescue DfpApi::Errors::ApiException => e | |
puts "Message: %s" % e.message | |
puts 'Errors:' | |
e.errors.each_with_index do |error, index| | |
puts "\tError [%d]:" % (index + 1) | |
error.each do |field, value| | |
puts "\t\t%s: %s" % [field, value] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment