easy_meli 0.6.1 → 0.6.6

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
  SHA256:
3
- metadata.gz: 8c8dc1140d9368b076baf0a49a9742aea8de95565a90512152c9c58ec2b98723
4
- data.tar.gz: 543cbed2c4789b0259b62f11d332a02c3b5b281feb2c5b60c5f87fd8640c0754
3
+ metadata.gz: 67691d59c76a0324aa03dfc6e9f67c86954be2d93a49836ce33cb52bed270bed
4
+ data.tar.gz: 6f4740a8bbce9b8cc55aaccdd55b53e46bb19cd89b44f8fdaa2dfd0204d29274
5
5
  SHA512:
6
- metadata.gz: c2a7ecfa32beed3a9329b01312b1cfd4ed70fceb26469dc90e8d0d2c3b42186a9c1ec06f115f1dc5fa15236aa48d99b7e074a089fad7f710bddaf2bcb324680a
7
- data.tar.gz: dfc01a8544c0f2d23711888fdfa3fb6a6f6f68196782d41cb501c1f50d53900882fb5c1b8d7a9f88e610eb1fafc8112447943ff87b8793af7fccd29e800a6d17
6
+ metadata.gz: e37596a5954f789afc52ab92fe58c9b0bb0cfd32cdb6662b96305362081c56dba4a7f346a48989fa6da1487c5e84332c4382dcb9ab78278fa30f82b9c50541f0
7
+ data.tar.gz: 4d61945cbb1389ba00ae6c47829769c45244c3ffffb23147e75f34c245d2eee95aff60b150ef1e7e8929315fdea9b2c8b7ce97278b10d024c1ddef1d4f29cc8a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## V 0.6.6
4
+ - Classify status code 401 as EasyMeli::InvalidTokenError.
5
+
6
+ ## V 0.6.5
7
+ - Move Error class search to its own class and reuse it for ApiClient and AuthorizationClient
8
+ - Raise EasyMeli exceptions for server side errors.
9
+
10
+ ## V 0.6.4
11
+ - Add Unknown error support
12
+
13
+ ## V 0.6.3
14
+ - Fix error when the api response doesn't include error or message keys.
15
+ - Add right error message for forbidden error.
16
+
17
+ ## V 0.6.2
18
+ - Update Error classification mechanism to use error message as first option
19
+
3
20
  ## V 0.6.1
4
21
  - Fix a bug in the error raising in the authorization client introduced in V 0.6.0
5
22
 
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,17 +5,6 @@ class EasyMeli::ApiClient
5
5
 
6
6
  API_ROOT_URL = 'https://api.mercadolibre.com'
7
7
 
8
- ERROR_LIST = {
9
- 'invalid_grant' => EasyMeli::InvalidGrantError,
10
- 'forbidden' => EasyMeli::ForbiddenError,
11
- 'invalid_token' => EasyMeli::InvalidTokenError,
12
- 'Malformed access_token' => EasyMeli::MalformedTokenError
13
- }
14
-
15
- STATUS_ERRORS = {
16
- 429 => EasyMeli::TooManyRequestsError
17
- }
18
-
19
8
  base_uri API_ROOT_URL
20
9
  headers EasyMeli::DEFAULT_HEADERS
21
10
  format :json
@@ -51,30 +40,26 @@ class EasyMeli::ApiClient
51
40
 
52
41
  self.class.send(verb, path, params.merge(query)).tap do |response|
53
42
  logger&.log response
54
- check_authentication(response)
55
43
  check_status(response)
44
+ check_for_errors(response)
56
45
  end
57
46
  end
58
47
 
59
- def check_authentication(response)
60
- response_message = error_message_from_body(response.to_h) if response.parsed_response.is_a? Hash
61
- return if response_message.to_s.empty?
48
+ def check_status(response)
49
+ exception = EasyMeli::ErrorParser.status_error_class(response)
62
50
 
63
- ERROR_LIST.keys.each do |key|
64
- if response_message.include?(key)
65
- exception = ERROR_LIST[key]
66
- raise exception.new(response)
67
- end
51
+ if exception
52
+ raise exception.new(response)
68
53
  end
69
54
  end
70
55
 
71
- def error_message_from_body(body)
72
- return if body.nil?
73
- body['error'].to_s.empty? ? body['message'] : body['error']
74
- end
56
+ def check_for_errors(response)
57
+ return unless response.parsed_response.is_a?(Hash) && !response.body.nil?
75
58
 
76
- def check_status(response)
77
- status_error = STATUS_ERRORS[response.code]
78
- raise status_error.new(response) if status_error
59
+ exception = EasyMeli::ErrorParser.error_class(response)
60
+
61
+ if exception
62
+ raise exception.new(response)
63
+ end
79
64
  end
80
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,43 @@
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
+ 401 => EasyMeli::InvalidTokenError,
13
+ 500 => EasyMeli::InternalServerError,
14
+ 501 => EasyMeli::NotImplementedError,
15
+ 502 => EasyMeli::BadGatewayError,
16
+ 503 => EasyMeli::ServiceUnavailableError,
17
+ 504 => EasyMeli::GatewayTimeoutError
18
+ }
19
+
20
+ def self.error_class(response)
21
+ ERROR_LIST.find { |key, _| self.error_message_from_body(response)&.include?(key) }&.last
22
+ end
23
+
24
+ def self.status_error_class(response)
25
+ return unless self.status_code_listed?(response) || self.server_side_error?(response)
26
+
27
+ STATUS_ERRORS[response.code] || EasyMeli::ServerError
28
+ end
29
+
30
+ private
31
+
32
+ def self.error_message_from_body(response)
33
+ response['message'].to_s.empty? ? response['error'] : response['message']
34
+ end
35
+
36
+ def self.server_side_error?(response)
37
+ response.code >= 500
38
+ end
39
+
40
+ def self.status_code_listed?(response)
41
+ STATUS_ERRORS.keys.include?(response.code)
42
+ end
43
+ end
@@ -65,4 +65,60 @@ module EasyMeli
65
65
  'Too many requests'
66
66
  end
67
67
  end
68
+
69
+ class UnknownError < Error
70
+ private
71
+
72
+ def local_message
73
+ 'Unknown Error'
74
+ end
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
68
124
  end
@@ -1,3 +1,3 @@
1
1
  module EasyMeli
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.6"
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.1
4
+ version: 0.6.6
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-27 00:00:00.000000000 Z
11
+ date: 2021-02-26 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