openx 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2008-04-07
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
@@ -0,0 +1,26 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/openx.rb
6
+ lib/openx/image.rb
7
+ lib/openx/services.rb
8
+ lib/openx/services/advertiser.rb
9
+ lib/openx/services/agency.rb
10
+ lib/openx/services/banner.rb
11
+ lib/openx/services/base.rb
12
+ lib/openx/services/campaign.rb
13
+ lib/openx/services/publisher.rb
14
+ lib/openx/services/session.rb
15
+ lib/openx/services/zone.rb
16
+ test/assets/300x250.jpg
17
+ test/assets/cat.swf
18
+ test/helper.rb
19
+ test/test_advertiser.rb
20
+ test/test_agency.rb
21
+ test/test_banner.rb
22
+ test/test_base.rb
23
+ test/test_campaign.rb
24
+ test/test_publisher.rb
25
+ test/test_session.rb
26
+ test/test_zone.rb
@@ -0,0 +1,80 @@
1
+ = openx
2
+
3
+ * http://openx.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ A Ruby interface to the OpenX XML-RPC API.
8
+
9
+ == SYNOPSIS:
10
+
11
+ OpenX::Services::Base.configuration = {
12
+ 'username' => 'admin',
13
+ 'password' => 'password',
14
+ 'url' => 'http://localhost/www/api/v1/xmlrpc',
15
+ }
16
+
17
+ OpenX::Services::Agency.find(:all).each do |agency|
18
+ puts agency.name
19
+
20
+ # Look up publishers
21
+ agency.publishers.each do |publisher|
22
+ puts "-- #{publisher.name}"
23
+ end
24
+
25
+ # Create a publisher
26
+ Publisher.create!(
27
+ :agency => agency,
28
+ :name => 'My Test Publisher',
29
+ :contact_name => 'Aaron Patterson',
30
+ :email => 'aaron@tenderlovemaking.com',
31
+ :username => 'user',
32
+ :password => 'password'
33
+ )
34
+ end
35
+
36
+ == REQUIREMENTS:
37
+
38
+ * ruby
39
+
40
+ == INSTALL:
41
+
42
+ * sudo gem install openx
43
+ * Update your $HOME/.openx/credentials.yml file. Here is a sample:
44
+
45
+ ---
46
+ production:
47
+ username: admin
48
+ password: admin
49
+ url: http://localhost/~aaron/openx-2.5.70-beta/www/api/v1/xmlrpc
50
+
51
+ The YAML file lists configuration for each environment. The gem uses the
52
+ 'production' environment by default.
53
+
54
+ == LICENSE:
55
+
56
+ (The MIT License)
57
+
58
+ Copyright (c) 2008:
59
+
60
+ * {Aaron Patterson}[http://tenderlovemaking.com]
61
+ * Andy Smith
62
+
63
+ Permission is hereby granted, free of charge, to any person obtaining
64
+ a copy of this software and associated documentation files (the
65
+ 'Software'), to deal in the Software without restriction, including
66
+ without limitation the rights to use, copy, modify, merge, publish,
67
+ distribute, sublicense, and/or sell copies of the Software, and to
68
+ permit persons to whom the Software is furnished to do so, subject to
69
+ the following conditions:
70
+
71
+ The above copyright notice and this permission notice shall be
72
+ included in all copies or substantial portions of the Software.
73
+
74
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
75
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
76
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
77
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
78
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
79
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
80
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ $: << "lib/"
7
+ require 'openx'
8
+
9
+ HOE = Hoe.new('openx', OpenX::VERSION) do |p|
10
+ # p.rubyforge_name = 'ruby-openxx' # if different than lowercase project name
11
+ p.developer('Aaron Patterson', 'aaron.patterson@gmail.com')
12
+ end
13
+
14
+ namespace :openx do
15
+ task :clean do
16
+ include OpenX::Services
17
+ ENV['OPENX_ENV'] = 'test'
18
+ Agency.find(:all) do |agency|
19
+ Advertiser.find(:all, agency.id).each do |advertiser|
20
+ Campaign.find(:all, advertiser.id).each do |campaign|
21
+ Banner.find(:all, campaign.id).each do |banner|
22
+ banner.destroy
23
+ end
24
+ campaign.destroy
25
+ end
26
+ advertiser.destroy
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ namespace :gem do
33
+ task :spec do
34
+ File.open("#{HOE.name}.gemspec", 'w') do |f|
35
+ f.write(HOE.spec.to_ruby)
36
+ end
37
+ end
38
+ end
39
+
40
+ # vim: syntax=Ruby
@@ -0,0 +1,7 @@
1
+ require 'xmlrpc/client'
2
+ require 'openx/services'
3
+ require 'openx/image'
4
+
5
+ module OpenX
6
+ VERSION = '1.0.0'
7
+ end
@@ -0,0 +1,22 @@
1
+ module OpenX
2
+ class Image
3
+ include XMLRPC::Marshallable
4
+ attr_accessor :filename, :content, :editswf
5
+
6
+ def initialize(hash_or_file)
7
+ @editswf = 0
8
+ if hash_or_file.is_a?(File)
9
+ @filename = File.basename(hash_or_file.path)
10
+ @editswf = File.basename(@filename, '.swf') == @filename ? 0 : 1
11
+ @content = XMLRPC::Base64.new(hash_or_file.read)
12
+ else
13
+ raise ArgumentError unless hash_or_file.key?(:filename)
14
+ raise ArgumentError unless hash_or_file.key?(:content)
15
+ hash_or_file.each { |k,v| send(:"#{k}=", v) }
16
+ unless self.content.is_a?(XMLRPC::Base64)
17
+ self.content = XMLRPC::Base64.new(self.content)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ require 'openx/services/base'
2
+ require 'openx/services/session'
3
+ require 'openx/services/advertiser'
4
+ require 'openx/services/agency'
5
+ require 'openx/services/campaign'
6
+ require 'openx/services/banner'
7
+ require 'openx/services/publisher'
8
+ require 'openx/services/zone'
@@ -0,0 +1,32 @@
1
+ module OpenX
2
+ module Services
3
+ class Advertiser < Base
4
+ openx_accessor :name => :advertiserName,
5
+ :contact_name => :contactName,
6
+ :email => :emailAddress,
7
+ :username => :username,
8
+ :password => :password,
9
+ :agency_id => :agencyId,
10
+ :id => :advertiserId
11
+
12
+ has_one :agency
13
+
14
+ self.endpoint = '/AdvertiserXmlRpcService.php'
15
+ self.create = 'addAdvertiser'
16
+ self.update = 'modifyAdvertiser'
17
+ self.delete = 'deleteAdvertiser'
18
+ self.find_one = 'getAdvertiser'
19
+ self.find_all = 'getAdvertiserListByAgencyId'
20
+
21
+ def initialize(params = {})
22
+ raise "need agency" unless params[:agency_id] || params[:agency]
23
+ params[:agency_id] ||= params[:agency].id
24
+ super(params)
25
+ end
26
+
27
+ def campaigns
28
+ Campaign.find(:all, self.id)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ module OpenX
2
+ module Services
3
+ class Agency < Base
4
+ # Translate our property names to OpenX property names
5
+ openx_accessor :name => :agencyName,
6
+ :contact_name => :contactName,
7
+ :email => :emailAddress,
8
+ :username => :username,
9
+ :password => :password,
10
+ :id => :agencyId,
11
+ :account_id => :accountId
12
+
13
+ self.endpoint = '/AgencyXmlRpcService.php'
14
+ self.create = 'addAgency'
15
+ self.update = 'modifyAgency'
16
+ self.delete = 'deleteAgency'
17
+ self.find_one = 'getAgency'
18
+ self.find_all = 'getAgencyList'
19
+
20
+ def create_advertiser!(params = {})
21
+ Advertiser.create!(params.merge({
22
+ :agency => self,
23
+ }))
24
+ end
25
+
26
+ def advertisers
27
+ Advertiser.find(:all, self.id)
28
+ end
29
+
30
+ def publishers
31
+ Publisher.find(:all, self.id)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,80 @@
1
+ require 'date'
2
+
3
+ module OpenX
4
+ module Services
5
+ class Banner < Base
6
+ LOCAL_SQL = 'sql'
7
+ LOCAL_WEB = 'web'
8
+ EXTERNAL = 'url'
9
+ HTML = 'html'
10
+ TEXT = 'txt'
11
+
12
+ class << self
13
+ def find(id, *args)
14
+ session = self.connection
15
+ server = XMLRPC::Client.new2("#{session.url}#{endpoint}")
16
+ if id == :all
17
+ responses = server.call(find_all(), session.id, *args)
18
+ response = responses.first
19
+ return [] unless response
20
+ responses = [response]
21
+
22
+ ### Annoying.. For some reason OpenX returns a linked list.
23
+ ### Probably a bug....
24
+ while response.key?('aImage')
25
+ response = response.delete('aImage')
26
+ break unless response
27
+ responses << response
28
+ end
29
+
30
+ responses.map { |response|
31
+ new(translate(response))
32
+ }
33
+ else
34
+ response = server.call(find_one(), session.id, id)
35
+ new(translate(response))
36
+ end
37
+ end
38
+ end
39
+
40
+ # Translate our property names to OpenX property names
41
+ openx_accessor :name => :bannerName,
42
+ :campaign_id => :campaignId,
43
+ :id => :bannerId,
44
+ :storage_type => :storageType,
45
+ :file_name => :fileName,
46
+ :image_url => :imageURL,
47
+ :html_template => :htmlTemplate,
48
+ :width => :width,
49
+ :height => :height,
50
+ :weight => :weight,
51
+ :target => :target,
52
+ :url => :url,
53
+ :status => :status,
54
+ :adserver => :adserver,
55
+ :transparent => :transparent,
56
+ :image => :aImage,
57
+ :backup_image => :aBackupImage
58
+
59
+ has_one :campaign
60
+
61
+ self.endpoint = '/BannerXmlRpcService.php'
62
+ self.create = 'addBanner'
63
+ self.update = 'modifyBanner'
64
+ self.delete = 'deleteBanner'
65
+ self.find_one = 'getBanner'
66
+ self.find_all = 'getBannerListByCampaignId'
67
+
68
+ def initialize(params = {})
69
+ raise ArgumentError unless params[:campaign_id] || params[:campaign]
70
+ params[:campaign_id] ||= params[:campaign].id
71
+ super(params)
72
+ end
73
+
74
+ def statistics start_on = Date.today, end_on = Date.today
75
+ session = self.class.connection
76
+ @server.call('bannerDailyStatistics', session.id, self.id, start_on, end_on)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,125 @@
1
+ require 'yaml'
2
+
3
+ module OpenX
4
+ module Services
5
+ class Base
6
+ include Comparable
7
+
8
+ CONFIGURATION_YAML = File.join(ENV['HOME'], '.openx', 'credentials.yml')
9
+
10
+ @@connection = nil
11
+ @@configuration = nil
12
+
13
+ class << self
14
+ attr_accessor :endpoint, :translations
15
+ attr_accessor :create, :update, :delete, :find_one, :find_all
16
+
17
+ def configuration
18
+ @@configuration ||=
19
+ YAML.load_file(CONFIGURATION_YAML)[ENV['OPENX_ENV'] || 'production']
20
+ end
21
+
22
+ def configuration=(c); @@configuration = c; end
23
+
24
+ def connection= c
25
+ @@connection = c
26
+ end
27
+
28
+ def connection
29
+ unless @@connection
30
+ @@connection = Session.new(configuration['url'])
31
+ @@connection.create( configuration['username'],
32
+ configuration['password']
33
+ )
34
+ end
35
+ @@connection
36
+ end
37
+
38
+ def create!(params = {})
39
+ new(params).save!
40
+ end
41
+
42
+ def openx_accessor(accessor_map)
43
+ @translations ||= {}
44
+ @translations = accessor_map.merge(@translations)
45
+ accessor_map.each do |ruby,openx|
46
+ attr_accessor :"#{ruby}"
47
+ end
48
+ end
49
+
50
+ def has_one(*things)
51
+ things.each do |thing|
52
+ attr_writer :"#{thing}"
53
+ define_method(:"#{thing}") do
54
+ klass = thing.to_s.capitalize.gsub(/_[a-z]/) { |m| m[1].chr.upcase }
55
+ klass = OpenX::Services.const_get(:"#{klass}")
56
+ klass.find(send("#{thing}_id"))
57
+ end
58
+ end
59
+ end
60
+
61
+ def find(id, *args)
62
+ session = self.connection
63
+ server = XMLRPC::Client.new2("#{session.url}#{endpoint}")
64
+ if id == :all
65
+ responses = server.call(find_all(), session.id, *args)
66
+ responses.map { |response|
67
+ new(translate(response))
68
+ }
69
+ else
70
+ response = server.call(find_one(), session.id, id)
71
+ new(translate(response))
72
+ end
73
+ end
74
+
75
+ def destroy(id)
76
+ new(:id => id).destroy
77
+ end
78
+
79
+ private
80
+ def translate(response)
81
+ params = {}
82
+ self.translations.each { |k,v|
83
+ params[k] = response[v.to_s] if response[v.to_s]
84
+ }
85
+ params
86
+ end
87
+ end
88
+
89
+ def initialize(params = {})
90
+ @id = nil
91
+ params.each { |k,v| send(:"#{k}=", v) }
92
+ @server = XMLRPC::Client.new2("#{self.class.connection.url}#{self.class.endpoint}")
93
+ #@server.instance_variable_get(:@http).set_debug_output($stderr)
94
+ end
95
+
96
+ def new_record?; @id.nil?; end
97
+
98
+ def save!
99
+ params = {}
100
+ session = self.class.connection
101
+ self.class.translations.keys.each { |k|
102
+ value = send(:"#{k}")
103
+ params[self.class.translations[k].to_s] = value if value
104
+ }
105
+
106
+ if new_record?
107
+ @id = @server.call(self.class.create, session.id, params)
108
+ else
109
+ @server.call(self.class.update, session.id, params)
110
+ end
111
+ self
112
+ end
113
+
114
+ def destroy
115
+ session = self.class.connection
116
+ @server.call(self.class.delete, session.id, id)
117
+ @id = nil
118
+ end
119
+
120
+ def <=>(other)
121
+ self.id <=> other.id
122
+ end
123
+ end
124
+ end
125
+ end