spiceworks-openx 1.0.0

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,51 @@
1
+ module OpenX
2
+ module Services
3
+ class Campaign < Base
4
+ # Translate our property names to OpenX property names
5
+ openx_accessor :name => :campaignName,
6
+ :advertiser_id => :advertiserId,
7
+ :id => :campaignId,
8
+ :start_date => :startDate,
9
+ :end_date => :endDate,
10
+ :impressions => :impressions,
11
+ :target_impressions => :targetImpressions,
12
+ :target_clicks => :targetClicks,
13
+ :revenue => :revenue,
14
+ :revenue_type => :revenueType,
15
+ :impressions => :impressions,
16
+ :clicks => :clicks,
17
+ :priority => :priority,
18
+ :weight => :weight,
19
+ :campaign_type => :campaignType
20
+
21
+ has_one :advertiser
22
+
23
+ self.create = 'ox.addCampaign'
24
+ self.update = 'ox.modifyCampaign'
25
+ self.delete = 'ox.deleteCampaign'
26
+ self.find_one = 'ox.getCampaign'
27
+ self.find_all = 'ox.getCampaignListByAdvertiserId'
28
+
29
+ # Revenue types
30
+ CPM = 1
31
+ CPC = 2
32
+ CPA = 3
33
+ MONTHLY_TENANCY = 4
34
+
35
+ # Campaign types
36
+ REMNANT = 1
37
+ HIGH = 2
38
+ EXCLUSIVE = 3
39
+
40
+ def initialize(params = {})
41
+ raise ArgumentError unless params[:advertiser_id] || params[:advertiser]
42
+ params[:advertiser_id] ||= params[:advertiser].id
43
+ super(params)
44
+ end
45
+
46
+ def banners
47
+ Banner.find(:all, self.id)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,31 @@
1
+ module OpenX
2
+ module Services
3
+ class Publisher < Base
4
+ openx_accessor :name => :publisherName,
5
+ :contact_name => :contactName,
6
+ :email => :emailAddress,
7
+ :username => :username,
8
+ :password => :password,
9
+ :id => :publisherId,
10
+ :agency_id => :agencyId
11
+
12
+ has_one :agency
13
+
14
+ self.create = 'ox.addPublisher'
15
+ self.update = 'ox.modifyPublisher'
16
+ self.delete = 'ox.deletePublisher'
17
+ self.find_one = 'ox.getPublisher'
18
+ self.find_all = 'ox.getPublisherListByAgencyId'
19
+
20
+ def initialize(params = {})
21
+ raise "need agency" unless params[:agency_id] || params[:agency]
22
+ params[:agency_id] ||= params[:agency].id
23
+ super(params)
24
+ end
25
+
26
+ def zones
27
+ Zone.find(:all, self.id)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ module OpenX
2
+ module Services
3
+ class Session
4
+ attr_accessor :url, :id
5
+
6
+ def initialize(url)
7
+ @url = url
8
+ @server = XMLRPC::Client.new2("#{@url}")
9
+ @id = nil
10
+ end
11
+
12
+ def create(user, password)
13
+ @id = @server.call('ox.logon', user, password)
14
+ self
15
+ end
16
+
17
+ def destroy
18
+ @server.call('ox.logoff', @id)
19
+ @id = nil
20
+ self
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,98 @@
1
+ module OpenX
2
+ module Services
3
+ class Zone < Base
4
+ # Delivery types
5
+ BANNER = 'delivery-b'
6
+ INTERSTITIAL = 'delivery-i'
7
+ TEXT = 'delivery-t'
8
+ EMAIL = 'delivery-e'
9
+
10
+ # Tag Types
11
+ JAVASCRIPT = 'adjs'
12
+ LOCAL = 'local'
13
+ IFRAME = 'adframe'
14
+ XMLRPC_TAG = 'xmlrpc'
15
+
16
+ openx_accessor :name => :zoneName,
17
+ :id => :zoneId,
18
+ :width => :width,
19
+ :height => :height,
20
+ :type => :type,
21
+ :publisher_id => :publisherId
22
+
23
+ has_one :publisher
24
+
25
+ self.create = 'ox.addZone'
26
+ self.update = 'ox.modifyZone'
27
+ self.delete = 'ox.deleteZone'
28
+ self.find_one = 'ox.getZone'
29
+ self.find_all = 'ox.getZoneListByPublisherId'
30
+
31
+ class << self
32
+ ###
33
+ # Deliver +zone_id+ to +ip_address+ with +cookies+,
34
+ def deliver zone_id, ip_address = '192.168.1.1', cookies = []
35
+ url = "#{self.configuration['root']}/delivery/axmlrpc.php"
36
+ server = XMLRPC::Client.new2(url)
37
+ server.call('openads.view', {
38
+ 'cookies' => cookies,
39
+ 'remote_addr' => ip_address,
40
+ }, "zone:#{zone_id}", 0, '', '', true, [])
41
+ end
42
+ end
43
+
44
+ def initialize(params = {})
45
+ raise "need publisher" unless params[:publisher_id] || params[:publisher]
46
+ params[:publisher_id] ||= params[:publisher].id
47
+ super(params)
48
+ end
49
+
50
+ # Link this zone to +campaign+
51
+ def link_campaign(campaign)
52
+ raise "Zone must be saved" if new_record?
53
+ raise ArgumentError.new("Campaign must be saved")if campaign.new_record?
54
+
55
+ session = self.class.connection
56
+ server = XMLRPC::Client.new2("#{session.url}")
57
+ server.call("ox.linkCampaign", session.id, self.id, campaign.id)
58
+ end
59
+
60
+ # Unlink this zone from +campaign+
61
+ def unlink_campaign(campaign)
62
+ raise "Zone must be saved" if new_record?
63
+ raise ArgumentError.new("Campaign must be saved")if campaign.new_record?
64
+
65
+ session = self.class.connection
66
+ server = XMLRPC::Client.new2("#{session.url}")
67
+ server.call("ox.unlinkCampaign", session.id, self.id, campaign.id)
68
+ end
69
+
70
+ # Link this zone to +banner+
71
+ def link_banner(banner)
72
+ raise "Zone must be saved" if new_record?
73
+ raise ArgumentError.new("Banner must be saved")if banner.new_record?
74
+
75
+ session = self.class.connection
76
+ server = XMLRPC::Client.new2("#{session.url}")
77
+ server.call("ox.linkBanner", session.id, self.id, banner.id)
78
+ end
79
+
80
+ # Unlink this zone from +banner+
81
+ def unlink_banner(banner)
82
+ raise "Zone must be saved" if new_record?
83
+ raise ArgumentError.new("Banner must be saved")if banner.new_record?
84
+
85
+ session = self.class.connection
86
+ server = XMLRPC::Client.new2("#{session.url}")
87
+ server.call("ox.unlinkBanner", session.id, self.id, banner.id)
88
+ end
89
+
90
+ # Generate tags for displaying this zone using +tag_type+
91
+ def generate_tags(tag_type = IFRAME)
92
+ session = self.class.connection
93
+ server = XMLRPC::Client.new2("#{session.url}")
94
+ server.call("ox.generateTags", session.id, self.id, tag_type, [])
95
+ end
96
+ end
97
+ end
98
+ end
Binary file
Binary file
@@ -0,0 +1,99 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '../lib')))
2
+
3
+ require 'test/unit'
4
+ require 'openx'
5
+
6
+ ENV['OPENX_ENV'] = 'test'
7
+
8
+ module OpenX
9
+ class TestCase < Test::Unit::TestCase
10
+ TEST_SWF = File.expand_path(File.join(File.dirname(__FILE__), 'assets', 'cat.swf'))
11
+ TEST_JPG = File.expand_path(File.join(File.dirname(__FILE__), 'assets', '300x250.jpg'))
12
+
13
+ include OpenX::Services
14
+
15
+ undef :default_test
16
+
17
+ def setup
18
+ assert(
19
+ File.exists?(Base::CONFIGURATION_YAML),
20
+ 'Please create the credentials file'
21
+ )
22
+ end
23
+
24
+ def agency
25
+ @agency ||= Agency.create!(
26
+ {
27
+ :name => "Testing! - #{Time.now}",
28
+ :contact_name => 'Contact Name!',
29
+ :email => 'foo@bar.com'
30
+ }
31
+ )
32
+ end
33
+
34
+ def advertiser
35
+ @advertiser ||= Advertiser.create!(
36
+ {
37
+ :name => "adv-#{Time.now}",
38
+ :contact_name => 'Contact Name!',
39
+ :email => 'foo@bar.com'
40
+ }.merge(:agency => agency)
41
+ )
42
+ end
43
+
44
+ def campaign
45
+ @campaign ||= Campaign.create!(
46
+ {
47
+ :advertiser => advertiser,
48
+ :name => "Campaign-#{Time.now}",
49
+ :impressions => 2000
50
+ }
51
+ )
52
+ end
53
+
54
+ def publisher
55
+ @publisher ||= Publisher.create!(
56
+ {
57
+ :agency => agency,
58
+ :name => "Publisher! - #{Time.now}",
59
+ :contact_name => 'Aaron Patterson',
60
+ :email => 'aaron@tenderlovemaking.com',
61
+ :username => 'one',
62
+ :password => 'two',
63
+ }
64
+ )
65
+ end
66
+
67
+ def zone
68
+ @zone ||= Zone.create!(
69
+ {
70
+ :publisher => publisher,
71
+ :name => "Zone - #{Time.now}",
72
+ :type => Zone::BANNER,
73
+ :width => 300,
74
+ :height => 250,
75
+ }
76
+ )
77
+ end
78
+
79
+ def banner
80
+ @banner ||= Banner.create!({
81
+ :name => "Banner-#{Time.now}",
82
+ :storage_type => Banner::LOCAL_SQL,
83
+ :campaign => campaign,
84
+ :url => 'http://tenderlovemaking.com/',
85
+ :file_name => 'oogabooga',
86
+ :image => OpenX::Image.new(File.open(TEST_SWF, 'rb'))
87
+ })
88
+ end
89
+
90
+ def destroy
91
+ @banner.destroy if defined? @banner
92
+ @zone.destroy if defined? @zone
93
+ @publisher.destroy if defined? @publisher
94
+ @campaign.destroy if defined? @campaign
95
+ @advertiser.destroy if defined? @advertiser
96
+ @agency.destroy if defined? @agency
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,82 @@
1
+ require 'helper'
2
+
3
+ class AdvertiserTest < OpenX::TestCase
4
+ def test_create_advertiser
5
+ advertiser = nil
6
+ assert_nothing_raised {
7
+ advertiser = Advertiser.create!(init_params.merge({
8
+ :agency => agency,
9
+ }))
10
+ }
11
+ assert_not_nil advertiser
12
+ init_params.each do |k,v|
13
+ assert_equal(v, advertiser.send(:"#{k}"))
14
+ end
15
+ end
16
+
17
+ def test_destroy
18
+ a = nil
19
+ assert_nothing_raised {
20
+ a = Advertiser.create!(init_params.merge({:agency => agency}))
21
+ }
22
+ id = a.id
23
+ assert_nothing_raised {
24
+ a.destroy
25
+ }
26
+ assert_raises(XMLRPC::FaultException) {
27
+ Advertiser.find(id)
28
+ }
29
+ end
30
+
31
+ def test_create_advertiser_with_agency
32
+ advertiser = nil
33
+ assert_nothing_raised {
34
+ advertiser = agency.create_advertiser!(init_params)
35
+ }
36
+ assert_not_nil advertiser
37
+ init_params.each do |k,v|
38
+ assert_equal(v, advertiser.send(:"#{k}"))
39
+ end
40
+ end
41
+
42
+ def test_find_advertiser
43
+ advertiser = nil
44
+ assert_nothing_raised {
45
+ advertiser = agency.create_advertiser!(init_params)
46
+ }
47
+ assert_not_nil advertiser
48
+
49
+ original = advertiser
50
+ advertiser = Advertiser.find(advertiser.id)
51
+ assert_equal(original, advertiser)
52
+ assert_not_nil advertiser
53
+ assert_not_nil advertiser.agency
54
+ init_params.each do |k,v|
55
+ assert_equal(v, advertiser.send(:"#{k}"))
56
+ end
57
+ end
58
+
59
+ def test_find_all_advertisers
60
+ advertiser = nil
61
+ assert_nothing_raised {
62
+ advertiser = agency.create_advertiser!(init_params)
63
+ }
64
+ assert_not_nil advertiser
65
+
66
+ advertisers = Advertiser.find(:all, agency.id)
67
+
68
+ advertiser = advertisers.find { |a| a.id == advertiser.id }
69
+ assert_not_nil advertiser
70
+ init_params.each do |k,v|
71
+ assert_equal(v, advertiser.send(:"#{k}"))
72
+ end
73
+ end
74
+
75
+ def init_params
76
+ {
77
+ :name => 'Test advertiser',
78
+ :contact_name => 'Contact name',
79
+ :email => 'email@example.com',
80
+ }
81
+ end
82
+ end
@@ -0,0 +1,94 @@
1
+ require 'helper'
2
+
3
+ class AgencyTest < OpenX::TestCase
4
+ def destroy
5
+ Agency.find(:all).each do |agency|
6
+ agency.destroy if agency.name == init_params[:name]
7
+ end
8
+ super
9
+ end
10
+
11
+ def test_create!
12
+ a = nil
13
+ assert_nothing_raised {
14
+ a = Agency.create!(init_params)
15
+ }
16
+ assert_not_nil a
17
+ assert_not_nil a.id
18
+ init_params.each { |k,v|
19
+ assert_equal(v, a.send(:"#{k}"))
20
+ }
21
+ end
22
+
23
+ def test_modify
24
+ a = nil
25
+ assert_nothing_raised {
26
+ a = Agency.create!(init_params)
27
+ }
28
+ assert_not_nil a
29
+ assert_not_nil a.id
30
+ a.name = 'Awesome name!'
31
+ a.save!
32
+
33
+ a = Agency.find(a.id)
34
+ assert_equal('Awesome name!', a.name)
35
+ a.destroy
36
+ end
37
+
38
+ def test_find
39
+ a = nil
40
+ assert_nothing_raised {
41
+ a = Agency.create!(init_params)
42
+ }
43
+ a = Agency.find(a.id)
44
+ init_params.each { |k,v|
45
+ assert_equal(v, a.send(:"#{k}"))
46
+ }
47
+ end
48
+
49
+ def test_find_all
50
+ a = nil
51
+ assert_nothing_raised {
52
+ a = Agency.create!(init_params)
53
+ }
54
+ list = Agency.find(:all)
55
+ assert list.all? { |x| x.is_a?(Agency) }
56
+ assert list.any? { |x| x.name == init_params[:name] }
57
+ end
58
+
59
+ def test_destroy
60
+ a = nil
61
+ assert_nothing_raised {
62
+ a = Agency.create!(init_params)
63
+ }
64
+ id = a.id
65
+ assert_nothing_raised {
66
+ a.destroy
67
+ }
68
+ assert_raises(XMLRPC::FaultException) {
69
+ Agency.find(id)
70
+ }
71
+ end
72
+
73
+ def test_static_destroy
74
+ a = nil
75
+ assert_nothing_raised {
76
+ a = Agency.create!(init_params)
77
+ }
78
+ id = a.id
79
+ assert_nothing_raised {
80
+ Agency.destroy(id)
81
+ }
82
+ assert_raises(XMLRPC::FaultException) {
83
+ Agency.find(id)
84
+ }
85
+ end
86
+
87
+ def init_params
88
+ {
89
+ :name => 'Testing!',
90
+ :contact_name => 'Contact Name!',
91
+ :email => 'foo@bar.com'
92
+ }
93
+ end
94
+ end