easy_meli 0.6.2 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +3 -3
- data/lib/easy_meli.rb +1 -0
- data/lib/easy_meli/api_client.rb +15 -19
- data/lib/easy_meli/authorization_client.rb +3 -1
- data/lib/easy_meli/error_parser.rb +43 -0
- data/lib/easy_meli/errors.rb +56 -0
- data/lib/easy_meli/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1338e416eb7581a2e568c8621f77789507d271bd0212936fa6c8d8ee7292025
|
4
|
+
data.tar.gz: fb5936ef060651fe095269d173dcda3a25ae97ae84d6bfc1386bec73d1d055d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bf7cddd43e511f2b55bed1f68c5905e0362821a7c825bb1a8f5ce7d6dd3dc4892e5dc9fb1d8499a42d17cc30a3a117194ace8cc734899a8cbeef7d58ac974ed
|
7
|
+
data.tar.gz: a56c55e73b131f1f36bef43a3db22f5c68c92cd611cc2f448a7e9486a2d302c5489669e8aa87f19755f7ceb3858c502ea11fca97922444321390606b2d31b55f
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## V 0.6.7
|
4
|
+
- Send `access_token` in the Authorization Header instead of the query params [See Official Documentation](https://developers.mercadolibre.com.ar/es_ar/desarrollo-seguro#header)
|
5
|
+
|
6
|
+
## V 0.6.6
|
7
|
+
- Classify status code 401 as EasyMeli::InvalidTokenError.
|
8
|
+
|
9
|
+
## V 0.6.5
|
10
|
+
- Move Error class search to its own class and reuse it for ApiClient and AuthorizationClient
|
11
|
+
- Raise EasyMeli exceptions for server side errors.
|
12
|
+
|
13
|
+
## V 0.6.4
|
14
|
+
- Add Unknown error support
|
15
|
+
|
16
|
+
## V 0.6.3
|
17
|
+
- Fix error when the api response doesn't include error or message keys.
|
18
|
+
- Add right error message for forbidden error.
|
19
|
+
|
3
20
|
## V 0.6.2
|
4
21
|
- Update Error classification mechanism to use error message as first option
|
5
22
|
|
data/README.md
CHANGED
@@ -63,7 +63,7 @@ You can also pass a logger to any methods that make remote calls. The logger cla
|
|
63
63
|
|
64
64
|
```ruby
|
65
65
|
EasyMeli.create_token('the_code_in_the_redirect', 'the_same_redirect_url_as_above', logger: my_logger)
|
66
|
-
EasyMeli.api_client(
|
66
|
+
EasyMeli.api_client(access_token: access_token, logger: my_logger)
|
67
67
|
```
|
68
68
|
|
69
69
|
### Example of how to retrieve a user profile
|
@@ -73,8 +73,8 @@ EasyMeli.configure do |config|
|
|
73
73
|
config.secret_key = "your_secret_key"
|
74
74
|
end
|
75
75
|
|
76
|
-
|
77
|
-
response =
|
76
|
+
client = EasyMeli.api_client(access_token: access_token)
|
77
|
+
response = client.get('/users/me')
|
78
78
|
|
79
79
|
```
|
80
80
|
|
data/lib/easy_meli.rb
CHANGED
data/lib/easy_meli/api_client.rb
CHANGED
@@ -5,16 +5,7 @@ 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
|
-
'forbidden' => EasyMeli::ForbiddenError,
|
11
|
-
'invalid_token' => EasyMeli::InvalidTokenError,
|
12
|
-
'Malformed access_token' => EasyMeli::MalformedTokenError,
|
13
|
-
'too_many_requests' => EasyMeli::TooManyRequestsError
|
14
|
-
}
|
15
|
-
|
16
8
|
base_uri API_ROOT_URL
|
17
|
-
headers EasyMeli::DEFAULT_HEADERS
|
18
9
|
format :json
|
19
10
|
|
20
11
|
attr_reader :logger, :access_token
|
@@ -44,31 +35,36 @@ class EasyMeli::ApiClient
|
|
44
35
|
|
45
36
|
def send_request(verb, path = '', params = {})
|
46
37
|
query = params[:query] || params['query'] || {}
|
47
|
-
|
38
|
+
params[:headers] = EasyMeli::DEFAULT_HEADERS.merge(authorization_header)
|
48
39
|
|
49
40
|
self.class.send(verb, path, params.merge(query)).tap do |response|
|
50
41
|
logger&.log response
|
42
|
+
check_status(response)
|
51
43
|
check_for_errors(response)
|
52
44
|
end
|
53
45
|
end
|
54
46
|
|
55
|
-
def
|
56
|
-
|
57
|
-
|
58
|
-
exception = error_class(response)
|
47
|
+
def check_status(response)
|
48
|
+
exception = EasyMeli::ErrorParser.status_error_class(response)
|
59
49
|
|
60
50
|
if exception
|
61
51
|
raise exception.new(response)
|
62
52
|
end
|
63
53
|
end
|
64
54
|
|
65
|
-
def
|
66
|
-
|
55
|
+
def check_for_errors(response)
|
56
|
+
return unless response.parsed_response.is_a?(Hash) && !response.body.nil?
|
57
|
+
|
58
|
+
exception = EasyMeli::ErrorParser.error_class(response)
|
59
|
+
|
60
|
+
if exception
|
61
|
+
raise exception.new(response)
|
62
|
+
end
|
67
63
|
end
|
68
64
|
|
69
|
-
def
|
70
|
-
return
|
65
|
+
def authorization_header
|
66
|
+
return {} unless access_token
|
71
67
|
|
72
|
-
|
68
|
+
{ Authorization: "Bearer #{access_token}" }
|
73
69
|
end
|
74
70
|
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
|
-
|
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
|
data/lib/easy_meli/errors.rb
CHANGED
@@ -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
|
data/lib/easy_meli/version.rb
CHANGED
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
|
+
version: 0.6.7
|
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-
|
11
|
+
date: 2021-03-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
|