Class: Adzerk::Client

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

Constant Summary collapse

DEFAULTS =
{
  :host => ENV["ADZERK_API_HOST"] || 'http://api.adzerk.net/v1/',
  :header => 'X-Adzerk-ApiKey'
}

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.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/adzerk/client.rb', line 16

def initialize(key, opts = {})
  @api_key = key
  @config = DEFAULTS.merge!(opts)
  @sites = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'site')
  @ad_types = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'adtypes')
  @flights = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'flight')
  @zones = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'zone')
  @campaigns = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'campaign')
  @channels = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'channel')
  @priorities = Adzerk::ApiEndpoint.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)
  @invitations = Adzerk::Invitation.new(:client => self)
  @reports = Adzerk::Reporting.new(:client => self)
  @channel_site_maps = Adzerk::ChannelSiteMap.new(:client => self)
  @logins = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'login')
  @geotargetings = Adzerk::GeoTargeting.new(:client => self, :endpoint => 'geotargeting')
  @sitezonetargetings = Adzerk::SiteZoneTargeting.new(:client => self, :endpoint => 'sitezone')
  @categories = Adzerk::Category.new(:client => self, :endpoint => 'category')

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

#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

#creativesObject (readonly)

Returns the value of attribute creatives.



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

def creatives
  @creatives
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

#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

#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 = '') ⇒ Object



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

def create_creative(data={}, image_path='')      
  response = RestClient.post(@config[:host] + 'creative',
                             {:creative => camelize_data(data).to_json},
                             :X_Adzerk_ApiKey => @api_key,
                             :content_type => :json, 
                             :accept => :json)
  response = upload_creative(JSON.parse(response)["Id"], image_path) unless image_path.empty?
  response
end

#get_request(url) ⇒ Object



40
41
42
43
44
45
# File 'lib/adzerk/client.rb', line 40

def get_request(url)
  uri = URI.parse(@config[:host] + url)
  request = Net::HTTP::Get.new(uri.request_uri)
  request.add_field(@config[:header], @api_key)
  send_request(request, uri)
end

#post_request(url, data) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/adzerk/client.rb', line 47

def post_request(url, data)
  uri = URI.parse(@config[:host] + url)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.add_field(@config[:header], @api_key)
  request.set_form_data(data)
  send_request(request, uri)
end

#put_request(url, data) ⇒ Object



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

def put_request(url, data)
  uri = URI.parse(@config[:host] + url)
  request = Net::HTTP::Put.new(uri.request_uri)
  request.add_field(@config[:header], @api_key)
  request.set_form_data(data)
  send_request(request, uri)
end

#send_request(request, uri) ⇒ Object



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

def send_request(request, uri)
  http = Net::HTTP.new(uri.host, uri.port)
  response = http.request(request)
  if response.kind_of? Net::HTTPClientError
    error_response = JSON.parse(response.body)
    raise Adzerk::ApiError.new(error_response["message"])
  end
  response
end

#upload_creative(id, image_path) ⇒ Object



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

def upload_creative(id, image_path)
  image = File.new(image_path, 'rb')
  RestClient.post(@config[:host] + 'creative/' + id.to_s + '/upload',
  {:image => image},
  "X-Adzerk-ApiKey" => @api_key,
  :accept => :mime)
end