amazon_flex_pay 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -21,14 +21,14 @@ Note that while the examples are shown using Rails methods, the only Rails requi
21
21
 
22
22
  2. Then on the return page, take the sender token and run a Pay request:
23
23
 
24
- response = AmazonFlexPay.pay('12.99', 'USD', 'STOKEN', 'myrequest3292')
25
- if response.error?
24
+ begin
25
+ response = AmazonFlexPay.pay('12.99', 'USD', 'STOKEN', 'myrequest3292')
26
+ flash[:notice] = "Thanks! Your payment is processing."
27
+ rescue AmazonFlexPay::API::ErrorResponse => e
26
28
  flash[:error] = "Sorry, something went wrong."
27
- response.errors.each do |error|
29
+ e.errors.each do |error|
28
30
  # notify yourself about error.code and error.message
29
31
  end
30
- else
31
- flash[:notice] = "Thanks! Your payment is processing."
32
32
  end
33
33
  redirect_to product_path
34
34
 
@@ -16,7 +16,7 @@ require 'amazon_flex_pay/api'
16
16
  require 'amazon_flex_pay/pipelines'
17
17
 
18
18
  module AmazonFlexPay
19
- VERSION = '0.9.2'
19
+ VERSION = '0.9.3'
20
20
  API_VERSION = '2010-08-28'
21
21
  PIPELINE_VERSION = '2009-01-09'
22
22
 
@@ -50,4 +50,3 @@ module AmazonFlexPay
50
50
  end
51
51
 
52
52
  end
53
-
@@ -1,16 +1,47 @@
1
1
  module AmazonFlexPay::API #:nodoc:
2
+ class ErrorResponse < StandardError
3
+ # Re-implements the XML parsing because ErrorResponse does not inherit from BaseResponse.
4
+ def self.from_xml(xml)
5
+ new(MultiXml.parse(xml)['Response'])
6
+ end
7
+
8
+ def initialize(hash)
9
+ self.request_id = hash['RequestID']
10
+ self.errors = hash['Errors']
11
+ end
12
+
13
+ attr_accessor :request
14
+ attr_accessor :request_id
15
+
16
+ attr_reader :errors
17
+ def errors=(val)
18
+ @errors = [val['Error']].flatten.map{|e| Error.new(e)}
19
+ end
20
+
21
+ def to_s
22
+ errors.map{|e| "#{e.code}: #{e.message}"}.join(', ')
23
+ end
24
+
25
+ class Error < AmazonFlexPay::Model #:nodoc:
26
+ attribute :code
27
+ attribute :message
28
+ end
29
+ end
30
+
2
31
  class Base < AmazonFlexPay::Model
3
32
  # This compiles an API request object into a URL, sends it to Amazon, and processes
4
33
  # the response.
5
34
  def submit
6
35
  url = AmazonFlexPay.api_endpoint + '?' + AmazonFlexPay.query_string(self.to_params)
7
- response = begin
8
- self.class::Response.from_xml(RestClient.get(url).body)
36
+ begin
37
+ response = self.class::Response.from_xml(RestClient.get(url).body)
38
+ response.request = self
39
+ response
9
40
  rescue RestClient::BadRequest, RestClient::Unauthorized, RestClient::Forbidden => e
10
- ErrorResponse.from_xml(e.response.body)
41
+ er = ErrorResponse.from_xml(e.response.body)
42
+ er.request = self
43
+ raise er
11
44
  end
12
- response.request = self
13
- response
14
45
  end
15
46
 
16
47
  # Converts the API request object into parameters and signs them.
@@ -33,11 +64,6 @@ module AmazonFlexPay::API #:nodoc:
33
64
  attribute :request
34
65
  attribute :request_id
35
66
 
36
- # Check response.error? to determine whether it's an ErrorResponse or not.
37
- def error?
38
- false
39
- end
40
-
41
67
  # Parses Amazon's XML response to REST requests and instantiates the response.
42
68
  def self.from_xml(xml)
43
69
  response = MultiXml.parse(xml)
@@ -52,31 +78,6 @@ module AmazonFlexPay::API #:nodoc:
52
78
  end
53
79
  end
54
80
 
55
- class ErrorResponse < AmazonFlexPay::Model #:nodoc:
56
- # Re-implements the XML parsing because ErrorResponse does not inherit from BaseResponse.
57
- def self.from_xml(xml)
58
- new(MultiXml.parse(xml)['Response'])
59
- end
60
-
61
- # Check response.error? to determine whether Amazon accepted the request.
62
- def error?
63
- true
64
- end
65
-
66
- attribute :request
67
- attribute :request_id
68
-
69
- attr_reader :errors
70
- def errors=(val)
71
- @errors = [val['Error']].flatten.map{|e| Error.new(e)}
72
- end
73
-
74
- class Error < AmazonFlexPay::Model #:nodoc:
75
- attribute :code
76
- attribute :message
77
- end
78
- end
79
-
80
81
  protected
81
82
 
82
83
  def action_name #:nodoc:
@@ -32,9 +32,6 @@ module AmazonFlexPay
32
32
  attribute_names << attr.to_s.camelcase
33
33
  name = attr.to_s.underscore
34
34
 
35
- # reader
36
- attr_reader name
37
-
38
35
  # writer
39
36
  if options[:enumeration]
40
37
  enumerated_attribute(name, options[:enumeration])
@@ -43,7 +40,7 @@ module AmazonFlexPay
43
40
  elsif options[:collection]
44
41
  collection_attribute(name, options[:collection])
45
42
  else
46
- attr_writer name
43
+ attr_accessor name
47
44
  end
48
45
  end
49
46
 
@@ -55,6 +52,10 @@ module AmazonFlexPay
55
52
  def enumerated_attribute(attr, source = nil) #:nodoc:
56
53
  source ||= attr
57
54
  class_eval <<-END
55
+ def #{attr}
56
+ @#{attr}
57
+ end
58
+
58
59
  def #{attr}=(val)
59
60
  options = AmazonFlexPay::Enumerations::#{source.to_s.camelcase}
60
61
  unless options.include?(val)
@@ -68,6 +69,10 @@ module AmazonFlexPay
68
69
  def complex_attribute(attr, data_type = nil) #:nodoc:
69
70
  data_type ||= attr
70
71
  class_eval <<-END
72
+ def #{attr}
73
+ @#{attr} ||= AmazonFlexPay::DataTypes::#{data_type.to_s.camelcase}.new
74
+ end
75
+
71
76
  def #{attr}=(hash)
72
77
  @#{attr} = AmazonFlexPay::DataTypes::#{data_type.to_s.camelcase}.new(hash)
73
78
  end
@@ -76,6 +81,10 @@ module AmazonFlexPay
76
81
 
77
82
  def collection_attribute(attr, data_type = nil) #:nodoc:
78
83
  class_eval <<-END
84
+ def #{attr}
85
+ @#{attr} ||= []
86
+ end
87
+
79
88
  def #{attr}=(array)
80
89
  @#{attr} = [array].flatten.map{|hash| AmazonFlexPay::DataTypes::#{data_type.to_s.camelcase}.new(hash)}
81
90
  end
@@ -93,7 +102,7 @@ module AmazonFlexPay
93
102
  def to_hash
94
103
  self.class.attribute_names.inject({}) do |hash, name|
95
104
  val = send(name.underscore)
96
- if val.nil? or val == ''
105
+ if val.nil? or val == '' or val == []
97
106
  hash
98
107
  else
99
108
  hash.merge(format_key(name) => val.is_a?(AmazonFlexPay::Model) ? val.to_hash : format_value(val))
@@ -44,9 +44,16 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
44
44
  class TestRequest < AmazonFlexPay::API::Base
45
45
  attribute :foo
46
46
  attribute :amount, :type => :amount
47
+ attribute :stuffs, :collection => :amount
47
48
 
48
49
  class Response < AmazonFlexPay::API::Base::BaseResponse; end
49
50
  end
51
+
52
+ should "respond with data structures even when models are empty" do
53
+ tr = TestRequest.new
54
+ assert tr.stuffs.is_a?(Array)
55
+ assert tr.amount.respond_to?(:value)
56
+ end
50
57
 
51
58
  should "add necessary fields and sign api requests" do
52
59
  Time.stubs(:now).returns(Time.parse('Jan 1 2011')) # so the signature remains constant
@@ -82,11 +89,15 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
82
89
  http_response = RestClient::Response.create(error_response, nil, nil)
83
90
  RestClient.expects(:get).raises(RestClient::BadRequest.new(http_response))
84
91
 
85
- response = TestRequest.new(:foo => 'bar').submit
86
- assert response.request_id
87
- assert response.error?
88
- assert response.errors.first.code
89
- assert response.errors.first.message
92
+ error = nil
93
+ begin
94
+ TestRequest.new(:foo => 'bar').submit
95
+ rescue AmazonFlexPay::API::ErrorResponse => e
96
+ error = e
97
+ end
98
+ assert error.request_id
99
+ assert error.errors.first.code
100
+ assert error.errors.first.message
90
101
  end
91
102
 
92
103
  should "not allow unknown values for enumerated attributes" do
data/test/api_test.rb CHANGED
@@ -17,7 +17,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
17
17
  assert_nothing_raised do
18
18
  response = AmazonFlexPay::API::Cancel::Response.from_xml(cancel_response)
19
19
  end
20
- assert !response.error?
21
20
  assert response.request_id
22
21
  assert response.transaction_id
23
22
  assert response.transaction_status
@@ -37,7 +36,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
37
36
  assert_nothing_raised do
38
37
  response = AmazonFlexPay::API::CancelToken::Response.from_xml(cancel_token_response)
39
38
  end
40
- assert !response.error?
41
39
  assert response.request_id
42
40
  end
43
41
 
@@ -57,7 +55,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
57
55
  assert_nothing_raised do
58
56
  response = AmazonFlexPay::API::GetAccountActivity::Response.from_xml(get_account_activity_response)
59
57
  end
60
- assert !response.error?
61
58
  assert response.request_id
62
59
  assert_equal 5, response.transactions.count
63
60
  end
@@ -76,7 +73,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
76
73
  assert_nothing_raised do
77
74
  response = AmazonFlexPay::API::GetAccountBalance::Response.from_xml(get_account_balance_response)
78
75
  end
79
- assert !response.error?
80
76
  assert response.request_id
81
77
  assert_equal '7.400000', response.account_balance.total_balance.value
82
78
  end
@@ -95,7 +91,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
95
91
  assert_nothing_raised do
96
92
  response = AmazonFlexPay::API::GetRecipientVerificationStatus::Response.from_xml(get_recipient_verification_status_response)
97
93
  end
98
- assert !response.error?
99
94
  assert response.request_id
100
95
  assert response.recipient_verification_status
101
96
  end
@@ -121,7 +116,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
121
116
  assert_nothing_raised do
122
117
  response = AmazonFlexPay::API::GetTokenByCaller::Response.from_xml(get_token_by_caller_response)
123
118
  end
124
- assert !response.error?
125
119
  assert response.request_id
126
120
  assert response.token.token_id
127
121
  assert response.token.token_status
@@ -177,7 +171,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
177
171
  assert_nothing_raised do
178
172
  response = AmazonFlexPay::API::GetTransaction::Response.from_xml(get_transaction_response)
179
173
  end
180
- assert !response.error?
181
174
  assert response.request_id
182
175
  assert response.transaction.caller_reference
183
176
  assert response.transaction.payment_method
@@ -197,7 +190,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
197
190
  assert_nothing_raised do
198
191
  response = AmazonFlexPay::API::GetTransactionStatus::Response.from_xml(get_transaction_status_response)
199
192
  end
200
- assert !response.error?
201
193
  assert response.request_id
202
194
  assert response.transaction_id
203
195
  assert_equal 'Success', response.transaction_status
@@ -218,7 +210,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
218
210
  assert_nothing_raised do
219
211
  response = AmazonFlexPay::API::Pay::Response.from_xml(reserve_response)
220
212
  end
221
- assert !response.error?
222
213
  assert response.request_id
223
214
  assert response.transaction_id
224
215
  assert response.transaction_status
@@ -239,7 +230,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
239
230
  assert_nothing_raised do
240
231
  response = AmazonFlexPay::API::Refund::Response.from_xml(refund_response)
241
232
  end
242
- assert !response.error?
243
233
  assert response.request_id
244
234
  assert response.transaction_id
245
235
  assert response.transaction_status
@@ -261,7 +251,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
261
251
  assert_nothing_raised do
262
252
  response = AmazonFlexPay::API::Reserve::Response.from_xml(reserve_response)
263
253
  end
264
- assert !response.error?
265
254
  assert response.request_id
266
255
  assert response.transaction_id
267
256
  assert response.transaction_status
@@ -283,7 +272,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
283
272
  assert_nothing_raised do
284
273
  response = AmazonFlexPay::API::Settle::Response.from_xml(settle_response)
285
274
  end
286
- assert !response.error?
287
275
  assert response.request_id
288
276
  assert response.transaction_id
289
277
  assert response.transaction_status
@@ -303,7 +291,6 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
303
291
  assert_nothing_raised do
304
292
  response = AmazonFlexPay::API::VerifySignature::Response.from_xml(verify_signature_response)
305
293
  end
306
- assert !response.error?
307
294
  assert response.request_id
308
295
  assert response.verification_status
309
296
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon_flex_pay
3
3
  version: !ruby/object:Gem::Version
4
- hash: 63
4
+ hash: 61
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 2
10
- version: 0.9.2
9
+ - 3
10
+ version: 0.9.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lance Ivy
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-10 00:00:00 -08:00
18
+ date: 2011-03-11 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency