adzerk 0.2 → 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fbf807615b442c107c0adf872c51fa3d91b55044
4
+ data.tar.gz: c22fccc8609e2999a14f218f4a892f96eb6c98ff
5
+ SHA512:
6
+ metadata.gz: 689ca16190cb2b8880252e0d5120ddfcedecd8a463c7b1260111eb9b7bf50c7cff095a29359d6aa094c123f79e0d911c8f9cd4c34cdb01ad1bf2e55f3e85458a
7
+ data.tar.gz: 686c57f7c0150b20f068550f07a3bc0ab0221a3e5841cd655703dab2b34946e1b0ea5495683ea013f3c5699282bc1000d3c77f76120d4eff79cd336c147ccf0a
@@ -3,6 +3,7 @@ require "net/http"
3
3
  require "active_support/inflector"
4
4
  require "rest_client"
5
5
  require "adzerk/util"
6
+ require "adzerk/errors"
6
7
  require "adzerk/api_endpoint"
7
8
  require "adzerk/advertiser"
8
9
  require "adzerk/flight"
@@ -12,4 +13,7 @@ require "adzerk/publisher"
12
13
  require "adzerk/invitation"
13
14
  require "adzerk/reporting"
14
15
  require "adzerk/channel_site_map"
16
+ require "adzerk/geo_targeting"
17
+ require "adzerk/site_zone_targeting"
18
+ require "adzerk/category"
15
19
  require "adzerk/client"
@@ -0,0 +1,34 @@
1
+ module Adzerk
2
+ class Category
3
+
4
+ include Adzerk::Util
5
+
6
+ attr_reader :client
7
+
8
+ def initialize(args={})
9
+ @client = args[:client]
10
+ end
11
+
12
+ def create(flight_id, data={})
13
+ url = "flight/#{flight_id}/category"
14
+ data = { 'category' => camelize_data(data).to_json }
15
+ parse_response(@client.post_request(url, data))
16
+ end
17
+
18
+ def delete(flight_id, id)
19
+ url = "flight/#{flight_id}/category/#{id}/delete"
20
+ @client.get_request(url)
21
+ end
22
+
23
+ def listAll
24
+ response = client.get_request('categories')
25
+ parse_response(response)
26
+ end
27
+
28
+ def list(flight_id)
29
+ url = "flight/#{flight_id}/categories"
30
+ response = client.get_request(url)
31
+ parse_response(response)
32
+ end
33
+ end
34
+ end
@@ -3,10 +3,10 @@ module Adzerk
3
3
 
4
4
  include Adzerk::Util
5
5
 
6
- attr_reader :sites, :zones, :campaigns, :channels, :priorities,
6
+ attr_reader :sites, :ad_types, :zones, :campaigns, :channels, :priorities,
7
7
  :advertisers, :flights, :creatives, :creative_maps,
8
8
  :publishers, :invitations, :reports, :channel_site_maps,
9
- :logins
9
+ :logins, :geotargetings, :sitezonetargetings, :categories
10
10
 
11
11
  DEFAULTS = {
12
12
  :host => ENV["ADZERK_API_HOST"] || 'http://api.adzerk.net/v1/',
@@ -17,6 +17,7 @@ module Adzerk
17
17
  @api_key = key
18
18
  @config = DEFAULTS.merge!(opts)
19
19
  @sites = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'site')
20
+ @ad_types = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'adtypes')
20
21
  @flights = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'flight')
21
22
  @zones = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'zone')
22
23
  @campaigns = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'campaign')
@@ -30,32 +31,33 @@ module Adzerk
30
31
  @reports = Adzerk::Reporting.new(:client => self)
31
32
  @channel_site_maps = Adzerk::ChannelSiteMap.new(:client => self)
32
33
  @logins = Adzerk::ApiEndpoint.new(:client => self, :endpoint => 'login')
34
+ @geotargetings = Adzerk::GeoTargeting.new(:client => self, :endpoint => 'geotargeting')
35
+ @sitezonetargetings = Adzerk::SiteZoneTargeting.new(:client => self, :endpoint => 'sitezone')
36
+ @categories = Adzerk::Category.new(:client => self, :endpoint => 'category')
37
+
33
38
  end
34
39
 
