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 +4 -4
- data/.github/workflows/ci.yml +32 -0
- data/CHANGELOG.md +19 -0
- data/README.md +3 -3
- data/lib/easy_meli.rb +3 -3
- data/lib/easy_meli/api_client.rb +20 -16
- data/lib/easy_meli/authorization_client.rb +8 -8
- data/lib/easy_meli/errors.rb +59 -3
- data/lib/easy_meli/version.rb +1 -1
- metadata +9 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55e9bca011c82596149caa2a4bbddbe3350aa239936c1687406172531d7ec62c
|
4
|
+
data.tar.gz: 25b16a878e1c1fb9a8f8d6e5cbe1df2171b69d7b41912e72bb45133a9cdfe8f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/CHANGELOG.md
ADDED
@@ -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.
|
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(
|
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 })
|
data/lib/easy_meli.rb
CHANGED
@@ -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.
|
29
|
-
EasyMeli::AuthorizationClient.
|
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
|
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
|
data/lib/easy_meli/api_client.rb
CHANGED
@@ -5,13 +5,15 @@ class EasyMeli::ApiClient
|
|
5
5
|
|
6
6
|
API_ROOT_URL = 'https://api.mercadolibre.com'
|
7
7
|
|
8
|
-
|
9
|
-
'
|
10
|
-
'
|
11
|
-
'
|
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
|
-
|
51
|
+
check_for_errors(response)
|
50
52
|
end
|
51
53
|
end
|
52
54
|
|
53
|
-
def
|
54
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
exception = error_class(response)
|
59
|
+
|
60
|
+
if exception
|
61
|
+
raise exception.new(response)
|
61
62
|
end
|
62
63
|
end
|
63
64
|
|
64
|
-
def
|
65
|
-
|
66
|
-
|
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::
|
48
|
+
raise EasyMeli::CreateTokenError.new(response)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
def self.
|
53
|
-
response = self.new(logger: logger).
|
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::
|
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
|
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
|
data/lib/easy_meli/errors.rb
CHANGED
@@ -2,11 +2,67 @@ module EasyMeli
|
|
2
2
|
class Error < StandardError
|
3
3
|
attr_reader :response
|
4
4
|
|
5
|
-
def initialize(
|
5
|
+
def initialize(response)
|
6
6
|
@response = response
|
7
|
-
super(
|
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
|
+
|
61
|
+
class TooManyRequestsError < Error
|
62
|
+
private
|
63
|
+
|
64
|
+
def local_message
|
65
|
+
'Too many requests'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
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.
|
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:
|
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.
|
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: []
|