easy_meli 0.6.2 → 0.6.7

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: bc240e7ae7a827cff6460162be1a9460f5e860676f3800a50d18ae3558f2976e
4
- data.tar.gz: 714503d3e3886b1d491d1ea579d72f1d74e2408d476b9ada5ee6446f0efbb467
3
+ metadata.gz: b1338e416eb7581a2e568c8621f77789507d271bd0212936fa6c8d8ee7292025
4
+ data.tar.gz: fb5936ef060651fe095269d173dcda3a25ae97ae84d6bfc1386bec73d1d055d0
5
5
  SHA512:
6
- metadata.gz: 7004c03b3ec4be44170bb4ea92c5ac2595ddd5d244a7e680a88e67d5bdc9eec0197ae7a7bd0970ac78d0912e42f04b9b44a65ee907e5333db479f569753853b2
7
- data.tar.gz: 139f426a07c7782b73ea52959283b70e068f8565d9c5e646790cd7f3373b51eed3f3e179d418a72ddf746012c13f4ea735a133c023b59da4c6d8d24aed38ab91
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(refresh_token: refresh_token, logger: my_logger)
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
- api_client = EasyMeli.api_client(refresh_token: refresh_token)
77
- response = api_client.get('/users/me')
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
@@ -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,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
- query[:access_token] = access_token if access_token
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 check_for_errors(response)
56
- return unless response.parsed_response.is_a? Hash
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 error_class(body)
66
- ERROR_LIST.find { |key, _| error_message_from_body(body).include?(key) }&.last
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 error_message_from_body(response)
70
- return if response.body.nil?
65
+ def authorization_header
66
+ return {} unless access_token
71
67
 
72
- response['message'].to_s.empty? ? response['error'] : response['message']
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
- 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.2"
2
+ VERSION = "0.6.7"
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.2
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-01-28 00:00:00.000000000 Z
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