easy_meli 0.6.4 → 0.6.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bea2b5ce32b7601401b386bbde1fd0e1bdc8bcfe7c2fab55fe9d44d065695b2b
4
- data.tar.gz: fb22b1e7d147f20d1d4638a66b4ab0eb57a2490e7d5b59725593dab506837438
3
+ metadata.gz: 269c22ea4f450792d02935692db9eec8adc207fc8f44c946f1d3f439cce435b1
4
+ data.tar.gz: 00565d53e5a9e3b40235bce3723ab962d7e3f0a8fb912713330255de90b7980c
5
5
  SHA512:
6
- metadata.gz: 0dae6c7df5a569e55828291fbca8986af2538309b7df51d7be10da6007857dc71d6442797375dd0a68ddf861920a828ddfa77f57c38095249c60ce34ca789622
7
- data.tar.gz: 1fa152bcb974cc631d2b5e0d990ed37cb610b4ef253966a10f948a86cdca66ceb421b87e569c9f3af860d4ebe2fead62d7e9034a2f978671a40e16e829aaf56a
6
+ metadata.gz: 7f3e5971e80a8a9b73b435186fad527d93e71f0d454208ecf78a412c5716726f28ab0fbc25cca60dc6701422adf089d05ae504a86d3b34efb918ef4ba1685223
7
+ data.tar.gz: 6f1e0cb4af4919bb9c15fbd15526787c1f13727b870d5b2345a36d67f5e790bd9602eb39fde70dcb137d62856eda5e789c2c3629962fb0c081d5ddc09d4fb738
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## V 0.6.5
4
+ - Move Error class search to its own class and reuse it for ApiClient and AuthorizationClient
5
+ - Raise EasyMeli exceptions for server side errors.
6
+
3
7
  ## V 0.6.4
4
8
  - Add Unknown error support
5
9
 
data/lib/easy_meli.rb CHANGED
@@ -3,6 +3,7 @@ require 'httparty'
3
3
  require 'easy_meli/version'
4
4
  require 'easy_meli/constants'
5
5
  require 'easy_meli/errors'
6
+ require 'easy_meli/error_parser'
6
7
  require 'easy_meli/configuration'
7
8
  require 'easy_meli/authorization_client'
8
9
  require 'easy_meli/api_client'
@@ -5,15 +5,6 @@ class EasyMeli::ApiClient
5
5
 
6
6
  API_ROOT_URL = 'https://api.mercadolibre.com'
7
7
 
8
- ERROR_LIST = {
9
- 'Error validating grant' => EasyMeli::InvalidGrantError,
10
- 'The User ID must match the consultant\'s' => EasyMeli::ForbiddenError,
11
- 'invalid_token' => EasyMeli::InvalidTokenError,
12
- 'Malformed access_token' => EasyMeli::MalformedTokenError,
13
- 'too_many_requests' => EasyMeli::TooManyRequestsError,
14
- 'unknown_error' => EasyMeli::UnknownError
15
- }
16
-
17
8
  base_uri API_ROOT_URL
18
9
  headers EasyMeli::DEFAULT_HEADERS
19
10
  format :json
@@ -49,25 +40,26 @@ class EasyMeli::ApiClient
49
40
 
50
41
  self.class.send(verb, path, params.merge(query)).tap do |response|
51
42
  logger&.log response
43
+ check_status(response)
52
44
  check_for_errors(response)
53
45
  end
54
46
  end
55
47
 
56
- def check_for_errors(response)
57
- return unless response.parsed_response.is_a?(Hash) && !response.body.nil?
58
-
59
- exception = error_class(response)
48
+ def check_status(response)
49
+ exception = EasyMeli::ErrorParser.status_error_class(response)
60
50
 
61
51
  if exception
62
52
  raise exception.new(response)
63
53
  end
64
54
  end
65
55
 
66
- def error_class(body)
67
- ERROR_LIST.find { |key, _| error_message_from_body(body)&.include?(key) }&.last
68
- end
56
+ def check_for_errors(response)
57
+ return unless response.parsed_response.is_a?(Hash) && !response.body.nil?
58
+
59
+ exception = EasyMeli::ErrorParser.error_class(response)
69
60
 
70
- def error_message_from_body(response)
71
- response['message'].to_s.empty? ? response['error'] : response['message']
61
+ if exception
62
+ raise exception.new(response)
63
+ end
72
64
  end
73
65
  end
@@ -54,7 +54,9 @@ class EasyMeli::AuthorizationClient
54
54
  if response.success?
55
55
  response.to_h[EasyMeli::AuthorizationClient::ACCESS_TOKEN_KEY]
56
56
  else
57
- raise EasyMeli::InvalidTokenError.new(response)
57
+ exception = EasyMeli::ErrorParser.error_class(response) || EasyMeli::InvalidTokenError
58
+
59
+ raise exception.new(response)
58
60
  end
59
61
  end
60
62
 
@@ -0,0 +1,38 @@
1
+ class EasyMeli::ErrorParser
2
+ ERROR_LIST = {
3
+ 'Error validating grant' => EasyMeli::InvalidGrantError,
4
+ 'The User ID must match the consultant\'s' => EasyMeli::ForbiddenError,
5
+ 'invalid_token' => EasyMeli::InvalidTokenError,
6
+ 'Malformed access_token' => EasyMeli::MalformedTokenError,
7
+ 'too_many_requests' => EasyMeli::TooManyRequestsError,
8
+ 'unknown_error' => EasyMeli::UnknownError
9
+ }
10
+
11
+ STATUS_ERRORS = {
12
+ 500 => EasyMeli::InternalServerError,
13
+ 501 => EasyMeli::NotImplementedError,
14
+ 502 => EasyMeli::BadGatewayError,
15
+ 503 => EasyMeli::ServiceUnavailableError,
16
+ 504 => EasyMeli::GatewayTimeoutError
17
+ }
18
+
19
+ def self.error_class(response)
20
+ ERROR_LIST.find { |key, _| self.error_message_from_body(response)&.include?(key) }&.last
21
+ end
22
+
23
+ def self.status_error_class(response)
24
+ return unless self.server_side_error?(response)
25
+
26
+ STATUS_ERRORS[response.code] || EasyMeli::ServerError
27
+ end
28
+
29
+ private
30
+
31
+ def self.error_message_from_body(response)
32
+ response['message'].to_s.empty? ? response['error'] : response['message']
33
+ end
34
+
35
+ def self.server_side_error?(response)
36
+ response.code >= 500
37
+ end
38
+ end
@@ -73,4 +73,52 @@ module EasyMeli
73
73
  'Unknown Error'
74
74
  end
75
75
  end
76
+
77
+ class ServerError < Error
78
+ private
79
+
80
+ def local_message
81
+ 'Server Error'
82
+ end
83
+ end
84
+
85
+ class InternalServerError < ServerError
86
+ private
87
+
88
+ def local_message
89
+ 'Internal Server Error'
90
+ end
91
+ end
92
+
93
+ class NotImplementedError < ServerError
94
+ private
95
+
96
+ def local_message
97
+ 'Not Implemented'
98
+ end
99
+ end
100
+
101
+ class BadGatewayError < ServerError
102
+ private
103
+
104
+ def local_message
105
+ 'Bad Gateway'
106
+ end
107
+ end
108
+
109
+ class ServiceUnavailableError < ServerError
110
+ private
111
+
112
+ def local_message
113
+ 'Service Unavailable'
114
+ end
115
+ end
116
+
117
+ class GatewayTimeoutError < ServerError
118
+ private
119
+
120
+ def local_message
121
+ 'Gateway Timeout'
122
+ end
123
+ end
76
124
  end
@@ -1,3 +1,3 @@
1
1
  module EasyMeli
2
- VERSION = "0.6.4"
2
+ VERSION = "0.6.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_meli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Northam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-29 00:00:00.000000000 Z
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -130,6 +130,7 @@ files:
130
130
  - lib/easy_meli/authorization_client.rb
131
131
  - lib/easy_meli/configuration.rb
132
132
  - lib/easy_meli/constants.rb
133
+ - lib/easy_meli/error_parser.rb
133
134
  - lib/easy_meli/errors.rb
134
135
  - lib/easy_meli/version.rb
135
136
  homepage: https://github.com/easybroker/easy_meli