krakatoa-openx 1.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'activesupport', :require => 'active_support/test_case'
5
+ gem 'rake'
6
+ gem 'jeweler'
7
+ gem 'mocha'
8
+ end
@@ -0,0 +1,25 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.0)
5
+ gemcutter (0.6.1)
6
+ git (1.2.5)
7
+ jeweler (1.4.0)
8
+ gemcutter (>= 0.1.0)
9
+ git (>= 1.2.5)
10
+ rubyforge (>= 2.0.0)
11
+ json_pure (1.4.6)
12
+ mocha (0.9.8)
13
+ rake
14
+ rake (0.8.7)
15
+ rubyforge (2.0.4)
16
+ json_pure (>= 1.1.7)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ activesupport
23
+ jeweler
24
+ mocha
25
+ rake
@@ -0,0 +1,85 @@
1
+ = OpenX
2
+
3
+ A Ruby interface to the OpenX XML-RPC API.
4
+
5
+ == SYNOPSIS:
6
+
7
+ OpenX.configuration = {
8
+ 'username' => 'admin',
9
+ 'password' => 'password',
10
+ 'url' => 'http://localhost/www/api/v2/xmlrpc/',
11
+ 'timeout' => 10, # Optional
12
+ 'reconnect' => true # Optional
13
+ }
14
+
15
+ OpenX::Services::Agency.find(:all).each do |agency|
16
+ puts agency.name
17
+
18
+ # Look up publishers
19
+ agency.publishers.each do |publisher|
20
+ puts "-- #{publisher.name}"
21
+ end
22
+
23
+ # Create a publisher
24
+ OpenX::Services::Publisher.create!(
25
+ :agency => agency,
26
+ :name => 'My Test Publisher',
27
+ :contact_name => 'Aaron Patterson',
28
+ :email => 'aaron@tenderlovemaking.com',
29
+ :username => 'user',
30
+ :password => 'password'
31
+ )
32
+ end
33
+
34
+ == REQUIREMENTS:
35
+
36
+ * ruby
37
+
38
+ == INSTALL:
39
+
40
+ * sudo gem install bsm-openx
41
+ * Update your $HOME/.openx/credentials.yml file.
42
+ You can set your own path via OpenX.config_file = '/path/pto/file.yml'.
43
+ Here is a sample:
44
+
45
+ ---
46
+ production:
47
+ username: admin
48
+ password: admin
49
+ url: http://www.example.com/www/api/v2/xmlrpc/
50
+ invocation_url: http://www.example.com/www/delivery/axmlrpc.php
51
+
52
+ The YAML file lists configuration for each environment. The gem uses the
53
+ 'production' environment by default. Trailing slash is required on the 'url'.
54
+ 'invocation_url' is only used by the OpenX::Invocation methods to serve
55
+ advertisements over XML-RPC
56
+
57
+ == LICENSE:
58
+
59
+ (The MIT License)
60
+
61
+ Copyright (c) 2010:
62
+
63
+ * {Aaron Patterson}[http://tenderlovemaking.com]
64
+ * Andy Smith
65
+ * {TouchLocal P/L}[http://www.touchlocal.com]
66
+ * Dimitrij Denissenko
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining
69
+ a copy of this software and associated documentation files (the
70
+ 'Software'), to deal in the Software without restriction, including
71
+ without limitation the rights to use, copy, modify, merge, publish,
72
+ distribute, sublicense, and/or sell copies of the Software, and to
73
+ permit persons to whom the Software is furnished to do so, subject to
74
+ the following conditions:
75
+
76
+ The above copyright notice and this permission notice shall be
77
+ included in all copies or substantial portions of the Software.
78
+
79
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
80
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
81
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
82
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
83
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
84
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
85
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ $: << File.join(File.dirname(__FILE__), "lib")
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'openx'
7
+
8
+ Rake::TestTask.new(:test) do |test|
9
+ test.test_files = FileList["test/**/test_*.rb"].sort
10
+ test.libs << "test"
11
+ test.verbose = false
12
+ test.warning = true
13
+ end
14
+
15
+ task :default => :test
16
+
17
+ namespace :openx do
18
+ task :clean do
19
+ ENV['OPENX_ENV'] = 'test'
20
+ include OpenX::Services
21
+
22
+ Agency.find(:all) do |agency|
23
+ Advertiser.find(:all, agency.id).each do |advertiser|
24
+ Campaign.find(:all, advertiser.id).each do |campaign|
25
+ Banner.find(:all, campaign.id).each do |banner|
26
+ banner.destroy
27
+ end
28
+ campaign.destroy
29
+ end
30
+ advertiser.destroy
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+
37
+
38
+ begin
39
+ require 'jeweler'
40
+ Jeweler::Tasks.new do |gemspec|
41
+ gemspec.name = "krakatoa-openx"
42
+ gemspec.summary = "A Ruby interface to the OpenX XML-RPC API"
43
+ gemspec.description = "A Ruby interface to the OpenX XML-RPC API"
44
+ gemspec.email = "krakatoa1987@gmail.com"
45
+ gemspec.homepage = "http://github.com/krakatoa/openx"
46
+ gemspec.authors = ["Aaron Patterson", "Andy Smith", "TouchLocal P/L", "Dimitrij Denissenko"]
47
+ end
48
+ Jeweler::GemcutterTasks.new
49
+ rescue LoadError
50
+ puts "Jeweler not available. Install it with: gem install jeweler"
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.9.4
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "krakatoa-openx"
8
+ s.version = "1.9.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Aaron Patterson", "Andy Smith", "TouchLocal P/L", "Dimitrij Denissenko"]
12
+ s.date = "2011-10-17"
13
+ s.description = "A Ruby interface to the OpenX XML-RPC API"
14
+ s.email = "krakatoa1987@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "README.txt"
17
+ ]
18
+ s.files = [
19
+ "Gemfile",
20
+ "Gemfile.lock",
21
+ "README.txt",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "krakatoa-openx.gemspec",
25
+ "lib/openx.rb",
26
+ "lib/openx/image.rb",
27
+ "lib/openx/invocation.rb",
28
+ "lib/openx/persistance.rb",
29
+ "lib/openx/services.rb",
30
+ "lib/openx/services/advertiser.rb",
31
+ "lib/openx/services/agency.rb",
32
+ "lib/openx/services/banner.rb",
33
+ "lib/openx/services/base.rb",
34
+ "lib/openx/services/campaign.rb",
35
+ "lib/openx/services/channel.rb",
36
+ "lib/openx/services/publisher.rb",
37
+ "lib/openx/services/session.rb",
38
+ "lib/openx/services/zone.rb",
39
+ "lib/openx/targeting_rule.rb",
40
+ "lib/openx/targeting_rules.rb",
41
+ "lib/openx/xmlrpc_client.rb",
42
+ "test/assets/300x250.jpg",
43
+ "test/assets/cat.swf",
44
+ "test/helper.rb",
45
+ "test/test_advertiser.rb",
46
+ "test/test_agency.rb",
47
+ "test/test_banner.rb",
48
+ "test/test_base.rb",
49
+ "test/test_campaign.rb",
50
+ "test/test_channel.rb",
51
+ "test/test_openx.rb",
52
+ "test/test_publisher.rb",
53
+ "test/test_services.rb",
54
+ "test/test_session.rb",
55
+ "test/test_targeting_rule.rb",
56
+ "test/test_targeting_rules.rb",
57
+ "test/test_xmlrpc_client.rb",
58
+ "test/test_xmlrpc_session_client.rb",
59
+ "test/test_zone.rb"
60
+ ]
61
+ s.homepage = "http://github.com/krakatoa/openx"
62
+ s.require_paths = ["lib"]
63
+ s.rubygems_version = "1.8.10"
64
+ s.summary = "A Ruby interface to the OpenX XML-RPC API"
65
+
66
+ if s.respond_to? :specification_version then
67
+ s.specification_version = 3
68
+
69
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
70
+ else
71
+ end
72
+ else
73
+ end
74
+ end
75
+
@@ -0,0 +1,44 @@
1
+ module OpenX
2
+ autoload :Persistance, 'openx/persistance'
3
+ autoload :Image, 'openx/image'
4
+ autoload :Invocation, 'openx/invocation'
5
+ autoload :TargetingRule, 'openx/targeting_rule'
6
+ autoload :TargetingRules, 'openx/targeting_rules'
7
+ autoload :Services, 'openx/services'
8
+ autoload :XmlrpcClient, 'openx/xmlrpc_client'
9
+ autoload :XmlrpcSessionClient, 'openx/xmlrpc_client'
10
+
11
+ @@env = ENV['OPENX_ENV'] || 'production'
12
+ @@config_file = File.join(ENV['HOME'], '.openx', 'credentials.yml')
13
+ @@configuration = nil
14
+
15
+ DEFAULT_OPTIONS = { 'retry' => true, 'timeout' => 10 }
16
+
17
+ class << self
18
+
19
+ def env
20
+ @@env
21
+ end
22
+
23
+ def env=(value)
24
+ @@env = value
25
+ end
26
+
27
+ def configuration
28
+ @@configuration ||= DEFAULT_OPTIONS.merge(YAML.load_file(config_file)[env])
29
+ end
30
+
31
+ def configuration=(value)
32
+ @@configuration = value
33
+ end
34
+
35
+ def config_file
36
+ @@config_file
37
+ end
38
+
39
+ def config_file=(value)
40
+ @@config_file = value
41
+ end
42
+
43
+ end
44
+ 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,62 @@
1
+ module OpenX
2
+ class Invocation
3
+ class << self
4
+ DEFAULTS = {
5
+ :count => 1,
6
+ :campaignid => 0,
7
+ :target => '',
8
+ :source => '',
9
+ :with_text => false,
10
+ :exclusions => [],
11
+ :inclusions => [],
12
+ :exclude_by_campaignid => false,
13
+ :exclude_by_bannerid => false
14
+ }.freeze
15
+
16
+ #
17
+ # banner = OpenX::Invocation.view("Plumber")
18
+ #
19
+ # banners = OpenX::Invocation.view("Plumber", :count => 2, :exclude_by_campaignid => true) ;nil
20
+ # banners.each do |banner|
21
+ # puts "Banner #{banner['bannerid']}"
22
+ # end; nil
23
+ #
24
+ def view(what, params = {})
25
+ params = DEFAULTS.merge(params)
26
+ url = OpenX::Services.configuration['invocation_url']
27
+ settings = {:cookies => [], :remote_addr => 'localhost'}
28
+
29
+ context = [] # used by reference after initial use
30
+ params[:exclusions].each { |item| context << convert_to_context(false, item) }
31
+ params[:inclusions].each { |item| context << convert_to_context(true, item) }
32
+ count = params[:count].to_i
33
+
34
+ remote_params = [ what, params[:campaignid], params[:target], params[:source], params[:with_text], context]
35
+ server = XmlrpcClient.new(url)
36
+
37
+ out = []
38
+ if count > 0
39
+ (0...count).each do
40
+ out << banner = server.call('openads.view', settings, *remote_params)
41
+ if count > 1
42
+ if params[:exclude_by_campaignid]
43
+ campaign_id = banner['campaignid']
44
+ context << convert_to_context(false, "campaignid:#{campaign_id}")
45
+ elsif params[:exclude_by_bannerid]
46
+ banner_id = banner['bannerid']
47
+ context << convert_to_context(false, "bannerid:#{banner_id}")
48
+ end
49
+ end
50
+ end
51
+ end
52
+ count > 1 ? out : out.first
53
+ end
54
+
55
+ def convert_to_context(is_inclusion, item)
56
+ key = is_inclusion ? '==' : '!='
57
+ { key => item }
58
+ end
59
+ protected :convert_to_context
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,63 @@
1
+ module OpenX
2
+ module Persistance
3
+
4
+ module ClassMethods
5
+
6
+ def create!(params = {})
7
+ new(params).save!
8
+ end
9
+
10
+ def find(id, *args)
11
+ if id == :all
12
+ responses = remote.call(find_all, *args)
13
+ responses.map do |response|
14
+ new(translate(response))
15
+ end
16
+ else
17
+ response = remote.call(find_one, id)
18
+ new(translate(response))
19
+ end
20
+ end
21
+
22
+ def destroy(id)
23
+ new(:id => id).destroy
24
+ end
25
+
26
+ private
27
+
28
+ def translate(response)
29
+ params = {}
30
+ self.translations.each do |k,v|
31
+ params[k] = response[v.to_s] if response[v.to_s]
32
+ end
33
+ params
34
+ end
35
+
36
+ end
37
+
38
+ module InstanceMethods
39
+
40
+ def save!
41
+ params = {}
42
+ self.class.translations.keys.each do |k|
43
+ value = send(:"#{k}")
44
+ params[self.class.translations[k].to_s] = value if value
45
+ end
46
+
47
+ if new_record?
48
+ @id = remote.call(self.class.create, params)
49
+ else
50
+ remote.call(self.class.update, params)
51
+ end
52
+ self
53
+ end
54
+
55
+ def destroy
56
+ remote.call(self.class.delete, id)
57
+ @id = nil
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end