Skip to content

Instantly share code, notes, and snippets.

@maier-stefan
Last active October 18, 2022 11:07
Show Gist options
  • Save maier-stefan/b07c9ac8ce042186b188 to your computer and use it in GitHub Desktop.
Save maier-stefan/b07c9ac8ce042186b188 to your computer and use it in GitHub Desktop.
Voluum Api Wrapper
require 'httparty'
require 'json'
class VoluumApiV2
include HTTParty
def urlify_time(time)
time.strftime("%Y-%m-%dT%H") << "%3A00%3A00Z"
end
def urlify_date(time)
time.strftime("%Y-%m-%dT") << "00%3A00%3A00Z"
end
@@token = ""
@@token_expiration = ""
def initialize
@auth = {
username: Rails.application.secrets.voluum_email,
password: Rails.application.secrets.voluum_password
}
check_token
end
def self.token
@@token
end
def self.token_expiration
@@token_expiration
end
def check_token
if @@token_expiration.blank? || @@token_expiration < Time.now
url = "https://security.voluum.com/login"
response = self.class.get url, {:basic_auth => @auth}
@@token_expiration = Time.parse(response["expirationTimestamp"])
@@token = response["token"]
end
end
def get_campaign_offer_reports_by_publisher_id(voluum_campaign_id, voluum_offer_id, date_time_start, date_time_end)
check_token
url = "https://reports.voluum.com/report?from=#{urlify_time(date_time_start)}&to=#{urlify_time(date_time_end)}&tz=Europe%2FBerlin&sort=visits&direction=desc&columns=customVariable1&columns=visits&columns=clicks&columns=conversions&columns=revenue&columns=cost&columns=profit&columns=cpv&columns=cpc&columns=ctr&columns=cr&columns=cv&columns=roi&columns=epv&columns=epc&columns=ap&columns=errors&groupBy=custom-variable-1&offset=0&limit=100&include=active&filter1=campaign&filter1Value=#{voluum_campaign_id}&filter2=offer&filter2Value=#{voluum_offer_id}"
response = self.class.get(url, headers: header_params)
case response.code
when 400..500
puts "#{response.code} now to sleep for 60 seconds"
sleep 60
check_token
response = self.class.get(url, headers: header_params)
end
return response['rows']
end
private
def header_params
{'cwauth-token' => "#{@@token}", 'Content-Type' => 'application/json' }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment