paypal_client 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13c0a0a54e40d7cabab0577d59577c4155b0ce83
4
- data.tar.gz: 1b4a3cd003b69dfbf092d1c7d5bb52ba5cb028a9
3
+ metadata.gz: 114fef4fab02f2da43e422615fb0d80edff7bb4e
4
+ data.tar.gz: 448d08ed8b3025c4f4d14ea93ac36efcc0e03725
5
5
  SHA512:
6
- metadata.gz: 7b9ee71b2a0fc7bea795e97e769eedef8b7144261757a4167190ec4a821afd66b9d317508a66a3c259840d5ee45865d0c875b407cfd023e8f82e012c3810ad3d
7
- data.tar.gz: f3d5d29d0cd208dd353da6a9dd5f73e056f38b43ce2c27a66d7efc7cafe7432aef290c80a552879dd02e02b46ae6c4cfa77bf8759a55d0d64e70f6e8e190756d
6
+ metadata.gz: 7ee81de3aaa594ee3f992ddd196360091d8ba33159a598a808f9500982324e7da878e81e94bab01d9d54a2af1464ba263f097b0dbcfa2ec7616fed79ec9b1e53
7
+ data.tar.gz: 490d3240ae8197d8d8d2874bfa163a13d9b6f1e66fc3e6097fb738e31409b11499aa3e2ff8c282a2d846255e785b50bf7152180b87b762ec221a04354e94a719
@@ -1,31 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday_middleware'
2
4
 
3
5
  module PaypalClient
4
6
  module Errors
5
7
  class Error < StandardError
6
- attr_reader :status_code
7
- attr_reader :body
8
- attr_reader :error
9
- attr_reader :error_message
10
-
11
- def initialize(response)
12
- @status_code = response.status
13
- @body = response.body
14
-
15
- if @body.class == Hash
16
- @error = @body[:name] if @body.key?(:name)
17
- @error_message = @body[:message] if @body.key?(:message)
18
- else
19
- @error = response.reason_phrase if @error.nil?
20
- end
8
+ attr_reader :code
9
+ attr_reader :http_body
10
+ attr_reader :http_status
11
+ attr_reader :message
12
+
13
+ def initialize(message = nil, http_status: nil, http_body: nil, code: nil)
14
+ @message = message
15
+ @http_status = http_status
16
+ @http_body = http_body
17
+ @code = code
18
+
21
19
  super(@error)
22
20
  end
23
21
 
24
22
  def inspect
25
- extra = ''
26
- extra << " status_code: #{status_code.inspect}" unless status_code.nil?
27
- extra << " body: #{body.inspect}" unless body.nil?
28
- "#<#{self.class.name}: #{message}#{extra}>"
23
+ extra = []
24
+ extra << " status_code: #{http_status}" unless http_status.nil?
25
+ extra << " body: #{http_body}" unless http_body.nil?
26
+ "#<#{self.class.name}: #{message}#{extra.join}>"
29
27
  end
30
28
  end
31
29
 
@@ -57,10 +55,34 @@ module PaypalClient
57
55
  503 => ServiceUnavailable
58
56
  }.freeze
59
57
 
58
+ ERRORS_WITHOUT_BODY = [404].freeze
59
+
60
60
  def on_complete(response)
61
- key = response[:status].to_i
62
- raise ERROR_MAP[key], response if ERROR_MAP.key? key
63
- raise Error, response if response.status >= 400
61
+ status = response[:status].to_i
62
+ if status >= 400
63
+ error = ERROR_MAP.key?(status) ? ERROR_MAP[status] : Error
64
+
65
+ body = response.body
66
+ raise error.new(get_error_message(response), code: get_error_code(response), http_status: status, http_body: body)
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def get_error_message(response)
73
+ body = response.body
74
+ return body[:message] if body.is_a?(Hash) && body.key?(:message)
75
+ return body[:error_description] if body.is_a?(Hash) && body.key?(:error_description)
76
+
77
+ 'Something went wrong'
78
+ end
79
+
80
+ def get_error_code(response)
81
+ body = response.body
82
+ return body[:name] if body.is_a?(Hash) && body.key?(:name)
83
+ return body[:error] if body.is_a?(Hash) && body.key?(:error)
84
+
85
+ response.status.to_s
64
86
  end
65
87
  end
66
88
  end
@@ -1,3 +1,3 @@
1
1
  module PaypalClient
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paypal_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis van der Vliet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-02 00:00:00.000000000 Z
11
+ date: 2019-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport