amazon_flex_pay 0.9.12 → 0.9.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,7 @@ require 'rest_client'
7
7
  require 'multi_xml'
8
8
  require 'active_support' # camelcase, underscore
9
9
  require 'active_support/inflector'
10
+ require 'active_support/notifications'
10
11
 
11
12
  require 'amazon_flex_pay/signing'
12
13
  require 'amazon_flex_pay/model'
@@ -17,7 +18,7 @@ require 'amazon_flex_pay/api'
17
18
  require 'amazon_flex_pay/pipelines'
18
19
 
19
20
  module AmazonFlexPay
20
- VERSION = '0.9.12'
21
+ VERSION = '0.9.13'
21
22
  API_VERSION = '2011-09-20'
22
23
  PIPELINE_VERSION = '2009-01-09'
23
24
 
@@ -4,14 +4,25 @@ module AmazonFlexPay::API #:nodoc:
4
4
  # the response.
5
5
  def submit
6
6
  url = AmazonFlexPay.api_endpoint + '?' + AmazonFlexPay.query_string(self.to_params)
7
- begin
8
- response = self.class::Response.from_xml(RestClient.get(url).body)
9
- response.request = self
10
- response
11
- rescue RestClient::BadRequest, RestClient::Unauthorized, RestClient::Forbidden => e
12
- er = ErrorResponse.from_xml(e.response.body)
13
- klass = AmazonFlexPay::API.const_get(er.errors.first.code)
14
- raise klass.new(er.errors.first.code, er.errors.first.message, er.request_id, self)
7
+ ActiveSupport::Notifications.instrument("amazon_flex_pay.api", :action => action_name, :request => url) do |payload|
8
+ begin
9
+ http = RestClient.get(url)
10
+
11
+ payload[:response] = http.body
12
+ payload[:code] = http.code
13
+
14
+ response = self.class::Response.from_xml(http.body)
15
+ response.request = self
16
+ response
17
+
18
+ rescue RestClient::BadRequest, RestClient::Unauthorized, RestClient::Forbidden => e
19
+ payload[:response] = e.http_body
20
+ payload[:code] = e.http_code
21
+
22
+ er = ErrorResponse.from_xml(e.response.body)
23
+ klass = AmazonFlexPay::API.const_get(er.errors.first.code)
24
+ raise klass.new(er.errors.first.code, er.errors.first.message, er.request_id, self)
25
+ end
15
26
  end
16
27
  end
17
28
 
@@ -5,6 +5,10 @@ module AmazonFlexPay::API
5
5
  def initialize(code, message, request_id, request)
6
6
  @request_id, @request, @code, @message = request_id, request, code, message
7
7
  end
8
+
9
+ def to_s
10
+ message
11
+ end
8
12
  end
9
13
 
10
14
  # generated from http://docs.amazonwebservices.com/AmazonFPS/latest/FPSAPIReference/APIErrorCodesTable.html
@@ -83,13 +83,31 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
83
83
  end
84
84
 
85
85
  should "store the request in the response" do
86
- RestClient.expects(:get).returns(stub(:body => cancel_token_response))
86
+ RestClient.expects(:get).returns(stub(:body => cancel_token_response, :code => 200))
87
87
  response = TestRequest.new(:foo => 'bar').submit
88
88
  assert_equal 'bar', response.request.foo
89
89
  end
90
90
 
91
+ should "instrument successful responses" do
92
+ events = []
93
+ callback = proc{ |*args| events << ActiveSupport::Notifications::Event.new(*args) }
94
+
95
+ ActiveSupport::Notifications.subscribed(callback, "amazon_flex_pay.api") do
96
+ RestClient.expects(:get).returns(stub(:body => cancel_token_response, :code => 200))
97
+ TestRequest.new(:foo => 'bar').submit
98
+ end
99
+
100
+ assert_equal 1, events.size
101
+ assert_equal 'TestRequest', events.first.payload[:action]
102
+ assert_equal 200, events.first.payload[:code]
103
+ assert events.first.payload.has_key?(:request)
104
+ assert events.first.payload.has_key?(:response)
105
+ assert events.first.duration > 0.1, events.first.duration.to_s
106
+ end
107
+
91
108
  should "catch and parse errors" do
92
- http_response = RestClient::Response.create(error_response, nil, nil)
109
+ net_http_res = stub(:code => 400)
110
+ http_response = RestClient::Response.create(error_response, net_http_res, nil)
93
111
  RestClient.expects(:get).raises(RestClient::BadRequest.new(http_response))
94
112
 
95
113
  error = nil
@@ -103,6 +121,26 @@ class AmazonFlexPayTest < AmazonFlexPay::Test
103
121
  assert error.message.match(/has to be a valid/)
104
122
  end
105
123
 
124
+ should "instrument error responses" do
125
+ events = []
126
+ callback = proc{ |*args| events << ActiveSupport::Notifications::Event.new(*args) }
127
+
128
+ ActiveSupport::Notifications.subscribed(callback, "amazon_flex_pay.api") do
129
+ net_http_res = stub(:code => 400)
130
+ http_response = RestClient::Response.create(error_response, net_http_res, nil)
131
+ RestClient.expects(:get).raises(RestClient::BadRequest.new(http_response))
132
+
133
+ TestRequest.new(:foo => 'bar').submit rescue AmazonFlexPay::API::Error
134
+ end
135
+
136
+ assert_equal 1, events.size
137
+ assert_equal 'TestRequest', events.first.payload[:action]
138
+ assert_equal 400, events.first.payload[:code]
139
+ assert events.first.payload.has_key?(:request)
140
+ assert events.first.payload.has_key?(:response)
141
+ assert events.first.duration > 0.1, events.first.duration.to_s
142
+ end
143
+
106
144
  should "not allow unknown values for enumerated attributes" do
107
145
  assert_raises ArgumentError do TestRequest.new(:method => 'UNKOWN') end
108
146
  end
@@ -1,8 +1,7 @@
1
1
  # load the support libraries
2
2
  require 'test/unit'
3
3
  require 'rubygems'
4
- #gem 'rails', '2.3.11'
5
- gem 'rails', '3.0.10'
4
+ gem 'rails', '3.2.5'
6
5
  require 'mocha'
7
6
 
8
7
  # load the gem
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon_flex_pay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.12
4
+ version: 0.9.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-18 00:00:00.000000000 Z
12
+ date: 2012-06-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &70095439663980 !ruby/object:Gem::Requirement
16
+ requirement: &70160943188200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70095439663980
24
+ version_requirements: *70160943188200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: multi_xml
27
- requirement: &70095439662220 !ruby/object:Gem::Requirement
27
+ requirement: &70160943186600 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,21 +32,21 @@ dependencies:
32
32
  version: 0.2.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70095439662220
35
+ version_requirements: *70160943186600
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
38
- requirement: &70095439661660 !ruby/object:Gem::Requirement
38
+ requirement: &70160943185140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
42
42
  - !ruby/object:Gem::Version
43
- version: 2.3.11
43
+ version: 3.0.14
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70095439661660
46
+ version_requirements: *70160943185140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mocha
49
- requirement: &70095439661160 !ruby/object:Gem::Requirement
49
+ requirement: &70160943184420 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70095439661160
57
+ version_requirements: *70160943184420
58
58
  description: A straight-forward REST API for Amazon's Flexible Payments Services.
59
59
  email: lance@kickstarter.com
60
60
  executables: []