zanox 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,6 @@
1
+ h2. the zanox gem
2
+
3
+ The ruby way to use the zanox API.
4
+
5
+
6
+
data/Rakefile ADDED
@@ -0,0 +1,72 @@
1
+ require 'rbconfig'
2
+ include Config
3
+
4
+ task :default => [:test]
5
+
6
+ case CONFIG['host_os']
7
+ when /mswin|windows/i
8
+ # Windows
9
+ $windows = true
10
+ when /linux/i
11
+ # Linux
12
+ $linux = true
13
+ when /sunos|solaris/i
14
+ # Solaris
15
+ $solaris = true
16
+ else
17
+ # Whatever
18
+ $mac = true
19
+ end
20
+
21
+ $gem_name = "zanox"
22
+
23
+ desc "Test the library"
24
+ task :test do
25
+ ruby Dir.glob("test/*")
26
+ end
27
+
28
+ desc "Run specs"
29
+ task :spec do
30
+ sh "spec spec/* --format specdoc --color"
31
+ end
32
+
33
+ desc "Build the gem"
34
+ task :build do
35
+ sh "gem build #$gem_name.gemspec"
36
+ if windows?
37
+ sh "move /Y *.gem ./pkg"
38
+ else
39
+ sh "mv #$gem_name*.gem ./pkg"
40
+ end
41
+ end
42
+
43
+ desc "Install the library at local machine"
44
+ task :install => :build do
45
+ if windows?
46
+ sh "gem install ./pkg/#$gem_name -l"
47
+ else
48
+ sh "sudo gem install ./pkg/#$gem_name -l"
49
+ end
50
+ end
51
+
52
+ desc "Uninstall the library from local machine"
53
+ task :uninstall do
54
+ if windows?
55
+ sh "gem uninstall #$gem_name"
56
+ else
57
+ sh "sudo gem uninstall #$gem_name"
58
+ end
59
+ end
60
+
61
+ desc "Reinstall the library in the local machine"
62
+ task :reinstall => [:uninstall, :install] do
63
+ end
64
+
65
+ desc "Clean"
66
+ task :clean do
67
+ # sh "rm #$gem_name*.gem"
68
+ end
69
+
70
+ def windows?
71
+ !!$windows
72
+ end
data/lib/zanox.rb CHANGED
@@ -1,34 +1,272 @@
1
- $:.unshift(File.dirname(__FILE__)) unless
2
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
-
4
1
  require 'rubygems'
5
- require 'active_support'
6
-
7
- require 'zanox/zanox_base'
8
- require 'zanox/zanox_xml'
9
- require 'zanox/zanox_json'
10
- require 'zanox/zanox_soap'
11
2
 
12
- # Module to encapsule the Zanox API.
13
3
  module Zanox
4
+ module API
5
+ require 'soap/wsdlDriver'
6
+ require 'base64'
7
+ require 'hmac-sha1'
8
+ require 'digest/md5'
9
+
10
+ class AuthError < ArgumentError; end
11
+
12
+ attr_accessor :connect_id
13
+ attr_accessor :secret_key
14
+ attr_accessor :wsdl
15
+
16
+ def self.request(method, options)
17
+ puts method + " " + options.inspect if $DEBUG
18
+
19
+ options.merge!(:connectId=>Zanox::API.connect_id)
20
+
21
+ raise AuthError, "Missing connect id. Try calling Zanox::API.authenticate('your connect id', 'your secret key') before your requests", caller[caller.length - 1] if !!options[:connect_id]
22
+
23
+ wsdl = 'http://api.zanox.com/wsdl/2009-07-01/' unless !!wsdl
24
+ $driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver unless !!$driver
25
+ $driver.wiredump_dev = STDOUT if $DEBUG
26
+ $driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE if $DEBUG
27
+ $driver.method(method.to_sym).call(options)
28
+ end
29
+
30
+ def self.generate_nonce
31
+ mt = Time.new.usec
32
+ rand = rand()
33
+ Digest::MD5.hexdigest((mt + rand).to_s)
34
+ end
35
+
36
+ def self.get_timestamp
37
+ Time.new.gmtime.strftime("%Y-%m-%dT%H:%M:%S.000Z")
38
+ end
39
+
40
+ def self.create_signature(secret_key, string2sign)
41
+ signature = Base64.encode64(HMAC::SHA1.new(secret_key).update(string2sign).digest)[0..-2]
42
+ end
43
+
44
+ def self.authenticate(connect_id, secret_key=nil)
45
+ #todo: real session request with connect flow
46
+ @connect_id = connect_id
47
+ @secret_key = secret_key
48
+ true
49
+ end
50
+
51
+ self.instance_methods.each do |method|
52
+ module_function method.to_sym
53
+ end
54
+ end
55
+
56
+ module Item
57
+ attr_accessor :id
14
58
 
15
- class Factory
16
-
17
- def Factory.get_interface(interface, version = false)
18
- case interface
19
- when 'xml'
20
- obj = ZanoxXml.new(version)
21
- when 'json'
22
- obj = ZanoxJson.new(version)
23
- when 'soap'
24
- obj = ZanoxSoap.new
25
- else
26
- raise 'No such Interface!'
59
+ def new(item=nil)
60
+ item.extend(self)
61
+ item.id= item.xmlattr_id
62
+ return item
63
+ end
64
+
65
+ def find_every(options)
66
+ items = []
67
+ class_name = self.name.split('::').last
68
+ api_method = 'get'+self.pluralize
69
+ timestamp = Zanox::API.get_timestamp
70
+ nonce = Zanox::API.generate_nonce
71
+
72
+ signature = Zanox::API.create_signature(Zanox::API.secret_key, "publisherservice"+api_method.downcase + timestamp + nonce)
73
+ options.merge!(:timestamp=>timestamp, :nonce=>nonce, :signature=>signature)
74
+
75
+ response = Zanox::API.request(api_method, options)
76
+
77
+ m1_name = (class_name.downcase+'Items').to_sym
78
+ m2_name = (class_name.downcase+'Item').to_sym
79
+
80
+ if(response.respond_to?(m1_name))
81
+ items_object = response.method(m1_name).call
82
+ if(items_object.respond_to?(m2_name))
83
+ item_object = items_object.method(m2_name).call
84
+ if(item_object.is_a?(Array))
85
+ item_list = item_object
86
+ else
87
+ item_list = [item_object]
88
+ end
89
+ [*item_object].each do |item|
90
+ items.push self.new(item)
91
+ end
92
+ end
27
93
  end
94
+ end
95
+
96
+ def find_other(args, options)
97
+ ids = []
98
+ queries = []
99
+ items = []
100
+ args.each do |arg|
101
+ self.is_key?(arg) ? ids.push(arg) : queries.push(arg)
102
+ end
103
+
104
+ puts "found ids: "+ids.to_s if $DEBUG
105
+ puts "found query strings: "+queries.to_s if $DEBUG
106
+
107
+ class_name = self.name.split('::').last
108
+ api_method = ''
109
+ timestamp = Zanox::API.get_timestamp
110
+ nonce = Zanox::API.generate_nonce
111
+
112
+ if(ids.size>0)
113
+ api_method = 'get'+class_name.capitalize
114
+ signature = Zanox::API.create_signature(Zanox::API.secret_key, "publisherservice"+api_method.downcase + timestamp + nonce)
115
+ options.merge!(:timestamp=>timestamp, :nonce=>nonce, :signature=>signature)
28
116
 
29
- return obj
117
+ ids.each do |id|
118
+ options.merge!(self.key_symbol=>id)
119
+ response = Zanox::API.request(api_method, options)
120
+ item = self.new(response.method((class_name.downcase+'Item').to_sym).call)
121
+ items.push item
122
+ end
123
+ end
124
+
125
+ if(queries.size>0)
126
+ api_method = 'search'+self.pluralize
127
+
128
+ queries.each do |query|
129
+ options.merge!(:query=>query)
130
+ response = Zanox::API.request(api_method, options)
131
+ m1_name = (class_name.downcase+'Items').to_sym
132
+ m2_name = (class_name.downcase+'Item').to_sym
133
+
134
+ if(response.respond_to?(m1_name))
135
+ items_object = response.method(m1_name).call
136
+ if(items_object.respond_to?(m2_name))
137
+ item_object = items_object.method(m2_name).call
138
+ if(item_object.is_a?(Array))
139
+ item_list = item_object
140
+ else
141
+ item_list = [item_object]
142
+ end
143
+ [*item_object].each do |item|
144
+ items.push self.new(item)
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ items
30
152
  end
31
153
 
154
+ def find(*args)
155
+
156
+ options = args.last.is_a?(Hash) ? args.pop : {}
157
+
158
+ puts "Arguments: " + args.inspect if $DEBUG
159
+ puts "Options: " + options.inspect if $DEBUG
160
+
161
+ case args.first
162
+ when :all then find_every(options)
163
+ when nil then find_every(options)
164
+ else find_other(args, options)
165
+ end
166
+
167
+ end
168
+ end
169
+
170
+ module Program
171
+ include Item
172
+ extend Item
173
+
174
+ def self.key_symbol
175
+ :programId
176
+ end
177
+
178
+ def self.pluralize
179
+ "Programs"
180
+ end
181
+
182
+ def self.is_key?(id)
183
+ id.to_s[/[0-9]{3,6}/] ? true : false
184
+ end
185
+ end
186
+
187
+ module Admedium
188
+ include Item
189
+ extend Item
190
+
191
+ def self.key_symbol
192
+ :admediumId
193
+ end
194
+
195
+ def self.pluralize
196
+ "Admedia"
197
+ end
198
+
199
+ def self.is_key?(id)
200
+ id.to_s[/[0-9]{3,6}/] ? true : false
201
+ end
202
+ end
203
+
204
+ module Product
205
+ include Item
206
+ extend Item
207
+
208
+ def self.key_symbol
209
+ :zupId
210
+ end
211
+
212
+ def self.pluralize
213
+ "Products"
214
+ end
215
+
216
+ def self.is_key?(id)
217
+ id.to_s[/[0-9A-Fa-f]{32}/] ? true : false
218
+ end
219
+ end
220
+
221
+ module Adspace
222
+ include Item
223
+ extend Item
224
+
225
+ def self.key_symbol
226
+ :adspaceId
227
+ end
228
+
229
+ def self.pluralize
230
+ "Adspaces"
231
+ end
232
+
233
+ def self.is_key?(id)
234
+ id.to_s[/[0-9]/] ? true : false
235
+ end
236
+ end
237
+
238
+ module Sale
239
+ include Item
240
+ extend Item
241
+
242
+ def self.key_symbol
243
+ :saleId
244
+ end
245
+
246
+ def self.pluralize
247
+ "Sales"
248
+ end
249
+
250
+ def self.is_key?(id)
251
+ id.to_s[/[0-9]/] ? true : false
252
+ end
253
+ end
254
+
255
+ module Lead
256
+ include Item
257
+ extend Item
258
+
259
+ def self.key_symbol
260
+ :leadId
261
+ end
262
+
263
+ def self.pluralize
264
+ "Leads"
265
+ end
266
+
267
+ def self.is_key?(id)
268
+ id.to_s[/[0-9]/] ? true : false
269
+ end
32
270
  end
33
271
 
34
272
  end
@@ -0,0 +1,55 @@
1
+ require File.join(File.dirname(__FILE__),"..","lib","zanox.rb")
2
+
3
+ describe Zanox::API do
4
+
5
+ # authenticate
6
+ TEST_CONNECT_ID = "your connect id here"
7
+ TEST_SECRET_KEY = "your secret key here"
8
+
9
+ before(:all) do
10
+ end
11
+
12
+ it "should authenticate a developer" do
13
+ Zanox::API.authenticate(TEST_CONNECT_ID, TEST_SECRET_KEY).should == true
14
+ end
15
+ end
16
+
17
+ describe Zanox::Product do
18
+ # Product.find
19
+ TEST_PRODUCT_QUERY = "ipod"
20
+ TEST_PRODUCT_ID = "afdd090e0ee25e796e5146f6fcd7b15e"
21
+
22
+ it "should find products by a keyword" do
23
+ Zanox::Product.find(TEST_PRODUCT_QUERY).size.should >= 1
24
+ end
25
+
26
+ it "should find a specific product by its id" do
27
+ Zanox::Product.find(TEST_PRODUCT_ID).size.should == 1
28
+ end
29
+ end
30
+
31
+ describe Zanox::Program do
32
+ # Program.find
33
+ TEST_PROGRAM_QUERY = "Amazon"
34
+ TEST_PROGRAM_ID = "1648"
35
+
36
+ it "should find programs by a keyword" do
37
+ Zanox::Program.find(TEST_PROGRAM_QUERY).size.should >= 1
38
+ end
39
+
40
+ it "should find a specific program by its id" do
41
+ Zanox::Program.find(TEST_PROGRAM_ID).size.should == 1
42
+ end
43
+ end
44
+
45
+ describe Zanox::Adspace do
46
+ # Adspace.find
47
+ TEST_ADSPACE_ID = "1289612"
48
+
49
+ it "should find all user's Adspaces" do
50
+ Zanox::Adspace.find(:all).size.should >=1
51
+ end
52
+ it "should find the users Adspace by an id" do
53
+ Zanox::Adspace.find(TEST_ADSPACE_ID).size.should == 1
54
+ end
55
+ end
@@ -0,0 +1,12 @@
1
+ require File.join(File.dirname(__FILE__),"..","lib","zanox")
2
+ require 'test/unit'
3
+
4
+ class ZanoxTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ end
8
+
9
+ def test_api_version_not_empty
10
+ end
11
+
12
+ end
data/zanox.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # require 'rubygems'
2
+ # require 'rubygems/gem_runner'
3
+ # #Gem.manage_gems
4
+ # require 'rake/gempackagetask'
5
+ require 'rake'
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ s.platform = Gem::Platform::RUBY
9
+ s.name = %q{zanox}
10
+ s.version = "0.2.0"
11
+ s.authors = ["Krispin Schulz"]
12
+ s.homepage = %q{http://kr1sp1n.com/}
13
+ s.date = %q{2010-05-07}
14
+ s.email = %q{krispinone@googlemail.com}
15
+ s.summary = %q{One gem to rule the zanox web services.}
16
+ s.description = %q{The easy way to the zanox web services.}
17
+ s.files = FileList['Rakefile', 'zanox.gemspec', 'README.textile', 'lib/**/*', 'test/*', 'spec/*'].to_a
18
+ s.require_paths = ["lib"]
19
+ s.rubygems_version = %q{1.3.5}
20
+ s.test_files = Dir.glob("{test, spec}/**/*")
21
+ # include README while generating rdoc
22
+ s.rdoc_options = ["--main", "README.textile"]
23
+ s.has_rdoc = true
24
+ s.extra_rdoc_files = ["README.textile"]
25
+ s.add_dependency("soap4r",">=1.5.8")
26
+ s.add_dependency("ruby-hmac", ">=0.4.0")
27
+ end
28
+
29
+ # Rake::GemPackageTask.new(spec) do |pkg|
30
+ # pkg.need_tar = true
31
+ # end
32
+ #
33
+ # task :default => "pkg/#{spec.name}-#{spec.version}.gem" do
34
+ # puts "generated latest version"
35
+ # end