webmoney 0.0.4.3 → 0.0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,5 +1,11 @@
1
1
  ChangeLog
2
2
 
3
+ Sun Mar 15 2009 14:55:18 +0400
4
+ ----------------------------
5
+ V 0.0.4.4
6
+ * Fix bug in send_message (reqn related)
7
+ * Refactor for more extendable
8
+
3
9
  Thu Jan 22 2009 14:00:00 +0400
4
10
  ----------------------------
5
11
  V 0.0.4.3
data/README CHANGED
@@ -1,11 +1,72 @@
1
- README
1
+ == About Webmoney library
2
2
 
3
- For run tests create ~/.wm/config.yml file:
3
+ This library should help to make requests to WebMoney Transfer http://www.wmtransfer.com
4
+ XML-interfaces: http://www.wmtransfer.com/eng/developers/interfaces/index.shtml
4
5
 
5
- common:
6
- type: classic
7
- wmid: '123456789012'
8
- key: 'gQABAIR6... YOUR BASE64-KEY (see wmsigner readme) ...2cC8FZTyKyjBM='
9
- password: _your_password_
6
+ Gem have built-in native *wmsigner*.
10
7
 
11
- and type: rake spec
8
+ Author:: Alexander Oryol (mailto:eagle.alex@gmail.com)
9
+ License:: MIT License
10
+
11
+ == Request types
12
+
13
+ Completed:
14
+ - send_message - x6
15
+ - get_passport - x11
16
+ - bussines_level
17
+
18
+ Incompleted (help need!):
19
+ - create_invoice - x1
20
+ - create_transaction - x2
21
+ - operation_history - x3
22
+ - outgoing_invoices - x4
23
+ - finish_protect - x5
24
+ - check_sign - x7
25
+ - find_wm - x8
26
+ - balance - x9
27
+ - incoming_invoices - x10
28
+ - reject_protection - x13
29
+ - transaction_moneyback - x14
30
+ - i_trust - x15
31
+ - trust_me - x15
32
+ - trust_save - x15
33
+ - create_purse - x16
34
+
35
+ Please, see relative documentation and parameters on wiki:
36
+
37
+ http://wiki.wmtransfer.com/wiki/list/XML-Interfaces
38
+
39
+ http://wiki.webmoney.ru/wiki/list/XML-%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D1%84%D0%B5%D0%B9%D1%81%D1%8B (in russian)
40
+
41
+ or official sites:
42
+
43
+ http://www.wmtransfer.com/eng/developers/interfaces/xml/index.shtml
44
+
45
+ http://www.webmoney.ru/rus/developers/interfaces/xml/index.shtml (in russian)
46
+
47
+ == Examples
48
+
49
+ = Setup
50
+
51
+ @wm = Webmoney.new(:wmid => '123456789012', :password => 'my_pass', :key => 'gQABAIR6...2cC8FZTyKyjBM=')
52
+
53
+ wmid = '111222333444'
54
+
55
+ = Passport (X11) request:
56
+
57
+ passport = @wm.request(:get_passport, :wmid => wmid)
58
+
59
+ = Bussines level request:
60
+
61
+ bl = @wm.request(:bussines_level, :wmid => wmid)
62
+
63
+ = Sending message
64
+
65
+ ... for one message:
66
+ @wm.request(:send_message, :wmid => wmid, :subj => 'Subject', :text => 'Body of \<b>message\</b>')
67
+
68
+ ... for many messages (with queue):
69
+ @wm.send_message(:wmid => wmid, :subj => 'Subject', :text => 'Body of \<b>message\</b>')
70
+
71
+
72
+ Also, see examples into spec's.
data/lib/messenger.rb CHANGED
@@ -13,12 +13,12 @@ class Webmoney
13
13
  unless msg.nil?
14
14
  begin
15
15
  result = @webmoney.request(:send_message, msg)
16
+ # Requeue if fail
16
17
  @queue.push(msg) unless result.kind_of?(Hash)
17
- rescue ResultError
18
- puts "ResultError: #{@webmoney.error} #{@webmoney.errormsg}"
19
- # Silent drop message
20
- rescue ResponseError
21
- puts "ResponseError: #{@webmoney.error}"
18
+ rescue ResultError, ResponseError => e
19
+ # TODO Replace this to logger call
20
+ # puts "#{e}: #{@webmoney.error} #{@webmoney.errormsg}"
21
+
22
22
  # Requeue message
23
23
  @queue.push(msg)
24
24
  end
@@ -30,6 +30,8 @@ class Webmoney
30
30
  def push(msg)
31
31
  @queue.push(msg)
32
32
  end
33
+
34
+ # TODO callback on success send message
33
35
 
34
36
  end
35
37
 
data/lib/request.rb ADDED
@@ -0,0 +1,69 @@
1
+ module Webmoney::XMLRequest # :nodoc:all
2
+
3
+ def envelope(utf = true)
4
+ x = Builder::XmlMarkup.new(:indent => 1)
5
+ encoding = utf ? "utf-8" : "windows-1251"
6
+ x.instruct!(:xml, :version => "1.0", :encoding => encoding)
7
+ x
8
+ end
9
+
10
+ def xml_get_passport(opt)
11
+ x = envelope(false)
12
+ x.request do
13
+ x.wmid @wmid
14
+ x.passportwmid opt[:wmid]
15
+ x.params { x.dict 0; x.info 1; x.mode 0 }
16
+ x.sign sign(@wmid + opt[:wmid]) if classic?
17
+ end
18
+ x
19
+ end
20
+
21
+ def xml_bussines_level(opt)
22
+ x = envelope
23
+ x.tag!('WMIDLevel.request') do
24
+ x.signerwmid @wmid
25
+ x.wmid opt[:wmid]
26
+ end
27
+ x
28
+ end
29
+
30
+ def xml_check_sign(opt)
31
+ x = envelope(false)
32
+ x.tag!('w3s.request') do
33
+ x.wmid @wmid
34
+ plan_out = @ic_out.iconv(opt[:plan])
35
+ x.testsign do
36
+ x.wmid opt[:wmid]
37
+ x.plan { x.cdata! plan_out }
38
+ x.sign opt[:sign]
39
+ end
40
+ if classic?
41
+ plan = @wmid + opt[:wmid] + plan_out + opt[:sign]
42
+ x.sign sign(plan)
43
+ end
44
+ end
45
+ x
46
+ end
47
+
48
+ def xml_send_message(opt)
49
+ x = envelope(false)
50
+ req = reqn()
51
+ x.tag!('w3s.request') do
52
+ x.wmid @wmid
53
+ x.reqn req
54
+ msgsubj = @ic_out.iconv(opt[:subj])
55
+ msgtext = @ic_out.iconv(opt[:text])
56
+ x.message do
57
+ x.receiverwmid opt[:wmid]
58
+ x.msgsubj { x.cdata! msgsubj }
59
+ x.msgtext { x.cdata! msgtext }
60
+ end
61
+ if classic?
62
+ @plan = opt[:wmid] + req + msgtext + msgsubj
63
+ x.sign sign(@plan)
64
+ end
65
+ end
66
+ x
67
+ end
68
+
69
+ end
data/lib/result.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Webmoney::RequestResult # :nodoc:all
2
+
3
+ def result_check_sign(doc)
4
+ doc.at('//testsign/res').inner_html == 'yes' ? true : false
5
+ end
6
+
7
+ def result_get_passport(doc)
8
+ Webmoney::Passport.new(doc)
9
+ end
10
+
11
+ def result_bussines_level(doc)
12
+ doc.at('//level').inner_html.to_i
13
+ end
14
+
15
+ def result_send_message(doc)
16
+ time = doc.at('//message/datecrt').inner_html
17
+ m = time.match(/(\d{4})(\d{2})(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
18
+ time = Time.mktime(*m[1..6])
19
+ { :id => doc.at('//message')['id'], :date => time }
20
+ end
21
+ end
data/lib/webmoney.rb CHANGED
@@ -1,67 +1,7 @@
1
- =begin rdoc
2
- == About Webmoney library
3
-
4
- This library help to make requests to WebMoney Transfer http://www.wmtransfer.com
5
- XML-interfaces: http://www.wmtransfer.com/eng/developers/interfaces/index.shtml
6
-
7
- Gem have built-in native *wmsigner*.
8
-
9
- Author:: Alexander Oryol (mailto:eagle.alex@gmail.com)
10
- License:: MIT License
11
-
12
- == Request types
13
-
14
- - create_invoice - x1
15
- - create_transaction - x2
16
- - operation_history - x3
17
- - outgoing_invoices - x4
18
- - finish_protect - x5
19
- - send_message - x6
20
- - check_sign - x7
21
- - find_wm - x8
22
- - balance - x9
23
- - incoming_invoices - x10
24
- - get_passport - x11
25
- - reject_protection - x13
26
- - transaction_moneyback - x14
27
- - i_trust - x15
28
- - trust_me - x15
29
- - trust_save - x15
30
- - create_purse - x16
31
- - bussines_level
32
-
33
- Please, see relative documentation and parameters on wiki:
34
-
35
- http://wiki.wmtransfer.com/wiki/list/XML-Interfaces
36
-
37
- http://wiki.webmoney.ru/wiki/list/XML-%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D1%84%D0%B5%D0%B9%D1%81%D1%8B (in russian)
38
-
39
- or official sites:
40
-
41
- http://www.wmtransfer.com/eng/developers/interfaces/xml/index.shtml
42
-
43
- http://www.webmoney.ru/rus/developers/interfaces/xml/index.shtml (in russian)
44
-
45
- == Examples
46
-
47
- @wm = Webmoney.new(:wmid => '123456789012', :password => 'my_pass', :key => 'gQABAIR6...2cC8FZTyKyjBM=')
48
-
49
- passport = @wm.request(:get_passport, :wmid => @wm.wmid)
50
-
51
- bl = @wm.request(:bussines_level, :wmid => '123456789012')
52
-
53
- @wm.request(:send_message, :wmid => @wm.wmid, :subj => 'Subject', :text => 'Body of \<b>message\</b>')
54
-
55
-
56
- Also, see examples into spec's.
57
- =end
58
-
59
-
60
1
  # :title:Webmoney library Documentation
61
2
  # :main:lib/webmoney.rb
62
3
  # :include:README
63
4
 
64
- require File.dirname(__FILE__) +'/wmsigner'
65
5
  require 'time'
66
6
  require 'net/http'
67
7
  require 'net/https'
@@ -70,11 +10,23 @@ require 'iconv'
70
10
  require 'builder'
71
11
  require 'hpricot'
72
12
 
13
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
14
+
15
+ require 'wmsigner'
16
+ require 'wmid'
17
+ require 'passport'
18
+ require 'request'
19
+ require 'result'
20
+ require 'messenger'
21
+
73
22
  # Main class for Webmoney lib. Instance contain info
74
23
  # for WMT-interfaces requests (wmid, key, etc).
75
24
  # Implement general requests.
76
25
  class Webmoney
77
26
 
27
+ include XMLRequest
28
+ include RequestResult
29
+
78
30
  # Error classes
79
31
  class WebmoneyError < StandardError; end
80
32
  class RequestError < WebmoneyError; end
@@ -82,10 +34,6 @@ class Webmoney
82
34
  class IncorrectWmidError < WebmoneyError; end
83
35
  class CaCertificateError < WebmoneyError; end
84
36
 
85
- require File.dirname(__FILE__) + '/../lib/wmid'
86
- require File.dirname(__FILE__) + '/../lib/passport'
87
- require File.dirname(__FILE__) + '/../lib/messenger'
88
-
89
37
  attr_reader :wmid, :error, :errormsg, :last_request, :messenger
90
38
 
91
39
  # Required options:
@@ -172,69 +120,13 @@ class Webmoney
172
120
  reqn = reqn()
173
121
  raise ArgumentError unless opt.kind_of?(Hash)
174
122
  opt[:wmid] = @wmid if opt[:wmid].nil?
175
- x = Builder::XmlMarkup.new(:indent => 1)
176
- x.instruct!(:xml, :version=>"1.0", :encoding=>"windows-1251")
177
- unless [:get_passport, :bussines_level].include?(iface)
178
- x.tag!('w3s.request') do
179
- x.wmid @wmid
180
- case iface
181
- when :check_sign then
182
- plan_out = @ic_out.iconv(opt[:plan])
183
- x.testsign do
184
- x.wmid opt[:wmid]
185
- x.plan { x.cdata! plan_out }
186
- x.sign opt[:sign]
187
- end
188
- plan = @wmid + opt[:wmid] + plan_out + opt[:sign]
189
- when :send_message
190
- x.reqn reqn
191
- msgsubj = @ic_out.iconv(opt[:subj])
192
- msgtext = @ic_out.iconv(opt[:text])
193
- x.message do
194
- x.receiverwmid opt[:wmid]
195
- x.msgsubj { x.cdata! msgsubj }
196
- x.msgtext { x.cdata! msgtext }
197
- end
198
- plan = opt[:wmid] + reqn + msgtext + msgsubj
199
- end
200
- x.sign sign(plan) if classic?
201
- end
202
- else
203
- case iface
204
- when :bussines_level
205
- x.tag!('WMIDLevel.request') do
206
- x.signerwmid @wmid
207
- x.wmid opt[:wmid]
208
- end
209
- when :get_passport
210
- x.request do
211
- x.wmid @wmid
212
- x.passportwmid opt[:wmid]
213
- x.params { x.dict 0; x.info 1; x.mode 0 }
214
- x.sign sign(@wmid + opt[:wmid]) if classic?
215
- end
216
- end
217
- end
218
-
123
+
219
124
  # Do request
220
- res = https_request(iface, x.target!)
221
-
125
+ res = https_request(iface, make_xml(iface, opt))
126
+
222
127
  # Parse response
223
128
  parse_retval(res)
224
- doc = Hpricot.XML(res)
225
- case iface
226
- when :check_sign
227
- return doc.at('//testsign/res').inner_html == 'yes' ? true : false
228
- when :get_passport
229
- return Passport.new(doc)
230
- when :bussines_level
231
- return doc.at('//level').inner_html.to_i
232
- when :send_message
233
- time = doc.at('//message/datecrt').inner_html
234
- m = time.match(/(\d{4})(\d{2})(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
235
- time = Time.mktime(*m[1..6])
236
- return {:id => doc.at('//message')['id'], :date => time}
237
- end
129
+ make_result(iface, res)
238
130
  end
239
131
 
240
132
  # Signing string by instance wmid's,
@@ -260,7 +152,7 @@ class Webmoney
260
152
  if File.file? @ca_cert
261
153
  http.ca_file = @ca_cert
262
154
  else
263
- raise CaCertificateError
155
+ raise CaCertificateError, @ca_cert
264
156
  end
265
157
  http.use_ssl = true
266
158
  @last_request = xml
@@ -273,7 +165,7 @@ class Webmoney
273
165
  else
274
166
  @error = result.code
275
167
  @errormsg = result.body if result.class.body_permitted?()
276
- raise RequestError
168
+ raise RequestError, [@error, @errormsg].join(' ')
277
169
  end
278
170
  end
279
171
 
@@ -291,7 +183,7 @@ class Webmoney
291
183
  unless retval == 0
292
184
  @error = retval
293
185
  @errormsg = retdesc
294
- raise ResultError
186
+ raise ResultError, [@error, @errormsg].join(' ')
295
187
  end
296
188
  end
297
189
 
@@ -301,5 +193,20 @@ class Webmoney
301
193
  t = Time.now
302
194
  t.strftime('%Y%m%d%H%M%S') + t.to_f.to_s.match(/\.(\d\d)/)[1]
303
195
  end
304
-
196
+
197
+ def make_xml(iface, opt) # :nodoc:
198
+ iface_func = ('xml_'+iface.to_s).to_sym
199
+ self.send(iface_func, opt).target!
200
+ rescue NoMethodError
201
+ raise NotImplementedError, "#{iface_func}()"
202
+ end
203
+
204
+ def make_result(iface, res) # :nodoc:
205
+ doc = Hpricot.XML(res)
206
+ iface_result = ('result_'+iface.to_s).to_sym
207
+ self.send(iface_result, doc)
208
+ rescue NoMethodError
209
+ raise NotImplementedError, "#{iface_result}()"
210
+ end
211
+
305
212
  end
data/lib/wmid.rb CHANGED
@@ -6,7 +6,7 @@ class Webmoney
6
6
 
7
7
  def initialize(str)
8
8
  str = str.to_s unless str.kind_of?(String)
9
- raise IncorrectWmidError, ': ' + str.to_s unless str =~ /^\d{12}$/
9
+ raise IncorrectWmidError, str unless str =~ /^\d{12}$/
10
10
  super(str)
11
11
  end
12
12
 
data/rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake/clean'
4
4
  require 'tools/rakehelp'
5
5
  require 'spec/rake/spectask'
6
6
 
7
- GEM_VERSION="0.0.4.3"
7
+ GEM_VERSION="0.0.4.4"
8
8
 
9
9
  setup_extension('wmsigner', 'wmsigner')
10
10
 
@@ -8,16 +8,20 @@ class Webmoney
8
8
  @wm = webmoney()
9
9
  end
10
10
 
11
- it "should create instance and send messages" do
11
+ it "should create instance" do
12
12
  @wm.messenger.should be_nil
13
13
  @wm.send_message(:wmid => @wm.wmid, :subj => 'FIRST', :text => 'BODY')
14
14
  @wm.messenger.should be_instance_of(Messenger)
15
- @wm.send_message(:wmid => @wm.wmid, :subj => 'SECOND', :text => 'SECOUND')
16
- sleep(3)
17
15
  end
18
16
 
19
- # TODO HOW TEST IT FUNCTIONALITY???
20
-
17
+ it "should call request(:send_message) twice" do
18
+ params = { :wmid => @wm.wmid, :subj => 'FIRST', :text => 'BODY' }
19
+ @wm.should_receive(:request).
20
+ with(:send_message, params).twice().and_return({:test => 'test'})
21
+ @wm.send_message(params)
22
+ @wm.send_message(params)
23
+ end
24
+
21
25
  end
22
26
 
23
27
  end
@@ -44,9 +44,9 @@ class Webmoney
44
44
  end
45
45
 
46
46
  it "should parse retval and raise error" do
47
- lambda { @wm.request(:create_transaction)}.should raise_error(ResultError)
48
- @wm.error.should == -3
49
- @wm.errormsg.should match(%r{incorrect value of w3s.request/trans/tranid is incorrect})
47
+ lambda { @wm.request(:send_message, :wmid => '')}.should raise_error(ResultError)
48
+ @wm.error.should == -2
49
+ @wm.errormsg.should match(%r{value of w3s.request/message/receiverwmid is incorrect})
50
50
  end
51
51
 
52
52
  it "should sign string" do
@@ -106,6 +106,10 @@ class Webmoney
106
106
  ((result[:date] + 60) > Time.now).should be_true
107
107
  end
108
108
 
109
+ it "should raise error on undefined xml func" do
110
+ lambda { @wm.request(:unexistent_interface) }.should raise_error(::NotImplementedError)
111
+ end
112
+
109
113
  end
110
114
 
111
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmoney
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.3
4
+ version: 0.0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Oryol
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-22 00:00:00 +03:00
12
+ date: 2009-03-15 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -47,6 +47,8 @@ files:
47
47
  - README
48
48
  - lib/WebMoneyCA.crt
49
49
  - lib/messenger.rb
50
+ - lib/request.rb
51
+ - lib/result.rb
50
52
  - lib/passport.rb
51
53
  - lib/webmoney.rb
52
54
  - lib/wmid.rb