Class: Adzerk::Client

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/adzerk/client.rb

Constant Summary collapse

VERSION =
Gem.loaded_specs['adzerk'].version.to_s
SDK_HEADER_NAME =
'X-Adzerk-Sdk-Version'
SDK_HEADER_VALUE =
"adzerk-management-sdk-ruby:#{VERSION}"
BASE_SLEEP =
0.25
MAX_SLEEP =
5
MAX_ATTEMPTS =
10
DEFAULTS =
{
  :host => ENV["ADZERK_API_HOST"] || 'https://api.adzerk.net/',
  :header => 'X-Adzerk-ApiKey',
  :include_creative_templates => true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#camelize_data, #parse_response, #uncamelize_data

Constructor Details

#initialize(key, opts = {}) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/adzerk/client.rb', line 26

def initialize(key, opts = {})
  @api_key = key
  @config = DEFAULTS.merge!(opts)
  @logins = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'login')
  @sites = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'site')
  @ad_types = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'adtypes', :subendpoint => 'channel', :datakey => 'adtype')
  @flights = Adzerk::Flight.new(:client => self, :endpoint => 'flight')
  @zones = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'zone')
  @campaigns = Adzerk::Campaign.new(:client => self, :endpoint => 'campaign')
  @channels = Adzerk::Channel.new(:client => self, :endpoint => 'channel')
  @priorities = Adzerk::Priority.new(:client => self, :endpoint => 'priority')
  @advertisers = Adzerk::Advertiser.new(:client => self, :endpoint => 'advertiser')
  @publishers = Adzerk::Publisher.new(:client => self, :endpoint => 'publisher')
  @creatives = Adzerk::Creative.new(:client => self, :endpoint => 'creative')
  @creative_maps = Adzerk::CreativeMap.new(:client => self)
  @ads = @creative_maps
  @invitations = Adzerk::Invitation.new(:client => self)
  @reports = Adzerk::Reporting.new(:client => self)
  @channel_site_maps = Adzerk::ChannelSiteMap.new(:client => self)
  @geotargetings = Adzerk::GeoTargeting.new(:client => self, :endpoint => 'geotargeting')
  @sitezonetargetings = Adzerk::SiteZoneTargeting.new(:client => self, :endpoint => 'sitezone')
  @categories = Adzerk::Category.new(:client => self, :endpoint => 'category')
  @instant_counts = Adzerk::InstantCount.new(:client => self)
  @creative_templates = Adzerk::CreativeTemplate.new(:client => self)
  @scheduled_reports = Adzerk::ScheduledReporting.new(:client => self, :endpoint => 'report')
  @day_parts = Adzerk::DayParting.new(:client => self, :endpoint => 'dayparting')
  @distance_targetings = Adzerk::DistanceTargeting.new(:client => self, :endpoint => 'distancetargeting')
end

Instance Attribute Details

#ad_typesObject (readonly)

Returns the value of attribute ad_types.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def ad_types
  @ad_types
end

#adsObject (readonly)

Returns the value of attribute ads.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def ads
  @ads
end

#advertisersObject (readonly)

Returns the value of attribute advertisers.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def advertisers
  @advertisers
end

#campaignsObject (readonly)

Returns the value of attribute campaigns.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def campaigns
  @campaigns
end

#categoriesObject (readonly)

Returns the value of attribute categories.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def categories
  @categories
end

#channel_site_mapsObject (readonly)

Returns the value of attribute channel_site_maps.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def channel_site_maps
  @channel_site_maps
end

#channelsObject (readonly)

Returns the value of attribute channels.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def channels
  @channels
end

#creative_mapsObject (readonly)

Returns the value of attribute creative_maps.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def creative_maps
  @creative_maps
end

#creative_templatesObject (readonly)

Returns the value of attribute creative_templates.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def creative_templates
  @creative_templates
end

#creativesObject (readonly)

Returns the value of attribute creatives.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def creatives
  @creatives
end

#day_partsObject (readonly)

Returns the value of attribute day_parts.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def day_parts
  @day_parts
end

#distance_targetingsObject (readonly)

Returns the value of attribute distance_targetings.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def distance_targetings
  @distance_targetings
end

#flightsObject (readonly)

Returns the value of attribute flights.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def flights
  @flights
end

#geotargetingsObject (readonly)

Returns the value of attribute geotargetings.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def geotargetings
  @geotargetings
end

#instant_countsObject (readonly)

Returns the value of attribute instant_counts.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def instant_counts
  @instant_counts
end

#invitationsObject (readonly)

Returns the value of attribute invitations.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def invitations
  @invitations
end

#loginsObject (readonly)

Returns the value of attribute logins.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def logins
  @logins
end

#prioritiesObject (readonly)

Returns the value of attribute priorities.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def priorities
  @priorities
end

#publishersObject (readonly)

Returns the value of attribute publishers.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def publishers
  @publishers
end

#reportsObject (readonly)

Returns the value of attribute reports.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def reports
  @reports
end

#scheduled_reportsObject (readonly)

Returns the value of attribute scheduled_reports.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def scheduled_reports
  @scheduled_reports
end

#sitesObject (readonly)

Returns the value of attribute sites.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def sites
  @sites
end

#sitezonetargetingsObject (readonly)

Returns the value of attribute sitezonetargetings.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def sitezonetargetings
  @sitezonetargetings
end

#zonesObject (readonly)