35
40
  def get_request(url)
36
41
  uri = URI.parse(@config[:host] + url)
37
- http = Net::HTTP.new(uri.host, uri.port)
38
42
  request = Net::HTTP::Get.new(uri.request_uri)
39
43
  request.add_field(@config[:header], @api_key)
40
- http.request(request)
44
+ send_request(request, uri)
41
45
  end
42
46
 
43
47
  def post_request(url, data)
44
48
  uri = URI.parse(@config[:host] + url)
45
- http = Net::HTTP.new(uri.host, uri.port)
46
49
  request = Net::HTTP::Post.new(uri.request_uri)
47
50
  request.add_field(@config[:header], @api_key)
48
51
  request.set_form_data(data)
49
- http.request(request)
52
+ send_request(request, uri)
50
53
  end
51
54
 
52
55
  def put_request(url, data)
53
56
  uri = URI.parse(@config[:host] + url)
54
- http = Net::HTTP.new(uri.host, uri.port)
55
57
  request = Net::HTTP::Put.new(uri.request_uri)
56
58
  request.add_field(@config[:header], @api_key)
57
59
  request.set_form_data(data)
58
- http.request(request)
60
+ send_request(request, uri)
59
61
  end
60
62
 
61
63
  def create_creative(data={}, image_path='')
@@ -76,5 +78,15 @@ module Adzerk
76
78
  :accept => :mime)
77
79
  end
78
80
 
81
+ def send_request(request, uri)
82
+ http = Net::HTTP.new(uri.host, uri.port)
83
+ response = http.request(request)
84
+ if response.kind_of? Net::HTTPClientError
85
+ error_response = JSON.parse(response.body)
86
+ raise Adzerk::ApiError.new(error_response["message"])
87
+ end
88
+ response
89
+ end
90
+
79
91
  end
80
92
  end
@@ -0,0 +1,3 @@
1
+ module Adzerk
2
+ class ApiError < StandardError ; end
3
+ end
@@ -0,0 +1,32 @@
1
+ module Adzerk
2
+ class GeoTargeting
3
+
4
+ include Adzerk::Util
5
+
6
+ def initialize(args={})
7
+ @client = args[:client]
8
+ end
9
+
10
+ def create(flight_id, data={})
11
+ url = "flight/#{flight_id}/geotargeting"
12
+ data = { 'geotargeting' => camelize_data(data).to_json }
13
+ parse_response(@client.post_request(url, data))
14
+ end
15
+
16
+ def get(flight_id, id)
17
+ url = "flight/#{flight_id}/geotargeting/#{id}"
18
+ parse_response(@client.get_request(url))
19
+ end
20
+
21
+ def update(flight_id, id, data={})
22
+ url = "flight/#{flight_id}/geotargeting/#{id}"
23
+ data = { 'geotargeting' => camelize_data(data).to_json }
24
+ parse_response(@client.put_request(url, data))
25
+ end
26
+
27
+ def delete(flight_id, id)
28
+ url = "flight/#{flight_id}/geotargeting/#{id}/delete"
29
+ @client.get_request(url)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ module Adzerk
2
+ class SiteZoneTargeting
3
+
4
+ include Adzerk::Util
5
+
6
+ def initialize(args={})
7
+ @client = args[:client]
8
+ end
9
+
10
+ def create(flight_id, data={})
11
+ url = "flight/#{flight_id}/sitezonetargeting"
12
+ data = { 'sitezone' => camelize_data(data).to_json }
13
+ parse_response(@client.post_request(url, data))
14
+ end
15
+
16
+ def get(flight_id, id)
17
+ url = "flight/#{flight_id}/sitezonetargeting/#{id}"
18
+ parse_response(@client.get_request(url))
19
+ end
20
+
21
+ def update(flight_id, id, data={})
22
+ url = "flight/#{flight_id}/sitezonetargeting/#{id}"
23
+ data = { 'sitezone' => camelize_data(data).to_json }
24
+ parse_response(@client.put_request(url, data))
25
+ end
26
+
27
+ def delete(flight_id, id)
28
+ url = "flight/#{flight_id}/sitezonetargeting/#{id}/delete"
29
+ @client.get_request(url)
30
+ end
31
+
32
+ end
33
+ end
@@ -1,15 +1,16 @@
1
1
  module Adzerk
