-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.rb
95 lines (80 loc) · 2.75 KB
/
example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true
require 'nielsen_dar_api'
require 'date'
require 'json'
NielsenDarApi.configure do |config|
config.username = 'email'
config.password = 'password'
config.basic_token = 'Basic auth token provided by Nielsen'
end
class SampleClient
include NielsenDarApi::Helper
end
client = SampleClient.new
## PROCESS REFERENCES
# process_available_campaigns
available_campaigns = client.available_campaign_list
# process_demographics
demographics = client.demographic_list
# process_platforms
platforms = client.platform_list
# process_market_areas
market_areas = client.market_area_list
# at this point it would be usefult to store somewhere the result
unless 2 > 1
File.open('available_campaigns.json', 'w') do |f|
f.write(JSON.pretty_generate(available_campaigns))
end
end
# There are a few manipulations to do on available_campaigns list
campaigns_map = Hash[available_campaigns.map do |h|
[h['campaignId'], Date.strptime(h['reportDate'], NielsenDarApi.configuration.date_format)]
end]
report_date = campaigns_map.values.max
# process_campaigns
campaigns = client.campaign_list(report_date)
# process_placements
# you should process the campaign list in slices to prevent 429 Too Many Requests Error
chunk_size = 10
sites = []
campaigns_map.each_slice(chunk_size) do |chunk|
sites << client.site_list(chunk)
end
## PROCESS EXPOSURES
# process_campaign_exposures
# retrieves all data for campaigns exposure
campaign_data = []
campaigns_map.each_slice(chunk_size) do |chunk|
campaign_data << client.campaign_exposure_list(chunk)
end
campaign_data.flatten!
# process_placement_exposures
placement_data = []
campaigns_map.each_slice(chunk_size) do |chunk|
placement_data << client.placement_exposure_list(chunk)
end
placement_data.flatten!
# process_placement_daily_data
# This is pretty tricky
report_dates = Hash[available_campaigns.map do |h|
[h['campaignId'], Date.strptime(h['reportDate'], NielsenDarApi.configuration.date_format)]
end]
start_dates = Hash[campaigns.map do |h|
[h['campaignId'], Date.strptime(h['campaignStartDate'], NielsenDarApi.configuration.date_format)]
end]
end_dates = Hash[campaigns.map do |h|
[h['campaignId'], Date.strptime(h['campaignEndDate'], NielsenDarApi.configuration.date_format)]
end]
max_dates = {} # if already collected and stored somewhere
placement_daily_data = []
report_dates.each do |original_id, rep_date|
client.refresh_access_token
start_date = [start_dates.fetch(original_id), max_dates[original_id]].compact.max
end_date = [end_dates.fetch(original_id), rep_date].min
range_dates = (start_date..end_date).to_a
range_dates.each_slice(5) do |date_slice|
list = client.placement_daily_datum_list(original_id, date_slice)
placement_daily_data << list
end
end
placement_daily_data.flatten!