zanox 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/zanox.rb +139 -31
  2. data/spec/zanox_specs.rb +26 -9
  3. data/zanox.gemspec +3 -3
  4. metadata +8 -8
data/lib/zanox.rb CHANGED
@@ -9,22 +9,36 @@ module Zanox
9
9
 
10
10
  class AuthError < ArgumentError; end
11
11
 
12
- attr_accessor :connect_id
12
+ attr_accessor :public_key
13
13
  attr_accessor :secret_key
14
14
  attr_accessor :wsdl
15
+ attr_accessor :driver
15
16
 
16
17
  def self.request(method, options)
17
- puts method + " " + options.inspect if $DEBUG
18
+ begin
19
+ puts method + " " + options.inspect if $DEBUG
18
20
 
19
- options.merge!(:connectId=>Zanox::API.connect_id)
21
+ options.merge!(:connectId=>Zanox::API::Session.connect_id)
20
22
 
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]
23
+ #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
24
 
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)
25
+ unless Zanox::API::Session.secret_key.nil?
26
+ timestamp = Zanox::API.get_timestamp
27
+ nonce = Zanox::API.generate_nonce
28
+ signature = Zanox::API.create_signature(Zanox::API::Session.secret_key, "publisherservice"+method.downcase + timestamp + nonce)
29
+ options.merge!(:timestamp=>timestamp, :nonce=>nonce, :signature=>signature)
30
+ end
31
+
32
+ @wsdl = 'http://api.zanox.com/wsdl/2009-07-01/' unless !!@wsdl
33
+ @driver = SOAP::WSDLDriverFactory.new(@wsdl).create_rpc_driver unless !!@driver
34
+ @driver.wiredump_dev = STDOUT if $DEBUG
35
+ @driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE if $DEBUG
36
+ @driver.method(method.to_sym).call(options)
37
+ rescue Exception => e
38
+ puts
39
+ puts "ERROR"
40
+ puts e.message
41
+ end
28
42
  end
29
43
 
30
44
  def self.generate_nonce
@@ -41,6 +55,12 @@ module Zanox
41
55
  signature = Base64.encode64(HMAC::SHA1.new(secret_key).update(string2sign).digest)[0..-2]
42
56
  end
43
57
 
58
+ def self.authorize(public_key, secret_key)
59
+ @public_key = public_key
60
+ @secret_key = secret_key
61
+ true
62
+ end
63
+
44
64
  def self.authenticate(connect_id, secret_key=nil)
45
65
  #todo: real session request with connect flow
46
66
  @connect_id = connect_id
@@ -48,6 +68,76 @@ module Zanox
48
68
  true
49
69
  end
50
70
 
71
+ module Session
72
+ attr_accessor :connect_id
73
+ attr_accessor :secret_key
74
+ attr_accessor :offline_token
75
+
76
+ def self.new(auth_token)
77
+ response = Zanox::Connect.request("getSession", {:authToken=>auth_token})
78
+ self.map(response)
79
+ end
80
+
81
+ def self.offline(offline_token)
82
+ response = Zanox::Connect.request("getOfflineSession", {:offlineToken=>offline_token})
83
+ self.map(response)
84
+ if(response.respond_to?(:session))
85
+ response.session
86
+ else
87
+ {:error=>"error!!! offline session"}
88
+ end
89
+ end
90
+
91
+ def map(response)
92
+ if(response.respond_to?(:session))
93
+ @connect_id = response.session.connectId
94
+ @secret_key = response.session.secretKey
95
+ @offline_token = response.session.offlineToken
96
+ true
97
+ else
98
+ false
99
+ end
100
+ end
101
+
102
+ self.instance_methods.each do |method|
103
+ module_function method.to_sym
104
+ end
105
+
106
+ end
107
+
108
+ self.instance_methods.each do |method|
109
+ module_function method.to_sym
110
+ end
111
+
112
+ end
113
+
114
+ module Connect
115
+ attr_accessor :wsdl
116
+ attr_accessor :driver
117
+
118
+ def self.request(method, options)
119
+ begin
120
+ options.merge!(:publicKey=>Zanox::API.public_key)
121
+
122
+ unless Zanox::API.secret_key.nil?
123
+ timestamp = Zanox::API.get_timestamp
124
+ nonce = Zanox::API.generate_nonce
125
+ signature = Zanox::API.create_signature(Zanox::API.secret_key, "connectservice"+method.downcase + timestamp + nonce)
126
+ options.merge!(:timestamp=>timestamp, :nonce=>nonce, :signature=>signature)
127
+ end
128
+
129
+ @wsdl = 'https://auth.zanox-affiliate.de/wsdl/2010-02-01' unless !!@wsdl
130
+ @driver = SOAP::WSDLDriverFactory.new(@wsdl).create_rpc_driver unless !!@driver
131
+ @driver.wiredump_dev = STDOUT if $DEBUG
132
+ @driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE if $DEBUG
133
+ @driver.method(method.to_sym).call(options)
134
+ rescue Exception => e
135
+ puts
136
+ puts "ERROR"
137
+ puts e.message
138
+ end
139
+ end
140
+
51
141
  self.instance_methods.each do |method|
52
142
  module_function method.to_sym
53
143
  end
@@ -65,18 +155,13 @@ module Zanox
65
155
  def find_every(options)
66
156
  items = []
67
157
  class_name = self.name.split('::').last
68
- api_method = 'get'+self.pluralize
69
158
 
70
- if(class_name=='Program' && options.has_key?(:adspaceId))
71
- api_method = "getProgramApplications"
159
+ if(self.respond_to?(:pluralize))
160
+ api_method = 'get'+self.pluralize
72
161
  end
73
162
 
74
- unless Zanox::API.secret_key.nil?
75
- timestamp = Zanox::API.get_timestamp
76
- nonce = Zanox::API.generate_nonce
77
-
78
- signature = Zanox::API.create_signature(Zanox::API.secret_key, "publisherservice"+api_method.downcase + timestamp + nonce)
79
- options.merge!(:timestamp=>timestamp, :nonce=>nonce, :signature=>signature)
163
+ if(class_name=='Program' && options.has_key?(:adspaceId))
164
+ api_method = "getProgramsByAdspace"
80
165
  end
81
166
 
82
167
  response = Zanox::API.request(api_method, options)
@@ -122,21 +207,35 @@ module Zanox
122
207
  class_name = self.name.split('::').last
123
208
  api_method = ''
124
209
 
125
- if(ids.size>0)
210
+ if(ids.size==0 && queries.size==0)
126
211
  api_method = 'get'+class_name
127
212
  unless Zanox::API.secret_key.nil?
128
- timestamp = Zanox::API.get_timestamp
129
- nonce = Zanox::API.generate_nonce
130
- signature = Zanox::API.create_signature(Zanox::API.secret_key, "publisherservice"+api_method.downcase + timestamp + nonce)
131
- options.merge!(:timestamp=>timestamp, :nonce=>nonce, :signature=>signature)
213
+ response = Zanox::API.request(api_method, options)
214
+ class_name.sub!(/\b\w/) { $&.downcase }
215
+ item_method = (class_name+'Item').to_sym
216
+ if(response.respond_to?(item_method))
217
+ item = self.new(response.method(item_method).call)
218
+ items.push item
219
+ end
132
220
  end
133
221
 
222
+ end
223
+
224
+ if(ids.size>0)
225
+
226
+ api_method = 'get'+class_name
227
+
134
228
  ids.each do |id|
135
229
  options.merge!(self.key_symbol=>id)
136
230
  response = Zanox::API.request(api_method, options)
231
+
137
232
  class_name.sub!(/\b\w/) { $&.downcase }
138
- item = self.new(response.method((class_name+'Item').to_sym).call)
139
- items.push item
233
+ item_method = (class_name+'Item').to_sym
234
+
235
+ if(response.respond_to?(item_method))
236
+ item = self.new(response.method(item_method).call)
237
+ items.push item
238
+ end
140
239
  end
141
240
  end
142
241
 
@@ -170,21 +269,30 @@ module Zanox
170
269
  end
171
270
 
172
271
  def find(*args)
173
-
272
+ item_name = self.name.split('::').last
174
273
  options = args.last.is_a?(Hash) ? args.pop : {}
175
274
 
176
275
  puts "Arguments: " + args.inspect if $DEBUG
177
276
  puts "Options: " + options.inspect if $DEBUG
178
277
 
179
- case args.first
180
- when :all then find_every(options)
181
- when nil then find_every(options)
182
- else find_other(args, options)
278
+ if(item_name=="Profile")
279
+ find_other(args, options)
280
+ else
281
+ case args.first
282
+ when :all then find_every(options)
283
+ when nil then find_every(options)
284
+ else find_other(args, options)
285
+ end
183
286
  end
184
287
 
185
288
  end
186
289
  end
187
290
 
291
+ module Profile
292
+ include Item
293
+ extend Item
294
+ end
295
+
188
296
  module Program
189
297
  include Item
190
298
  extend Item
@@ -198,7 +306,7 @@ module Zanox
198
306
  end
199
307
 
200
308
  def self.is_key?(id)
201
- id.to_s[/^[0-9]{2,8}$/] ? true : false
309
+ id.to_s[/^[0-9]{1,8}$/] ? true : false
202
310
  end