2
2
  module Util
3
- extend self
3
+ extend self
4
4
 
5
5
  def camelize_data(data)
6
6
  return data unless data.respond_to?(:reduce)
7
7
  data.reduce({}) do |acc, (sym, val)|
8
- acc[sym.to_s.camelize] = case val
9
- when Hash then camelize_data(val)
10
- when Array then val.map { |elem| camelize_data(elem) }
11
- else val
12
- end
8
+ sym = sym.to_s.camelize if sym.class == Symbol
9
+ acc[sym] = case val
10
+ when Hash then camelize_data(val)
11
+ when Array then val.map { |elem| camelize_data(elem) }
12
+ else val
13
+ end
13
14
  acc
14
15
  end
15
16
  end
@@ -28,10 +29,7 @@ module Adzerk
28
29
  end
29
30
 
30
31
  def parse_response(response)
31
- if response.code == "200" or response.code == "400"
32
- uncamelize_data(JSON.parse(response.body))
33
- else
34
- end
32
+ uncamelize_data(JSON.parse(response.body))
35
33
  end
36
34
  end
37
35
  end
@@ -1,3 +1,3 @@
1
1
  module Adzerk
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
  end
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Ad Type API" do
4
+
5
+ before(:each) do
6
+ @client = Adzerk::Client.new(API_KEY)
7
+ end
8
+
9
+ it "should list all ad_types" do
10
+ result = @client.ad_types.list
11
+ expect(result.length).to be > 0
12
+ expect(result[:items].last[:id].to_s).to_not eq(nil)
13
+ expect(result[:items].last[:width]).to_not eq(nil)
14
+ expect(result[:items].last[:height]).to_not eq(nil)
15
+ end
16
+ end
@@ -15,67 +15,63 @@ describe "Advertiser API" do
15
15
  :is_active => $is_active,
16
16
  :is_deleted => $is_deleted)
17
17
  $advertiser_id = advertiser[:id].to_s
18
- $title.should == advertiser[:title]
19
- $is_active.should == advertiser[:is_active]
20
- $is_deleted.should == advertiser[:is_deleted]
18
+ expect($title).to eq(advertiser[:title])
19
+ expect($is_active).to eq(advertiser[:is_active])
20
+ expect($is_deleted).to eq(advertiser[:is_deleted])
21
21
  end
22
22
 
23
23
  it "should list a specific advertiser" do
24
24
  advertiser = @advertisers.get($advertiser_id)
25
- $title.should == advertiser[:title]
26
- $is_active.should == advertiser[:is_active]
27
- $is_deleted.should == advertiser[:is_deleted]
25
+ expect($title).to eq(advertiser[:title])
26
+ expect($is_active).to eq(advertiser[:is_active])
27
+ expect($is_deleted).to eq(advertiser[:is_deleted])
28
28
  end
29
29
 
30
30
  it "should list all advertisers" do
31
31
  result = @advertisers.list
32
- result.length.should > 0
32
+ expect(result.length).to be > 0
33
33
  advertiser = result[:items].last
34
- $title.should == advertiser[:title]
35
- $is_active.should == advertiser[:is_active]
36
- $is_deleted.should == advertiser[:is_deleted]
37
- $advertiser_id.should == advertiser[:id].to_s
34
+ expect($title).to eq(advertiser[:title])
35
+ expect($is_active).to eq(advertiser[:is_active])
36
+ expect($is_deleted).to eq(advertiser[:is_deleted])
37
+ expect($advertiser_id).to eq(advertiser[:id].to_s)
38
38
  end
39
39
 
40
40
  it "should update a advertiser" do
41
41
  advertiser = @advertisers.update(:id => $advertiser_id,
42
42
  :title => "Cocacola",
43
43
  :is_active => false)
44
- advertiser[:title].should eq("Cocacola")
45
- advertiser[:is_active].should eq(false)
44
+ expect(advertiser[:title]).to eq("Cocacola")
45
+ expect(advertiser[:is_active]).to eq(false)
46
46
  end
47
47
 
48
48
  it "should search advertiser based on name" do
49
49
  advertiser = @advertisers.search("Cocacola")
