google-dfp-api 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. data/COPYING +201 -0
  2. data/ChangeLog +10 -0
  3. data/README +210 -0
  4. data/Rakefile +83 -0
  5. data/examples/v201101/common/error_handling.rb +76 -0
  6. data/examples/v201101/company_service/create_companies.rb +82 -0
  7. data/examples/v201101/company_service/get_all_companies.rb +92 -0
  8. data/examples/v201101/company_service/get_companies_by_statement.rb +88 -0
  9. data/examples/v201101/company_service/get_company.rb +72 -0
  10. data/examples/v201101/company_service/update_companies.rb +95 -0
  11. data/examples/v201101/creative_service/copy_image_creatives.rb +117 -0
  12. data/examples/v201101/creative_service/create_creatives.rb +109 -0
  13. data/examples/v201101/creative_service/get_all_creatives.rb +93 -0
  14. data/examples/v201101/creative_service/get_creative.rb +72 -0
  15. data/examples/v201101/creative_service/get_creatives_by_statement.rb +87 -0
  16. data/examples/v201101/creative_service/update_creatives.rb +98 -0
  17. data/examples/v201101/custom_targeting_service/create_custom_targeting_keys_and_values.rb +118 -0
  18. data/examples/v201101/custom_targeting_service/delete_custom_targeting_keys.rb +121 -0
  19. data/examples/v201101/custom_targeting_service/delete_custom_targeting_values.rb +125 -0
  20. data/examples/v201101/custom_targeting_service/get_all_custom_targeting_keys_and_values.rb +148 -0
  21. data/examples/v201101/custom_targeting_service/get_custom_targeting_keys_by_statement.rb +92 -0
  22. data/examples/v201101/custom_targeting_service/get_custom_targeting_values_by_statement.rb +98 -0
  23. data/examples/v201101/custom_targeting_service/update_custom_targeting_keys.rb +94 -0
  24. data/examples/v201101/custom_targeting_service/update_custom_targeting_values.rb +106 -0
  25. data/examples/v201101/forecast_service/get_forecast.rb +103 -0
  26. data/examples/v201101/forecast_service/get_forecast_by_id.rb +80 -0
  27. data/examples/v201101/inventory_service/create_ad_units.rb +98 -0
  28. data/examples/v201101/inventory_service/deactivate_ad_units.rb +119 -0
  29. data/examples/v201101/inventory_service/get_ad_unit.rb +72 -0
  30. data/examples/v201101/inventory_service/get_ad_units_by_statement.rb +96 -0
  31. data/examples/v201101/inventory_service/get_all_ad_units.rb +93 -0
  32. data/examples/v201101/inventory_service/update_ad_units.rb +94 -0
  33. data/examples/v201101/line_item_creative_association_service/create_licas.rb +94 -0
  34. data/examples/v201101/line_item_creative_association_service/deactivate_licas.rb +127 -0
  35. data/examples/v201101/line_item_creative_association_service/get_all_licas.rb +95 -0
  36. data/examples/v201101/line_item_creative_association_service/get_lica.rb +77 -0
  37. data/examples/v201101/line_item_creative_association_service/get_licas_by_statement.rb +91 -0
  38. data/examples/v201101/line_item_creative_association_service/update_licas.rb +92 -0
  39. data/examples/v201101/line_item_service/activate_line_items.rb +131 -0
  40. data/examples/v201101/line_item_service/create_line_items.rb +141 -0
  41. data/examples/v201101/line_item_service/get_all_line_items.rb +94 -0
  42. data/examples/v201101/line_item_service/get_line_item.rb +74 -0
  43. data/examples/v201101/line_item_service/get_line_items_by_statement.rb +89 -0
  44. data/examples/v201101/line_item_service/target_custom_criteria.rb +135 -0
  45. data/examples/v201101/line_item_service/update_line_items.rb +110 -0
  46. data/examples/v201101/network_service/get_all_networks.rb +74 -0
  47. data/examples/v201101/network_service/get_current_network.rb +66 -0
  48. data/examples/v201101/order_service/approve_orders.rb +116 -0
  49. data/examples/v201101/order_service/create_orders.rb +93 -0
  50. data/examples/v201101/order_service/get_all_orders.rb +93 -0
  51. data/examples/v201101/order_service/get_order.rb +74 -0
  52. data/examples/v201101/order_service/get_orders_by_statement.rb +90 -0
  53. data/examples/v201101/order_service/update_orders.rb +95 -0
  54. data/examples/v201101/placement_service/create_placements.rb +128 -0
  55. data/examples/v201101/placement_service/deactivate_placements.rb +118 -0
  56. data/examples/v201101/placement_service/get_all_placements.rb +93 -0
  57. data/examples/v201101/placement_service/get_placement.rb +74 -0
  58. data/examples/v201101/placement_service/get_placements_by_statement.rb +86 -0
  59. data/examples/v201101/placement_service/update_placements.rb +96 -0
  60. data/examples/v201101/publisher_query_language_service/get_all_cities.rb +85 -0
  61. data/examples/v201101/publisher_query_language_service/get_all_countries.rb +85 -0
  62. data/examples/v201101/publisher_query_language_service/get_all_metros.rb +85 -0
  63. data/examples/v201101/publisher_query_language_service/get_all_regions.rb +85 -0
  64. data/examples/v201101/report_service/download_report.rb +81 -0
  65. data/examples/v201101/report_service/run_delivery_report.rb +89 -0
  66. data/examples/v201101/report_service/run_inventory_report.rb +90 -0
  67. data/examples/v201101/report_service/run_sales_report.rb +89 -0
  68. data/examples/v201101/user_service/create_users.rb +89 -0
  69. data/examples/v201101/user_service/deactivate_users.rb +99 -0
  70. data/examples/v201101/user_service/get_all_roles.rb +73 -0
  71. data/examples/v201101/user_service/get_all_users.rb +92 -0
  72. data/examples/v201101/user_service/get_user.rb +73 -0
  73. data/examples/v201101/user_service/get_users_by_statement.rb +79 -0
  74. data/examples/v201101/user_service/update_users.rb +89 -0
  75. data/lib/dfp_api.rb +142 -0
  76. data/lib/dfp_api/api_config.rb +126 -0
  77. data/lib/dfp_api/credential_handler.rb +60 -0
  78. data/lib/dfp_api/errors.rb +59 -0
  79. data/lib/dfp_api/extensions.rb +54 -0
  80. data/lib/dfp_api/v201101/company_service.rb +52 -0
  81. data/lib/dfp_api/v201101/company_service_registry.rb +37 -0
  82. data/lib/dfp_api/v201101/creative_service.rb +52 -0
  83. data/lib/dfp_api/v201101/creative_service_registry.rb +37 -0
  84. data/lib/dfp_api/v201101/custom_targeting_service.rb +60 -0
  85. data/lib/dfp_api/v201101/custom_targeting_service_registry.rb +37 -0
  86. data/lib/dfp_api/v201101/forecast_service.rb +36 -0
  87. data/lib/dfp_api/v201101/forecast_service_registry.rb +37 -0
  88. data/lib/dfp_api/v201101/inventory_service.rb +56 -0
  89. data/lib/dfp_api/v201101/inventory_service_registry.rb +37 -0
  90. data/lib/dfp_api/v201101/line_item_creative_association_service.rb +56 -0
  91. data/lib/dfp_api/v201101/line_item_creative_association_service_registry.rb +37 -0
  92. data/lib/dfp_api/v201101/line_item_service.rb +56 -0
  93. data/lib/dfp_api/v201101/line_item_service_registry.rb +37 -0
  94. data/lib/dfp_api/v201101/network_service.rb +40 -0
  95. data/lib/dfp_api/v201101/network_service_registry.rb +37 -0
  96. data/lib/dfp_api/v201101/order_service.rb +56 -0
  97. data/lib/dfp_api/v201101/order_service_registry.rb +37 -0
  98. data/lib/dfp_api/v201101/placement_service.rb +56 -0
  99. data/lib/dfp_api/v201101/placement_service_registry.rb +37 -0
  100. data/lib/dfp_api/v201101/publisher_query_language_service.rb +32 -0
  101. data/lib/dfp_api/v201101/publisher_query_language_service_registry.rb +37 -0
  102. data/lib/dfp_api/v201101/report_service.rb +40 -0
  103. data/lib/dfp_api/v201101/report_service_registry.rb +37 -0
  104. data/lib/dfp_api/v201101/user_service.rb +60 -0
  105. data/lib/dfp_api/v201101/user_service_registry.rb +37 -0
  106. metadata +204 -0
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/ruby
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
+ # This example creates new line items. To determine which line items exist, run
21
+ # get_all_line_items.rb. To determine which orders exist, run
22
+ # get_all_orders.rb. To determine which placements exist, run
23
+ # get_all_placements.rb.
24
+ #
25
+ # Tags: LineItemService.createLineItems
26
+
27
+ require 'rubygems'
28
+ require 'dfp_api'
29
+
30
+ API_VERSION = :v201101
31
+ # Number of line items to create.
32
+ ITEM_COUNT = 5
33
+
34
+ def create_line_items()
35
+ # Get DfpApi instance and load configuration from ~/dfp_api.yml.
36
+ dfp = DfpApi::Api.new
37
+
38
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
39
+ # the configuration file or provide your own logger:
40
+ # dfp.logger = Logger.new('dfp_xml.log')
41
+
42
+ # Get the LineItemService.
43
+ line_item_service = dfp.service(:LineItemService, API_VERSION)
44
+
45
+ # Set the order that all created line items will belong to and the placement
46
+ # ID to target.
47
+ order_id = 'INSERT_ORDER_ID_HERE'.to_i
48
+ targeted_placement_ids = Array.new(ITEM_COUNT) do
49
+ 'INSERT_PLACEMENT_ID_HERE'.to_i
50
+ end
51
+
52
+ # Create inventory targeting.
53
+ inventory_targeting = {:targeted_placement_ids => targeted_placement_ids}
54
+
55
+ # Create geographical targeting.
56
+ geo_targeting = Hash.new
57
+ # Include the US and Quebec, Canada.
58
+ geo_targeting[:targeted_locations] = [
59
+ {:xsi_type => 'CountryLocation', :country_code => 'US'},
60
+ {:xsi_type => 'RegionLocation', :region_code => 'CA-QC'}
61
+ ]
62
+
63
+ # Exclude Chicago and the New York metro area.
64
+ geo_targeting[:excluded_locations] = [
65
+ {:xsi_type => 'CityLocation',
66
+ :city_name => 'Chicago', :country_code => 'US'},
67
+ {:xsi_type => 'MetroLocation', :metro_code => '501'}
68
+ ]
69
+
70
+ # Create targeting.
71
+ targeting = {:geo_targeting => geo_targeting,
72
+ :inventory_targeting => inventory_targeting}
73
+
74
+ # Create an array to store local line item objects.
75
+ line_items = Array.new
76
+ ITEM_COUNT.times do |index|
77
+ line_item = {:name => "Line item #%d" % index,
78
+ :order_id => order_id,
79
+ :targeting => targeting,
80
+ :line_item_type => 'STANDARD',
81
+ :allow_overbook => true}
82
+ # Set the creative rotation type to even.
83
+ line_item[:creative_rotation_type] = 'EVEN'
84
+
85
+ # Set the size of creatives that can be associated with this line item.
86
+ line_item[:creative_sizes] = [
87
+ {:width => 300, :height => 250, :is_aspect_ratio => false}
88
+ ]
89
+
90
+ # Set the length of the line item to run.
91
+ line_item[:start_date_time_type] = 'IMMEDIATELY'
92
+ line_item[:end_date_time] = Time.new + 60 * 60 * 24 * 7
93
+
94
+ # Set the cost per unit to $2.
95
+ line_item[:cost_type] = 'CPM'
96
+ line_item[:cost_per_unit] = {
97
+ :currency_code => 'USD',
98
+ :micro_amount => 2000000
99
+ }
100
+
101
+ # Set the number of units bought to 500,000 so that the budget is $1,000.
102
+ line_item[:units_bought] = 500000
103
+ line_item[:unit_type] = 'IMPRESSIONS'
104
+
105
+ line_items << line_item
106
+ end
107
+
108
+ # Create the line items on the server.
109
+ return_line_items = line_item_service.create_line_items(line_items)
110
+
111
+ if return_line_items
112
+ return_line_items.each do |line_item|
113
+ puts ("Line item with ID: %d, belonging to order ID: %d, " +
114
+ "and named: %s was created.") %
115
+ [line_item[:id], line_item[:order_id], line_item[:name]]
116
+ end
117
+ else
118
+ raise 'No line items were created.'
119
+ end
120
+ end
121
+
122
+ if __FILE__ == $0
123
+ begin
124
+ create_line_items()
125
+
126
+ # HTTP errors.
127
+ rescue AdsCommon::Errors::HttpError => e
128
+ puts "HTTP Error: %s" % e
129
+
130
+ # API errors.
131
+ rescue DfpApi::Errors::ApiException => e
132
+ puts "Message: %s" % e.message
133
+ puts 'Errors:'
134
+ e.errors.each_with_index do |error, index|
135
+ puts "\tError [%d]:" % (index + 1)
136
+ error.each do |field, value|
137
+ puts "\t\t%s: %s" % [field, value]
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/ruby
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
+ # This example gets all line items. To create line items, run
21
+ # create_line_items.rb.
22
+ #
23
+ # Tags: LineItemService.getLineItemsByStatement
24
+
25
+ require 'rubygems'
26
+ require 'dfp_api'
27
+
28
+ API_VERSION = :v201101
29
+ PAGE_SIZE = 500
30
+
31
+ def get_all_line_items()
32
+ # Get DfpApi instance and load configuration from ~/dfp_api.yml.
33
+ dfp = DfpApi::Api.new
34
+
35
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
36
+ # the configuration file or provide your own logger:
37
+ # dfp.logger = Logger.new('dfp_xml.log')
38
+
39
+ # Get the LineItemService.
40
+ line_item_service = dfp.service(:LineItemService, API_VERSION)
41
+
42
+ # Define initial values.
43
+ offset = 0
44
+ page = Hash.new
45
+
46
+ begin
47
+ # Create a statement to get one page with current offset.
48
+ statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
49
+
50
+ # Get line items by statement.
51
+ page = line_item_service.get_line_items_by_statement(statement)
52
+
53
+ if page[:results]
54
+ # Increase query offset by page size.
55
+ offset += PAGE_SIZE
56
+
57
+ # Get the start index for printout.
58
+ start_index = page[:start_index]
59
+
60
+ # Print details about each line item in results.
61
+ page[:results].each_with_index do |line_item, index|
62
+ puts "%d) Line item ID: %d, order ID: %d, name: %s" %
63
+ [index + start_index, line_item[:id],
64
+ line_item[:order_id], line_item[:name]]
65
+ end
66
+ end
67
+ end while offset < page[:total_result_set_size]
68
+
69
+ # Print a footer.
70
+ if page.include?(:total_result_set_size)
71
+ puts "Total number of line items: %d" % page[:total_result_set_size]
72
+ end
73
+ end
74
+
75
+ if __FILE__ == $0
76
+ begin
77
+ get_all_line_items()
78
+
79
+ # HTTP errors.
80
+ rescue AdsCommon::Errors::HttpError => e
81
+ puts "HTTP Error: %s" % e
82
+
83
+ # API errors.
84
+ rescue DfpApi::Errors::ApiException => e
85
+ puts "Message: %s" % e.message
86
+ puts 'Errors:'
87
+ e.errors.each_with_index do |error, index|
88
+ puts "\tError [%d]:" % (index + 1)
89
+ error.each do |field, value|
90
+ puts "\t\t%s: %s" % [field, value]
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/ruby
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
+ # This example gets a line item by its ID. To determine which line items
21
+ # exist, run get_all_line_items.rb.
22
+ #
23
+ # Tags: LineItemService.getLineItem
24
+
25
+ require 'rubygems'
26
+ require 'dfp_api'
27
+
28
+ API_VERSION = :v201101
29
+
30
+ def get_line_item()
31
+ # Get DfpApi instance and load configuration from ~/dfp_api.yml.
32
+ dfp = DfpApi::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
+ # dfp.logger = Logger.new('dfp_xml.log')
37
+
38
+ # Get the LineItemService.
39
+ line_item_service = dfp.service(:LineItemService, API_VERSION)
40
+
41
+ # Set the ID of the line_item to get.
42
+ line_item_id = 'INSERT_LINE_ITEM_ID_HERE'.to_i
43
+
44
+ # Get the line item.
45
+ line_item = line_item_service.get_line_item(line_item_id)
46
+
47
+ if line_item
48
+ puts "Line item with ID: %d, name: %s and order id: %d was found." %
49
+ [line_item[:id], line_item[:name], line_item[:order_id]]
50
+ else
51
+ puts 'No line item found for this ID.'
52
+ end
53
+ end
54
+
55
+ if __FILE__ == $0
56
+ begin
57
+ get_line_item()
58
+
59
+ # HTTP errors.
60
+ rescue AdsCommon::Errors::HttpError => e
61
+ puts "HTTP Error: %s" % e
62
+
63
+ # API errors.
64
+ rescue DfpApi::Errors::ApiException => e
65
+ puts "Message: %s" % e.message
66
+ puts 'Errors:'
67
+ e.errors.each_with_index do |error, index|
68
+ puts "\tError [%d]:" % (index + 1)
69
+ error.each do |field, value|
70
+ puts "\t\t%s: %s" % [field, value]
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/ruby
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
+ # This example gets all line items for the given order. The statement retrieves
21
+ # up to the maximum page size limit of 500. To create line items, run
22
+ # create_line_items.rb. To determine which orders exist, run get_all_orders.rb.
23
+ #
24
+ # Tags: LineItemService.getLineItemsByStatement
25
+
26
+ require 'rubygems'
27
+ require 'dfp_api'
28
+
29
+ API_VERSION = :v201101
30
+
31
+ def get_line_items_by_statement()
32
+ # Get DfpApi instance and load configuration from ~/dfp_api.yml.
33
+ dfp = DfpApi::Api.new
34
+
35
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
36
+ # the configuration file or provide your own logger:
37
+ # dfp.logger = Logger.new('dfp_xml.log')
38
+
39
+ # Get the LineItemService.
40
+ line_item_service = dfp.service(:LineItemService, API_VERSION)
41
+
42
+ # Set the ID of the order to get line items from.
43
+ order_id = 'INSERT_ORDER_ID_HERE'.to_i
44
+
45
+ # Create a statement to only select line items from a given order.
46
+ statement = {
47
+ :query => 'WHERE orderId = :order_id LIMIT 500',
48
+ :values => [
49
+ {:key => 'order_id',
50
+ :value => {:value => order_id, :xsi_type => 'NumberValue'}}
51
+ ]
52
+ }
53
+
54
+ # Get line items by statement.
55
+ page = line_item_service.get_line_items_by_statement(statement)
56
+
57
+ if page and page[:results]
58
+ page[:results].each_with_index do |line_item, index|
59
+ puts "%d) [%d] belongs to order ID %d, name: %s." % [index,
60
+ line_item[:id], line_item[:order_id], line_item[:name]]
61
+ end
62
+ end
63
+
64
+ # Print a footer.
65
+ if page.include?(:total_result_set_size)
66
+ puts "Number of results found: %d" % page[:total_result_set_size]
67
+ end
68
+ end
69
+
70
+ if __FILE__ == $0
71
+ begin
72
+ get_line_items_by_statement()
73
+
74
+ # HTTP errors.
75
+ rescue AdsCommon::Errors::HttpError => e
76
+ puts "HTTP Error: %s" % e
77
+
78
+ # API errors.
79
+ rescue DfpApi::Errors::ApiException => e
80
+ puts "Message: %s" % e.message
81
+ puts 'Errors:'
82
+ e.errors.each_with_index do |error, index|
83
+ puts "\tError [%d]:" % (index + 1)
84
+ error.each do |field, value|
85
+ puts "\t\t%s: %s" % [field, value]
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/ruby
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
+ # This example updates a line item to add custom criteria targeting. To
21
+ # determine which line items exist, run get_all_line_items.rb. To determine
22
+ # which custom targeting keys and values exist, run
23
+ # get_all_custom_targeting_keys_and_values.rb.
24
+ #
25
+ # Tags: LineItemService.getLineItem, LineItemService.updateLineItem
26
+
27
+ require 'rubygems'
28
+ require 'dfp_api'
29
+
30
+ require 'pp'
31
+
32
+ API_VERSION = :v201101
33
+ PAGE_SIZE = 500
34
+
35
+ def target_custom_criteria()
36
+ # Get DfpApi instance and load configuration from ~/dfp_api.yml.
37
+ dfp = DfpApi::Api.new
38
+
39
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
40
+ # the configuration file or provide your own logger:
41
+ # dfp.logger = Logger.new('dfp_xml.log')
42
+
43
+ # Get the LineItemService.
44
+ line_item_service = dfp.service(:LineItemService, API_VERSION)
45
+
46
+ # Set the ID of the line item to update targeting.
47
+ line_item_id = 'INSERT_LINE_ITEM_ID_HERE'.to_i
48
+
49
+ # Set the IDs of the custom targeting keys.
50
+ free_form_custom_targeting_key_id1 =
51
+ 'INSERT_FREE_FORM_CUSTOM_TARGETING_KEY_ID_HERE'.to_i
52
+ free_form_custom_targeting_key_id2 =
53
+ 'INSERT_FREE_FORM_CUSTOM_TARGETING_KEY_ID_HERE'.to_i
54
+
55
+ # Create the free-form custom criteria for targeting "toyota".
56
+ toyota_free_form_custom_criteria = {
57
+ :xsi_type => 'FreeFormCustomCriteria',
58
+ :operator => 'IS',
59
+ :key_id => free_form_custom_targeting_key_id1,
60
+ :values => [{:name => 'toyota', :match_type => 'EXACT'}]
61
+ }
62
+
63
+ # Create the free-form custom criteria for targeting "honda".
64
+ honda_free_form_custom_criteria = {
65
+ :xsi_type => 'FreeFormCustomCriteria',
66
+ :operator => 'IS_NOT',
67
+ :key_id => free_form_custom_targeting_key_id1,
68
+ :values => [{:name => 'honda', :match_type => 'EXACT'}]
69
+ }
70
+
71
+ # Create the free-form custom criteria for targeting "ford".
72
+ ford_free_form_custom_criteria = {
73
+ :xsi_type => 'FreeFormCustomCriteria',
74
+ :operator => 'IS',
75
+ :key_id => free_form_custom_targeting_key_id2,
76
+ :values => [{:name => 'ford', :match_type => 'EXACT'}]
77
+ }
78
+
79
+ # Create the custom criteria set that will resemble:
80
+ #
81
+ # (freeFormCustomTargetingKeyId1 == toyota OR
82
+ # (freeFormCustomTargetingKeyId1 != honda AND
83
+ # freeFormCustomTargetingKeyId2 == ford))
84
+ sub_custom_criteria_set = {
85
+ :xsi_type => 'CustomCriteriaSet',
86
+ :logical_operator => 'AND',
87
+ :children => [honda_free_form_custom_criteria,
88
+ ford_free_form_custom_criteria]
89
+ }
90
+ top_custom_criteria_set = {
91
+ :xsi_type => 'CustomCriteriaSet',
92
+ :logical_operator => 'OR',
93
+ :children => [toyota_free_form_custom_criteria,
94
+ sub_custom_criteria_set]
95
+ }
96
+
97
+ # Get the line item.
98
+ line_item = line_item_service.get_line_item(line_item_id)
99
+
100
+ # Set the custom criteria targeting on the line item.
101
+ line_item[:targeting] = {:custom_targeting => top_custom_criteria_set}
102
+
103
+ # Update the line items on the server.
104
+ return_line_item = line_item_service.update_line_item(line_item)
105
+
106
+ # Display the updated line item.
107
+ if return_line_item
108
+ puts "Line item ID: %d was updated with custom criteria targeting:" %
109
+ return_line_item[:id]
110
+ pp return_line_item[:targeting]
111
+ else
112
+ puts 'Line item update failed.'
113
+ end
114
+ end
115
+
116
+ if __FILE__ == $0
117
+ begin
118
+ target_custom_criteria()
119
+
120
+ # HTTP errors.
121
+ rescue AdsCommon::Errors::HttpError => e
122
+ puts "HTTP Error: %s" % e
123
+
124
+ # API errors.
125
+ rescue DfpApi::Errors::ApiException => e
126
+ puts "Message: %s" % e.message
127
+ puts 'Errors:'
128
+ e.errors.each_with_index do |error, index|
129
+ puts "\tError [%d]:" % (index + 1)
130
+ error.each do |field, value|
131
+ puts "\t\t%s: %s" % [field, value]
132
+ end
133
+ end
134
+ end
135
+ end