easy_meli 0.6.0 → 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: 9e8de8ae979d9da175327ba9238ceabac72a6276f0e51a9d6a99fb35040f50f8
4
- data.tar.gz: 613fa5048c19460f7b209552ea9d314e222f43092b9780a6f09eff6a10b07314
3
+ metadata.gz: 269c22ea4f450792d02935692db9eec8adc207fc8f44c946f1d3f439cce435b1
4
+ data.tar.gz: 00565d53e5a9e3b40235bce3723ab962d7e3f0a8fb912713330255de90b7980c
5
5
  SHA512:
6
- metadata.gz: b19390ec2c0d7f62de4d2a7b57647d565c82047a3ac094c27b1ae136128d8efaceb442bd23fa22d281003969c7ace0753c844c2d78b0cc042024d36669c1abda
7
- data.tar.gz: 57325bba54d582992def8dd90ddaff390588b8cd400bdbaa3299803be7639f4e1520c08e5a1d226f0266decb54812650b096b39ebfa4a43908263521ac68ec92
6
+ metadata.gz: 7f3e5971e80a8a9b73b435186fad527d93e71f0d454208ecf78a412c5716726f28ab0fbc25cca60dc6701422adf089d05ae504a86d3b34efb918ef4ba1685223
7
+ data.tar.gz: 6f1e0cb4af4919bb9c15fbd15526787c1f13727b870d5b2345a36d67f5e790bd9602eb39fde70dcb137d62856eda5e789c2c3629962fb0c081d5ddc09d4fb738
@@ -0,0 +1,32 @@
1
+ name: CI
2
+ on:
3
+ pull_request:
4
+ branches: [ master ]
5
+ push:
6
+ branches: [ master ]
7
+ jobs:
8
+ test:
9
+ runs-on: ubuntu-latest
10
+ strategy:
11
+ matrix:
12
+ ruby_version: [ '2.6' ]
13
+ steps:
14
+ - name: Checkout
15
+ uses: actions/checkout@v2
16
+ - name: Setup ruby ${{ matrix.ruby_version }}
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby_version }}
20
+ - name: Setup cache key and directory for gems cache
21
+ uses: actions/cache@v1
22
+ with:
23
+ path: vendor/bundle
24
+ key: ${{ runner.os }}-gem-use-ruby-${{ matrix.ruby_version }}-${{ hashFiles('**/Gemfile.lock') }}
25
+ - name: Bundle install
26
+ run: |
27
+ gem install bundler:1.17.0
28
+ bundle config path vendor/bundle
29
+ bundle install --jobs 4 --retry 3
30
+ - name: Run tests
31
+ run: |
32
+ bundle exec rake test
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
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
+
7
+ ## V 0.6.4
8
+ - Add Unknown error support
9
+
10
+ ## V 0.6.3
11
+ - Fix error when the api response doesn't include error or message keys.
12
+ - Add right error message for forbidden error.
13
+
14
+ ## V 0.6.2
15
+ - Update Error classification mechanism to use error message as first option
16
+
17
+ ## V 0.6.1
18
+ - Fix a bug in the error raising in the authorization client introduced in V 0.6.0
19
+
3
20
  ## V 0.6.0
4
21
  - Classify an `invalid_token` response as an `InvalidTokenError`.
5
22
  - `Malformed access_token` error changed from `AuthenticationError` to `InvalidTokenError`.
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
@@ -45,7 +45,7 @@ class EasyMeli::AuthorizationClient
45
45
  if response.success?
46
46
  response.to_h
47
47
  else
48
- raise EasyMeli::AuthenticationError.new('Error Creating Token', response)
48
+ raise EasyMeli::CreateTokenError.new(response)
49
49
  end
50
50
  end
51
51
 
@@ -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::AuthenticationError.new('Error Refreshing Token', 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
@@ -16,6 +16,14 @@ module EasyMeli
16
16
 
17
17
  class AuthenticationError < Error; end
18
18
 
19
+ class CreateTokenError < AuthenticationError
20
+ private
21
+
22
+ def local_message
23
+ 'Error Creating Token'
24
+ end
25
+ end
26
+
19
27
  class InvalidGrantError < AuthenticationError
20
28
  private
21
29
 
@@ -57,4 +65,60 @@ module EasyMeli
57
65
  'Too many requests'
58
66
  end
59
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
60
124
  end
@@ -1,3 +1,3 @@
1
1
  module EasyMeli
2
- VERSION = "0.6.0"
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.0
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-27 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
@@ -115,6 +115,7 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - ".github/workflows/ci.yml"
118
119
  - ".gitignore"
119
120
  - CHANGELOG.md
120
121
  - Gemfile
@@ -129,6 +130,7 @@ files:
129
130
  - lib/easy_meli/authorization_client.rb
130
131
  - lib/easy_meli/configuration.rb
131
132
  - lib/easy_meli/constants.rb
133
+ - lib/easy_meli/error_parser.rb
132
134
  - lib/easy_meli/errors.rb
133
135
  - lib/easy_meli/version.rb
134
136
  homepage: https://github.com/easybroker/easy_meli