easy_meli 0.5.0 → 0.6.4

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: d5a3424202e135a649e069fb9381c26e7e6760d9e387821c3e7f2f0358ec4f58
4
- data.tar.gz: f947be600fd5c1654b7c920c630f1e916edf58a0c8244c5d5a83def1a2fec288
3
+ metadata.gz: bea2b5ce32b7601401b386bbde1fd0e1bdc8bcfe7c2fab55fe9d44d065695b2b
4
+ data.tar.gz: fb22b1e7d147f20d1d4638a66b4ab0eb57a2490e7d5b59725593dab506837438
5
5
  SHA512:
6
- metadata.gz: 0fb2a5a757c53a87aa5fa77a24a203ae65ab58ca7c979bba04a358df8dd78459e01395f36314c399b33beb15fc03d37b4945ff937c5b19b4281190e098d7910e
7
- data.tar.gz: 76a41f9314e945a4ce0846f922d7fb3e9f2d0937a0f74094fc03b52927b105dfa48fb4cc7898f9baff6d850988f8a1d156ef105ebfd54607b498dbc688fd8372
6
+ metadata.gz: 0dae6c7df5a569e55828291fbca8986af2538309b7df51d7be10da6007857dc71d6442797375dd0a68ddf861920a828ddfa77f57c38095249c60ce34ca789622
7
+ data.tar.gz: 1fa152bcb974cc631d2b5e0d990ed37cb610b4ef253966a10f948a86cdca66ceb421b87e569c9f3af860d4ebe2fead62d7e9034a2f978671a40e16e829aaf56a
@@ -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
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## V 0.6.4
4
+ - Add Unknown error support
5
+
6
+ ## V 0.6.3
7
+ - Fix error when the api response doesn't include error or message keys.
8
+ - Add right error message for forbidden error.
9
+
10
+ ## V 0.6.2
11
+ - Update Error classification mechanism to use error message as first option
12
+
13
+ ## V 0.6.1
14
+ - Fix a bug in the error raising in the authorization client introduced in V 0.6.0
15
+
16
+ ## V 0.6.0
17
+ - Classify an `invalid_token` response as an `InvalidTokenError`.
18
+ - `Malformed access_token` error changed from `AuthenticationError` to `InvalidTokenError`.
19
+
3
20
  ## V 0.5.0
4
21
  - `self.api_client` Prevent access_token override when initialize together with a refresh_token.
5
22
  - Raises a EasyMeli::TooManyRequestsError if a 429 response status is returned.
data/README.md CHANGED
@@ -42,16 +42,16 @@ response = EasyMeli.create_token('the_code_in_the_redirect', 'the_same_redirect_
42
42
  ```
43
43
  This will return a response object with a json body that you can easily access via `response.to_h`.
44
44
 
45
- If you want to refresh the token call
45
+ If you want to refresh the token call
46
46
 
47
47
  ```ruby
48
- access_token = EasyMeli.refresh_token('a_refresh_token')
48
+ access_token = EasyMeli.access_token('a_refresh_token')
49
49
  ```
50
50
 
51
51
  Once you can have an access token you can create a client and call the supported http verb methods.
52
52
 
53
53
  ```ruby
54
- client = EasyMeli.api_client(refresh_token: refresh_token)
54
+ client = EasyMeli.api_client(access_token: access_token)
55
55
 
56
56
  client.get(path, query: { a: 1 })
57
57
  client.post(path, query: { a: 1 }, body: { b: 1 })
@@ -5,17 +5,16 @@ class EasyMeli::ApiClient
5
5
 
6
6
  API_ROOT_URL = 'https://api.mercadolibre.com'
7
7
 
8
- TOKEN_ERRORS = {
9
- 'invalid_grant' => 'Invalid Grant',
10
- 'forbidden' => 'Forbidden',
11
- 'Malformed access_token' => 'Malformed access token'
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
12
15
  }
13
16
 
14
- STATUS_ERRORS = {
15
- 429 => EasyMeli::TooManyRequestsError
16
- }
17
-
18
- base_uri API_ROOT_URL
17
+ base_uri API_ROOT_URL
19
18
  headers EasyMeli::DEFAULT_HEADERS
20
19
  format :json
21
20
 
@@ -50,29 +49,25 @@ class EasyMeli::ApiClient
50
49
 
51
50
  self.class.send(verb, path, params.merge(query)).tap do |response|
52
51
  logger&.log response
53
- check_authentication(response)
54
- check_status(response)
52
+ check_for_errors(response)
55
53
  end
56
54
  end
57
55
 
58
- def check_authentication(response)
59
- response_message = error_message_from_body(response.to_h) if response.parsed_response.is_a? Hash
60
- return if response_message.to_s.empty?
56
+ def check_for_errors(response)
57
+ return unless response.parsed_response.is_a?(Hash) && !response.body.nil?
58
+
59
+ exception = error_class(response)
61
60
 
62
- TOKEN_ERRORS.keys.each do |key|
63
- if response_message.include?(key)
64
- raise EasyMeli::AuthenticationError.new(TOKEN_ERRORS[key], response)
65
- end
61
+ if exception
62
+ raise exception.new(response)
66
63
  end
67
64
  end
68
65
 
69
- def error_message_from_body(body)
70
- return if body.nil?
71
- body['error'].to_s.empty? ? body['message'] : body['error']
66
+ def error_class(body)
67
+ ERROR_LIST.find { |key, _| error_message_from_body(body)&.include?(key) }&.last
72
68
  end
73
69
 
74
- def check_status(response)
75
- status_error = STATUS_ERRORS[response.code]
76
- raise status_error.new(response) if status_error
70
+ def error_message_from_body(response)
71
+ response['message'].to_s.empty? ? response['error'] : response['message']
77
72
  end
78
73
  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,7 @@ 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
+ raise EasyMeli::InvalidTokenError.new(response)
58
58
  end
59
59
  end
60
60
 
@@ -2,17 +2,75 @@ module EasyMeli
2
2
  class Error < StandardError
3
3
  attr_reader :response
4
4
 
5
- def initialize(message, response)
5
+ def initialize(response)
6
6
  @response = response
7
- super(message)
7
+ super(local_message)
8
+ end
9
+
10
+ private
11
+
12
+ def local_message
13
+ raise NotImplementedError
8
14
  end
9
15
  end
10
16
 
11
17
  class AuthenticationError < Error; end
12
-
18
+
19
+ class CreateTokenError < AuthenticationError
20
+ private
21
+
22
+ def local_message
23
+ 'Error Creating Token'
24
+ end
25
+ end
26
+
27
+ class InvalidGrantError < AuthenticationError
28
+ private
29
+
30
+ def local_message
31
+ 'Invalid Grant'
32
+ end
33
+ end
34
+
35
+ class ForbiddenError < AuthenticationError
36
+ private
37
+
38
+ def local_message
39
+ 'Forbidden'
40
+ end
41
+ end
42
+
43
+ class AccessTokenError < Error; end
44
+
45
+ class InvalidTokenError < AccessTokenError
46
+ private
47
+
48
+ def local_message
49
+ 'Invalid Token'
50
+ end
51
+ end
52
+
53
+ class MalformedTokenError < AccessTokenError
54
+ private
55
+
56
+ def local_message
57
+ 'Malformed access token'
58
+ end
59
+ end
60
+
13
61
  class TooManyRequestsError < Error
14
- def initialize(response)
15
- super('Too many requests', response)
62
+ private
63
+
64
+ def local_message
65
+ 'Too many requests'
66
+ end
67
+ end
68
+
69
+ class UnknownError < Error
70
+ private
71
+
72
+ def local_message
73
+ 'Unknown Error'
16
74
  end
17
75
  end
18
- end
76
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyMeli
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.4"
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.5.0
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Northam
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-18 00:00:00.000000000 Z
11
+ date: 2021-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -108,13 +108,14 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- description:
111
+ description:
112
112
  email:
113
113
  - eric@northam.us
114
114
  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
@@ -136,7 +137,7 @@ licenses:
136
137
  - MIT
137
138
  metadata:
138
139
  homepage_uri: https://github.com/easybroker/easy_meli
139
- post_install_message:
140
+ post_install_message:
140
141
  rdoc_options: []
141
142
  require_paths:
142
143
  - lib
@@ -151,8 +152,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
152
  - !ruby/object:Gem::Version
152
153
  version: '0'
153
154
  requirements: []
154
- rubygems_version: 3.0.3
155
- signing_key:
155
+ rubygems_version: 3.1.4
156
+ signing_key:
156
157
  specification_version: 4
157
158
  summary: A simple gem to work with MercadoLibre's API
158
159
  test_files: []