Class: Adzerk::Client
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
-
#ad_types ⇒ Object
readonly
Returns the value of attribute ad_types.
-
#ads ⇒ Object
readonly
Returns the value of attribute ads.
-
#advertisers ⇒ Object
readonly
Returns the value of attribute advertisers.
-
#campaigns ⇒ Object
readonly
Returns the value of attribute campaigns.
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#channel_site_maps ⇒ Object
readonly
Returns the value of attribute channel_site_maps.
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#creative_maps ⇒ Object
readonly
Returns the value of attribute creative_maps.
-
#creative_templates ⇒ Object
readonly
Returns the value of attribute creative_templates.
-
#creatives ⇒ Object
readonly
Returns the value of attribute creatives.
-
#day_parts ⇒ Object
readonly
Returns the value of attribute day_parts.
-
#distance_targetings ⇒ Object
readonly
Returns the value of attribute distance_targetings.
-
#flights ⇒ Object
readonly
Returns the value of attribute flights.
-
#geotargetings ⇒ Object
readonly
Returns the value of attribute geotargetings.
-
#instant_counts ⇒ Object
readonly
Returns the value of attribute instant_counts.
-
#invitations ⇒ Object
readonly
Returns the value of attribute invitations.
-
#logins ⇒ Object
readonly
Returns the value of attribute logins.
-
#priorities ⇒ Object
readonly
Returns the value of attribute priorities.
-
#publishers ⇒ Object
readonly
Returns the value of attribute publishers.
-
#reports ⇒ Object
readonly
Returns the value of attribute reports.
-
#scheduled_reports ⇒ Object
readonly
Returns the value of attribute scheduled_reports.
-
#sites ⇒ Object
readonly
Returns the value of attribute sites.
-
#sitezonetargetings ⇒ Object
readonly
Returns the value of attribute sitezonetargetings.
-
#zones ⇒ Object
readonly
Returns the value of attribute zones.
Instance Method Summary collapse
- #create_creative(data = {}, image_path = '', version: 'v1') ⇒ Object
- #delete_request(url, version: 'v1') ⇒ Object
- #filter(url, version: 'v1') ⇒ Object
- #get_request(url, version: 'v1') ⇒ Object
-
#initialize(key, opts = {}) ⇒ Client
constructor
A new instance of Client.
- #post_json_request(url, data, version: 'v1') ⇒ Object
- #post_request(url, data, version: 'v1') ⇒ Object
- #put_json_request(url, data, version: 'v1') ⇒ Object
- #put_request(url, data, version: 'v1') ⇒ Object
- #send_request(request, uri) ⇒ Object
- #upload_creative(id, image_path, size_override: false, version: 'v1') ⇒ Object
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::Site.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_types ⇒ Object (readonly)
Returns the value of attribute ad_types.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def ad_types @ad_types end |
#ads ⇒ Object (readonly)
Returns the value of attribute ads.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def ads @ads end |
#advertisers ⇒ Object (readonly)
Returns the value of attribute advertisers.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def advertisers @advertisers end |
#campaigns ⇒ Object (readonly)
Returns the value of attribute campaigns.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def campaigns @campaigns end |
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def categories @categories end |
#channel_site_maps ⇒ Object (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 |
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def channels @channels end |
#creative_maps ⇒ Object (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_templates ⇒ Object (readonly)
Returns the value of attribute creative_templates.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def creative_templates @creative_templates end |
#creatives ⇒ Object (readonly)
Returns the value of attribute creatives.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def creatives @creatives end |
#day_parts ⇒ Object (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_targetings ⇒ Object (readonly)
Returns the value of attribute distance_targetings.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def distance_targetings @distance_targetings end |
#flights ⇒ Object (readonly)
Returns the value of attribute flights.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def flights @flights end |
#geotargetings ⇒ Object (readonly)
Returns the value of attribute geotargetings.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def geotargetings @geotargetings end |
#instant_counts ⇒ Object (readonly)
Returns the value of attribute instant_counts.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def instant_counts @instant_counts end |
#invitations ⇒ Object (readonly)
Returns the value of attribute invitations.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def invitations @invitations end |
#logins ⇒ Object (readonly)
Returns the value of attribute logins.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def logins @logins end |
#priorities ⇒ Object (readonly)
Returns the value of attribute priorities.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def priorities @priorities end |
#publishers ⇒ Object (readonly)
Returns the value of attribute publishers.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def publishers @publishers end |
#reports ⇒ Object (readonly)
Returns the value of attribute reports.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def reports @reports end |
#scheduled_reports ⇒ Object (readonly)
Returns the value of attribute scheduled_reports.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def scheduled_reports @scheduled_reports end |
#sites ⇒ Object (readonly)
Returns the value of attribute sites.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def sites @sites end |
#sitezonetargetings ⇒ Object (readonly)
Returns the value of attribute sitezonetargetings.
6 7 8 |
# File 'lib/adzerk/client.rb', line 6 def sitezonetargetings @sitezonetargetings end |
#zones ⇒ Object (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
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/adzerk/client.rb', line 134 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 |
#filter(url, version: 'v1') ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/adzerk/client.rb', line 107 def filter(url, version: 'v1') uri = URI.parse("#{@config[:host]}#{version}/#{url}") Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |https| request = Net::HTTP::Get.new uri request.add_field(@config[:header], @api_key) request.add_field(SDK_HEADER_NAME, SDK_HEADER_VALUE) https.request request do |response| arr = [] response.read_body do |segment| str = '' str.concat(segment) split_str = str.split("\n") for line in split_str do begin obj = JSON.parse(line) rescue => exception str.concat(line) else arr.append(obj) end end return arr end end end 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
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/adzerk/client.rb', line 172 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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/adzerk/client.rb', line 152 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 |