50
- advertiser[:total_items].should > 0
50
+ expect(advertiser[:total_items]).to be > 0
51
51
  end
52
52
 
53
53
  it "should delete a new advertiser" do
54
54
  response = @advertisers.delete($advertiser_id)
55
- response.body.should == '"Successfully deleted."'
55
+ expect(response.body).to eq('"Successfully deleted."')
56
56
  end
57
57
 
58
58
  it "should not list deleted advertisers" do
59
59
  response = @advertisers.list
60
60
  response[:items].each do |r|
61
- r["Id"].should_not == $advertiser_id
61
+ expect(r["Id"]).not_to eq($advertiser_id)
62
62
  end
63
63
  end
64
64
 
65
65
  it "should not get individual deleted advertiser" do
66
- response = @advertisers.get $advertiser_id
67
- response[:message].should eq("This advertiser is deleted.")
66
+ expect { @advertisers.get $advertiser_id }.to raise_error("This advertiser is deleted.")
68
67
  end
69
68
 
70
69
  it "should not update a deleted advertiser" do
71
- advertiser = @advertisers.update(:id => $advertiser_id,
72
- :title => "Cocacola")
73
- advertiser[:message].should eq("This advertiser is deleted.")
70
+ expect { @advertisers.update(:id => $advertiser_id, :title => "Cocacola") }.to raise_error("This advertiser is deleted.")
74
71
  end
75
72
 
76
73
  it "should require a title" do
77
- response = @advertisers.create()
78
- response[:message].should eq("A title is required.")
74
+ expect{ @advertisers.create() }.to raise_error("A title is required.")
79
75
  end
80
76
 
81
77
  end
@@ -48,14 +48,14 @@ describe "Campaign API" do
48
48
  :is_deleted => false)
49
49
 
50
50
  $campaign_id = campaign[:id].to_s
51
- $campaign_name.should == campaign[:name]
51
+ expect($campaign_name).to eq(campaign[:name])
52
52
  # JSON.parse(response.body)["StartDate"].should == "/Date(1293858000000-0500)/"
53
53
  # JSON.parse(response.body)["EndDate"].should == "/Date(1325307600000-0500)/"
54
- $campaign_is_active.should == campaign[:is_active]
55
- $campaign_price.to_f.should == campaign[:price]
56
- $campaign_advertiser_id.should == campaign[:advertiser_id]
57
- campaign[:is_deleted].should == false
58
- campaign[:flights].should eq([])
54
+ expect($campaign_is_active).to eq(campaign[:is_active])
55
+ expect($campaign_price.to_f).to eq(campaign[:price])
56
+ expect($campaign_advertiser_id).to eq(campaign[:advertiser_id])
57
+ expect(campaign[:is_deleted]).to eq(false)
58
+ expect(campaign[:flights]).to eq([])
59
59
  end
60
60
 
61
61
  it "should create a new campaign with one flight" do
@@ -82,22 +82,22 @@ describe "Campaign API" do
82
82
  }
83
83
  campaign = @campaigns.create(new1_campaign)
84
84
  $campaign_id_1 = campaign[:id].to_s
85
- $campaign_name.should == campaign[:name]
85
+ expect($campaign_name).to eq(campaign[:name])
86
86
  # JSON.parse(response.body)["StartDate"].should == "/Date(1293858000000-0500)/"
87
87
  # JSON.parse(response.body)["EndDate"].should == "/Date(1325307600000-0500)/"
88
- $campaign_is_active.should == campaign[:is_active]
89
- campaign[:is_deleted].should == false
90
- $campaign_price.to_f.should == campaign[:price]
91
- $campaign_advertiser_id.should == campaign[:advertiser_id]
92
- campaign[:flights].first[:id].should_not == nil
88
+ expect($campaign_is_active).to eq(campaign[:is_active])
89
+ expect(campaign[:is_deleted]).to eq(false)
90
+ expect($campaign_price.to_f).to eq(campaign[:price])
91
+ expect($campaign_advertiser_id).to eq(campaign[:advertiser_id])
92
+ expect(campaign[:flights].first[:id]).not_to eq(nil)
93
93
  end
94
94
 
95
95
 
96
96
  it "should list a specific campaign" do
