easy_meli 0.4.0 → 0.6.3

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: ee793dc5802586adac55653bb44e8183f2903583fe2463e57d3f6ba6f8f87810
4
- data.tar.gz: bddc2f0e96a9d2080088af552aba50cdf02530d869d92837ddb8d1339a5450b0
3
+ metadata.gz: 55e9bca011c82596149caa2a4bbddbe3350aa239936c1687406172531d7ec62c
4
+ data.tar.gz: 25b16a878e1c1fb9a8f8d6e5cbe1df2171b69d7b41912e72bb45133a9cdfe8f0
5
5
  SHA512:
6
- metadata.gz: 301620aaaabac2777da9d129f043aca962af57c2e6c93138a4f75ed741a4ff05a977971e94352f611c80107a485f0bf0e6935f7ce2732a2f4470bfa950ad6606
7
- data.tar.gz: 4ef357f6574ca2883befdcfac864397cdc2c402adcc6bc81e5e3fde337e36254b48553b5e882f99011238027061611ba2be9a0d8c0af8aef400c2a6ede17eff0
6
+ metadata.gz: 82a0a0178335a821c1113f775fffef25fb4f81eb8ae992f44308dd0f0d7d51fac92db23d56445e99c634dd17f86a959baeacd6237f91892358c4bf8511b9ae9d
7
+ data.tar.gz: 18da80bf6afefcb08394578f2a7e8c95220640c52b13bd2ff605648a90650d1376ba50f044b83f2a60089afbbaae3828ead52cadb0d42a6f013cc7ee3bddb036
@@ -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
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ ## V 0.6.3
4
+ - Fix error when the api response doesn't include error or message keys.
5
+ - Add right error message for forbidden error.
6
+
7
+ ## V 0.6.2
8
+ - Update Error classification mechanism to use error message as first option
9
+
10
+ ## V 0.6.1
11
+ - Fix a bug in the error raising in the authorization client introduced in V 0.6.0
12
+
13
+ ## V 0.6.0
14
+ - Classify an `invalid_token` response as an `InvalidTokenError`.
15
+ - `Malformed access_token` error changed from `AuthenticationError` to `InvalidTokenError`.
16
+
17
+ ## V 0.5.0
18
+ - `self.api_client` Prevent access_token override when initialize together with a refresh_token.
19
+ - 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 })
@@ -25,12 +25,12 @@ module EasyMeli
25
25
  EasyMeli::AuthorizationClient.create_token(code, redirect_uri, logger: logger)
26
26
  end
27
27
 
28
- def self.refresh_token(refresh_token, logger: nil)
29
- EasyMeli::AuthorizationClient.refresh_token(refresh_token, logger: logger)
28
+ def self.access_token(refresh_token, logger: nil)
29
+ EasyMeli::AuthorizationClient.access_token(refresh_token, logger: logger)
30
30
  end
31
31
 
32
32
  def self.api_client(access_token: nil, refresh_token: nil, logger: nil)
33
- access_token = self.refresh_token(refresh_token, logger: logger) if refresh_token
33
+ access_token ||= self.access_token(refresh_token, logger: logger) if refresh_token
34
34
  EasyMeli::ApiClient.new(access_token, logger: logger)
35
35
  end
36
36
  end
@@ -5,13 +5,15 @@ 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
12
14
  }
13
15
 
14
- base_uri API_ROOT_URL
16
+ base_uri API_ROOT_URL
15
17
  headers EasyMeli::DEFAULT_HEADERS
16
18
  format :json
17
19
 
@@ -46,23 +48,25 @@ class EasyMeli::ApiClient
46
48
 
47
49
  self.class.send(verb, path, params.merge(query)).tap do |response|
48
50
  logger&.log response
49
- check_authentication(response)
51
+ check_for_errors(response)
50
52
  end
51
53
  end
52
54
 
53
- def check_authentication(response)
54
- response_message = error_message_from_body(response.to_h) if response.parsed_response.is_a? Hash
55
- return if response_message.to_s.empty?
55
+ def check_for_errors(response)
56
+ return unless response.parsed_response.is_a?(Hash) && !response.body.nil?
56
57
 
