-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtbmob.rb
48 lines (42 loc) · 1.33 KB
/
rtbmob.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
class AdtekioAdnetworks::Cost::Rtbcom
include AdtekioAdnetworks::CostImport
define_required_credentials do
[:advertiser_id, :secret_key]
end
def csv_data(from, till)
uri = Addressable::URI.parse("https://api.manage.com/1/stats.php")
uri.query_values = {
:advertiser_id => credentials.advertiser_id,
:advertiser_key => credentials.secret_key,
:date_from => from.strftime("%Y-%m-%d"),
:date_to => till.strftime("%Y-%m-%d"),
:type => "campaign",
:break_by => "date",
}
http = Net::HTTP::Persistent.new('rtbcom')
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = http.request(URI.parse(uri.to_s))
if request.code =~ /3../
http.request(URI.parse(request.header["Location"])).body
else
request.body
end
end
def campaign_costs(from, till)
csv_options = {
:col_sep => ",",
:quote_char => '"',
:headers => :first_row,
}
CSV.new(csv_data(from,till), csv_options).to_a.map do |row|
{
:date => Date.strptime(row["date"], "%Y-%m-%d"),
:campaign => row['campaign'],
:impressions => row['impression'].to_i,
:clicks => row['click'].to_i,
:conversions => row['install'].to_i,
:amount => row["cost"].to_f,
}
end
end
end