97
97
  campaign = @campaigns.get($campaign_id_1)
98
- campaign[:id].should eq($campaign_id_1.to_i)
99
- campaign[:flights].should_not be_empty
100
- campaign[:name].should eq($campaign_name)
98
+ expect(campaign[:id]).to eq($campaign_id_1.to_i)
99
+ expect(campaign[:flights]).not_to be_empty
100
+ expect(campaign[:name]).to eq($campaign_name)
101
101
  end
102
102
 
103
103
  it "should update a campaign" do
@@ -121,34 +121,19 @@ describe "Campaign API" do
121
121
  }
122
122
 
123
123
  campaign = @campaigns.update(campaign_to_update)
124
- campaign[:name].should eq($campaign_name)
124
+ expect(campaign[:name]).to eq($campaign_name)
125
125
  # JSON.parse(response.body)["StartDate"].should == "/Date(1293858000000-0500)/"
126
126
  # JSON.parse(response.body)["EndDate"].should == "/Date(1325307600000-0500)/"
127
- campaign[:is_active].should eq($campaign_is_active)
128
- campaign[:price].should eq($campaign_price.to_f)
129
- campaign[:advertiser_id].should eq($campaign_advertiser_id)
130
- campaign[:is_deleted].should eq(false)
131
- campaign[:flights].should eq($campaign_flights)
127
+ expect(campaign[:is_active]).to eq($campaign_is_active)
128
+ expect(campaign[:price]).to eq($campaign_price.to_f)
129
+ expect(campaign[:advertiser_id]).to eq($campaign_advertiser_id)
130
+ expect(campaign[:is_deleted]).to eq(false)
131
+ expect(campaign[:flights]).to eq($campaign_flights)
132
132
  end
133
133
 
134
134
  it "should list all campaigns" do
135
135
  campaigns = @campaigns.list
136
- campaigns.length.should > 0
137
- end
138
-
139
- it "should delete a new campaign" do
140
- response = @campaigns.delete($campaign_id)
141
- response.body.should == '"Successfully deleted."'
142
- end
143
-
144
- it "should not update a deleted campaign" do
145
- campaign_to_update = {
146
- :id => $campaign_id,
147
- :name => "test"
148
- }
149
-
150
- campaign = @campaigns.update(campaign_to_update)
151
- campaign[:message].should eq("This advertiser is not part of your network")
136
+ expect(campaigns.length).to be > 0
152
137
  end
153
138
 
154
139
  it "should not create/update a campaign with a advertiserId that doesn't belong to it" do
@@ -163,8 +148,7 @@ describe "Campaign API" do
163
148
  :is_deleted => false
164
149
  }
165
150
 
166
- campaign = @campaigns.create(new_campaign)
167
- campaign[:message].should eq("This advertiser is not part of your network")
151
+ expect{ @campaigns.create(new_campaign) }.to raise_error("This advertiser is not part of your network")
168
152
 
169
153
  updated_campaign = {
170
154
  :id => $campaign_id,
@@ -178,13 +162,25 @@ describe "Campaign API" do
178
162
  :is_deleted => false
179
163
  }
180
164
 
181
- campaign = @campaigns.update(updated_campaign)
182
- campaign[:message].should eq("This advertiser is not part of your network")
165
+ expect{ @campaigns.update(updated_campaign) }.to raise_error("This advertiser is not part of your network")
183
166
  end
184
167
 
185
168
  it "should not retrieve a campaign with a advertiserId that doesn't belong to it" do
186
- response = @campaigns.get('123')
187
- response[:message].should eq("This campaign is not part of your network")
169
+ expect { @campaigns.get('123') }.to raise_error("This campaign is not part of your network")
170
+ end
171
+
172
+ it "should delete a new campaign" do
173
+ response = @campaigns.delete($campaign_id)
174
+ expect(response.body).to eq('"Successfully deleted."')
175
+ end
176
+
177
+ it "should not update a deleted campaign" do
178
+ campaign_to_update = {
179
+ :id => $campaign_id,
180
+ :name => "test"
181
+ }
182
+
183
+ expect { @campaigns.update(campaign_to_update) }.to raise_error("This campaign has been deleted")
188
184
  end
189
185
 
190
186
  end