203
311
  end
204
312
 
data/spec/zanox_specs.rb CHANGED
@@ -2,15 +2,33 @@ require File.join(File.dirname(__FILE__),"..","lib","zanox.rb")
2
2
 
3
3
  describe Zanox::API do
4
4
 
5
- # authenticate
6
- TEST_CONNECT_ID = "your connect id here"
5
+ TEST_PUBLIC_KEY = "your public key here"
7
6
  TEST_SECRET_KEY = "your secret key here"
8
7
 
9
8
  before(:all) do
10
9
  end
11
10
 
12
- it "should authenticate a developer" do
13
- Zanox::API.authenticate(TEST_CONNECT_ID, TEST_SECRET_KEY).should == true
11
+ it "should authorize a developer's application" do
12
+ Zanox::API.authorize(TEST_PUBLIC_KEY, TEST_SECRET_KEY).should == true
13
+ end
14
+ end
15
+
16
+ describe Zanox::API::Session do
17
+ it "should get a new session" do
18
+ TEST_AUTH_TOKEN = "your auth token here"
19
+ Zanox::API::Session.new(TEST_AUTH_TOKEN).should == true
20
+ end
21
+ it "should get a new offline session" do
22
+ TEST_OFFLINE_TOKEN = "your offline token here"
23
+ TEST_CONNECT_ID = "publisher connect id here"
24
+ Zanox::API::Session.offline(TEST_OFFLINE_TOKEN)
25
+ Zanox::API::Session.connect_id.should == TEST_CONNECT_ID
26
+ end
27
+ end
28
+
29
+ describe Zanox::Profile do
30
+ it "should find the users profile" do
31
+ Zanox::Profile.find.size.should == 1
14
32
  end
15
33
  end
16
34
 
@@ -42,15 +60,14 @@ describe Zanox::Program do
42
60
  Zanox::Program.find(TEST_PROGRAM_ID).size.should == 1
43
61
  end
44
62
 
45
- # TODO: fix it
46
- # it "should find programs by an adspace" do
47
- # Zanox::Program.find(:adspaceId=>TEST_ADSPACE_ID).size.should >= 1
48
- # end
63
+ it "should find programs by an adspace" do
64
+ Zanox::Program.find(:adspaceId=>TEST_ADSPACE_ID).size.should >= 1
65
+ end
49
66
  end
50
67
 
51
68
  describe Zanox::Adspace do
52
69
  # Adspace.find
53
- TEST_ADSPACE_ID = "1289612"
70
+ TEST_ADSPACE_ID = "99501"
54
71
 
55
72
  it "should find all user's Adspaces" do
56
73
  Zanox::Adspace.find(:all).size.should >=1
data/zanox.gemspec CHANGED
@@ -8,13 +8,13 @@ require 'rake'
8
8
  spec = Gem::Specification.new do |s|
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.name = %q{zanox}
11
- s.version = "0.2.4"
11
+ s.version = "0.2.5"
12
12
  s.authors = ["Krispin Schulz"]
13
13
  s.homepage = %q{http://github.com/kr1sp1n/zanox}
14
14
  s.date = Time.now.strftime("%Y-%m-%d")
15
15
  s.email = %q{krispinone@googlemail.com}
16
- s.summary = %q{One gem to rule the zanox web services.}
17
- s.description = %q{The easy way to the zanox web services.}
16
+ s.summary = %q{One gem to rule the zanox API.}
17
+ s.description = %q{The easy way to the zanox API.}
18
18
  s.files = FileList['Rakefile', 'zanox.gemspec', 'README.textile', 'lib/**/*', 'test/*', 'spec/*'].to_a
19
19
  s.require_paths = ["lib"]
20
20
  s.rubygems_version = %q{1.3.5}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zanox
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
4
+ hash: 29
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 4
10
- version: 0.2.4
9
+ - 5
10
+ version: 0.2.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Krispin Schulz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-10 00:00:00 +01:00
18
+ date: 2011-02-15 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -50,7 +50,7 @@ dependencies:
50
50
  version: 0.4.0
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
- description: The easy way to the zanox web services.
53
+ description: The easy way to the zanox API.
54
54
  email: krispinone@googlemail.com
55
55
  executables: []
56
56
 
@@ -96,9 +96,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  requirements: []
97
97
 
98
98
  rubyforge_project:
99
- rubygems_version: 1.3.7
99
+ rubygems_version: 1.4.1
100
100
  signing_key:
101
101
  specification_version: 3
102
- summary: One gem to rule the zanox web services.
102
+ summary: One gem to rule the zanox API.
103
103
  test_files:
104
104
  - test/zanox_tests.rb