Returns the value of attribute zones.



6
7
8
# File 'lib/adzerk/client.rb', line 6

def zones
  @zones
end

Instance Method Details

#create_creative(data = {}, image_path = '', version: 'v1') ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/adzerk/client.rb', line 107

def create_creative(data={}, image_path='', version: 'v1')
  response = nil
  attempt = 0

  loop do
    response = RestClient.post(@config[:host] + version + '/creative',
                              {:creative => camelize_data(data).to_json},
                                :X_Adzerk_ApiKey => @api_key,
                                :X_Adzerk_Sdk_Version => SDK_HEADER_VALUE,
                                :accept => :json)
    break if response.code != 429 or attempt >= (@config[:max_attempts] || MAX_ATTEMPTS)
    sleep(rand(0.0..[MAX_SLEEP, BASE_SLEEP * 2 ** attempt].min()))
    attempt += 1
  end
  response = upload_creative(JSON.parse(response)["Id"], image_path) unless image_path.empty?
  response
end

#delete_request(url, version: 'v1') ⇒ Object



63
64
65
66
67
68
69
# File 'lib/adzerk/client.rb', line 63

def delete_request(url, version: 'v1')
  uri = URI.parse("#{@config[:host]}#{version}/#{url}")
  request = Net::HTTP::Delete.new(uri.request_uri)
  request.add_field(@config[:header], @api_key)
  request.add_field(SDK_HEADER_NAME, SDK_HEADER_VALUE)
  send_request(request, uri)
end

#get_request(url, version: 'v1') ⇒ Object



55
56
57
58
59
60
61
# File 'lib/adzerk/client.rb', line 55

def get_request(url, version: 'v1')
  uri = URI.parse("#{@config[:host]}#{version}/#{url}")
  request = Net::HTTP::Get.new(uri.request_uri)
  request.add_field(@config[:header], @api_key)
  request.add_field(SDK_HEADER_NAME, SDK_HEADER_VALUE)
  send_request(request, uri)
end

#post_json_request(url, data, version: 'v1') ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/adzerk/client.rb', line 80

def post_json_request(url, data, version: 'v1')
  uri = URI.parse("#{@config[:host]}#{version}/#{url}")
  request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
  request.add_field(@config[:header], @api_key)
  request.add_field(SDK_HEADER_NAME, SDK_HEADER_VALUE)
  request.body = data.to_json
  send_request(request, uri)
end

#post_request(url, data, version: 'v1') ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/adzerk/client.rb', line 71

def post_request(url, data, version: 'v1')
  uri = URI.parse("#{@config[:host]}#{version}/#{url}")
  request = Net::HTTP::Post.new(uri.request_uri)
  request.add_field(@config[:header], @api_key)
  request.add_field(SDK_HEADER_NAME, SDK_HEADER_VALUE)
  request.set_form_data(data)
  send_request(request, uri)
end

#put_json_request(url, data, version: 'v1') ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/adzerk/client.rb', line 98

def put_json_request(url, data, version: 'v1')
  uri = URI.parse("#{@config[:host]}#{version}/#{url}")
  request = Net::HTTP::Put.new(uri.request_uri, 'Content-Type' => 'application/json')
  request.add_field(@config[:header], @api_key)
  request.add_field(SDK_HEADER_NAME, SDK_HEADER_VALUE)
  request.body = data.to_json
  send_request(request, uri)
end

#put_request(url, data, version: 'v1') ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/adzerk/client.rb', line 89

def put_request(url, data, version: 'v1')
  uri = URI.parse("#{@config[:host]}#{version}/#{url}")
  request = Net::HTTP::Put.new(uri.request_uri)
  request.add_field(@config[:header], @api_key)
  request.add_field(SDK_HEADER_NAME, SDK_HEADER_VALUE)
  request.set_form_data(data)
  send_request(request, uri)
end

#send_request(request, uri) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/adzerk/client.rb', line 145

def send_request(request, uri)
  response = nil
  attempt = 0
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'

  loop do
    response = http.request(request)
    break if response.code != "429" or attempt >= (@config[:max_attempts] || MAX_ATTEMPTS)
    sleep(rand(0.0..[MAX_SLEEP, BASE_SLEEP * 2 ** attempt].min()))
    attempt += 1
  end

  if response.kind_of? Net::HTTPClientError or response.kind_of? Net::HTTPServerError
    error_response = JSON.parse(response.body)
    msg = error_response["message"] || error_response["Error"] || response.body
    raise Adzerk::ApiError.new(msg)
  end

  response
end

#upload_creative(id, image_path, size_override: false, version: 'v1') ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/adzerk/client.rb', line 125

def upload_creative(id, image_path, size_override: false, version: 'v1')
  response = nil
  attempt = 0
  image = File.new(image_path, 'rb')
  url = @config[:host] + version + '/creative/' + id.to_s + '/upload'
  url += '?sizeOverride=true' if size_override
  loop do
    response = RestClient.post(url,
    {:image => image},
    "X-Adzerk-ApiKey" => @api_key,
    SDK_HEADER_NAME => SDK_HEADER_VALUE,
    :accept => :mime)

    break if response.code != 429 or attempt >= (@config[:max_attempts] || MAX_ATTEMPTS)
    sleep(rand(0.0..[MAX_SLEEP, BASE_SLEEP * 2 ** attempt].min()))
    attempt += 1
  end
  response
end