57
- TOKEN_ERRORS.keys.each do |key|
58
- if response_message.include?(key)
59
- raise EasyMeli::AuthenticationError.new(TOKEN_ERRORS[key], response)
60
- end
58
+ exception = error_class(response)
59
+
60
+ if exception
61
+ raise exception.new(response)
61
62
  end
62
63
  end
63
64
 
64
- def error_message_from_body(body)
65
- return if body.nil?
66
- body['error'].to_s.empty? ? body['message'] : body['error']
65
+ def error_class(body)
66
+ ERROR_LIST.find { |key, _| error_message_from_body(body)&.include?(key) }&.last
67
+ end
68
+
69
+ def error_message_from_body(response)
70
+ response['message'].to_s.empty? ? response['error'] : response['message']
67
71
  end
68
72
  end
@@ -35,7 +35,7 @@ class EasyMeli::AuthorizationClient
35
35
  params = {
36
36
  client_id: EasyMeli.configuration.application_id,
37
37
  response_type: 'code',
38
- redirect_uri: redirect_uri
38
+ redirect_uri: redirect_uri
39
39
  }
40
40
  HTTParty::Request.new(:get, country_auth_url(country_code), query: params).uri.to_s
41
41
  end
@@ -45,16 +45,16 @@ 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
 
52
- def self.refresh_token(refresh_token, logger: nil)
53
- response = self.new(logger: logger).refresh_token_with_response(refresh_token)
52
+ def self.access_token(refresh_token, logger: nil)
53
+ response = self.new(logger: logger).access_token_with_response(refresh_token)
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
 
@@ -67,7 +67,7 @@ class EasyMeli::AuthorizationClient
67
67
  post_auth(query_params)
68
68
  end
69
69
 
70
- def refresh_token_with_response(refresh_token)
70
+ def access_token_with_response(refresh_token)
71
71
  query_params = merge_auth_params(
72
72
  grant_type: 'refresh_token',
73
73
  refresh_token: refresh_token
@@ -84,7 +84,7 @@ class EasyMeli::AuthorizationClient
84
84
  end
85
85
 
86
86
  def self.country_auth_url(country_code)
87
- url = BASE_AUTH_URLS[country_code.to_s.upcase.to_sym] ||
87
+ url = BASE_AUTH_URLS[country_code.to_s.upcase.to_sym] ||
88
88
  (raise ArgumentError.new('%s is an invalid country code' % country_code))
89
89
  [url, AUTH_PATH].join
90
90
  end
@@ -95,4 +95,4 @@ class EasyMeli::AuthorizationClient
95
95
  client_secret: EasyMeli.configuration.secret_key
96
96
  )
97
97
  end
98
- end
98
+ end
@@ -2,11 +2,67 @@ 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
- end
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
+
61
+ class TooManyRequestsError < Error
62
+ private
63
+
64
+ def local_message
65
+ 'Too many requests'
66
+ end
67
+ end
68
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyMeli
2
- VERSION = "0.4.0"
2
+ VERSION = "0.6.3"
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.4.0
4
+ version: 0.6.3
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: 2020-05-19 00:00:00.000000000 Z
11
+ date: 2021-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -108,14 +108,16 @@ 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"
120
+ - CHANGELOG.md
119
121
  - Gemfile
120
122
  - LICENSE.txt
121
123
  - README.md
@@ -135,7 +137,7 @@ licenses:
135
137
  - MIT
136
138
  metadata:
137
139
  homepage_uri: https://github.com/easybroker/easy_meli
138
- post_install_message:
140
+ post_install_message:
139
141
  rdoc_options: []
140
142
  require_paths:
141
143
  - lib
@@ -150,8 +152,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
152
  - !ruby/object:Gem::Version
151
153
  version: '0'
152
154
  requirements: []
153
- rubygems_version: 3.0.3
154
- signing_key:
155
+ rubygems_version: 3.1.4
156
+ signing_key:
155
157
  specification_version: 4
156
158
  summary: A simple gem to work with MercadoLibre's API
157
159
  test_files: []