easy_meli 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -13
- data/lib/easy_meli.rb +17 -0
- data/lib/easy_meli/api_client.rb +2 -2
- data/lib/easy_meli/authorization_client.rb +21 -2
- data/lib/easy_meli/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee793dc5802586adac55653bb44e8183f2903583fe2463e57d3f6ba6f8f87810
|
4
|
+
data.tar.gz: bddc2f0e96a9d2080088af552aba50cdf02530d869d92837ddb8d1339a5450b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 301620aaaabac2777da9d129f043aca962af57c2e6c93138a4f75ed741a4ff05a977971e94352f611c80107a485f0bf0e6935f7ce2732a2f4470bfa950ad6606
|
7
|
+
data.tar.gz: 4ef357f6574ca2883befdcfac864397cdc2c402adcc6bc81e5e3fde337e36254b48553b5e882f99011238027061611ba2be9a0d8c0af8aef400c2a6ede17eff0
|
data/README.md
CHANGED
@@ -32,26 +32,26 @@ end
|
|
32
32
|
To get the authorization url that the end-user uses to give your app authorization to access MercadoLibre on your part call the `authorization_url` with the desired country and the url to redirect to in order to complete the authorization.
|
33
33
|
|
34
34
|
```ruby
|
35
|
-
EasyMeli
|
35
|
+
EasyMeli.authorization_url('MX', 'your_redirect_url')
|
36
36
|
```
|
37
37
|
|
38
38
|
Once MercadoLibre calls your redirect url you can get a refresh token by calling
|
39
39
|
|
40
40
|
```ruby
|
41
|
-
response = EasyMeli
|
41
|
+
response = EasyMeli.create_token('the_code_in_the_redirect', 'the_same_redirect_url_as_above')
|
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
45
|
If you want to refresh the token call
|
46
46
|
|
47
47
|
```ruby
|
48
|
-
|
48
|
+
access_token = EasyMeli.refresh_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
|
54
|
+
client = EasyMeli.api_client(refresh_token: refresh_token)
|
55
55
|
|
56
56
|
client.get(path, query: { a: 1 })
|
57
57
|
client.post(path, query: { a: 1 }, body: { b: 1 })
|
@@ -59,25 +59,21 @@ client.put(path, query: { a: 1 }, body: { b: 1 })
|
|
59
59
|
client.delete(path, query: { a: 1 })
|
60
60
|
```
|
61
61
|
|
62
|
-
You can also pass a logger
|
62
|
+
You can also pass a logger to any methods that make remote calls. The logger class must implement a `log` method which will be called with the [HTTParty response](https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/Response) for every remote request sent.
|
63
63
|
|
64
64
|
```ruby
|
65
|
-
EasyMeli
|
66
|
-
EasyMeli
|
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)
|
67
67
|
```
|
68
68
|
|
69
|
-
###
|
69
|
+
### Example of how to retrieve a user profile
|
70
70
|
```ruby
|
71
71
|
EasyMeli.configure do |config|
|
72
72
|
config.application_id = "your_app_id"
|
73
73
|
config.secret_key = "your_secret_key"
|
74
74
|
end
|
75
75
|
|
76
|
-
|
77
|
-
token = authorization_client.refresh_token(previously_stored_refresh_token).to_h['access_token']
|
78
|
-
|
79
|
-
api_client = EasyMeli::ApiClient.new(token)
|
80
|
-
|
76
|
+
api_client = EasyMeli.api_client(refresh_token: refresh_token)
|
81
77
|
response = api_client.get('/users/me')
|
82
78
|
|
83
79
|
```
|
data/lib/easy_meli.rb
CHANGED
@@ -16,4 +16,21 @@ module EasyMeli
|
|
16
16
|
def self.configure
|
17
17
|
yield(configuration)
|
18
18
|
end
|
19
|
+
|
20
|
+
def self.authorization_url(country_code, redirect_uri)
|
21
|
+
EasyMeli::AuthorizationClient.authorization_url(country_code, redirect_uri)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.create_token(code, redirect_uri, logger: nil)
|
25
|
+
EasyMeli::AuthorizationClient.create_token(code, redirect_uri, logger: logger)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.refresh_token(refresh_token, logger: nil)
|
29
|
+
EasyMeli::AuthorizationClient.refresh_token(refresh_token, logger: logger)
|
30
|
+
end
|
31
|
+
|
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
|
34
|
+
EasyMeli::ApiClient.new(access_token, logger: logger)
|
35
|
+
end
|
19
36
|
end
|
data/lib/easy_meli/api_client.rb
CHANGED
@@ -17,7 +17,7 @@ class EasyMeli::ApiClient
|
|
17
17
|
|
18
18
|
attr_reader :logger, :access_token
|
19
19
|
|
20
|
-
def initialize(access_token, logger: nil)
|
20
|
+
def initialize(access_token = nil, logger: nil)
|
21
21
|
@logger = logger
|
22
22
|
@access_token = access_token
|
23
23
|
end
|
@@ -42,7 +42,7 @@ class EasyMeli::ApiClient
|
|
42
42
|
|
43
43
|
def send_request(verb, path = '', params = {})
|
44
44
|
query = params[:query] || params['query'] || {}
|
45
|
-
query[:access_token] = access_token
|
45
|
+
query[:access_token] = access_token if access_token
|
46
46
|
|
47
47
|
self.class.send(verb, path, params.merge(query)).tap do |response|
|
48
48
|
logger&.log response
|
@@ -20,6 +20,7 @@ class EasyMeli::AuthorizationClient
|
|
20
20
|
PT: 'https://auth.mercadolibre.com.pt',
|
21
21
|
DO: 'https://auth.mercadolibre.com.do'
|
22
22
|
}
|
23
|
+
ACCESS_TOKEN_KEY = 'access_token'
|
23
24
|
|
24
25
|
headers EasyMeli::DEFAULT_HEADERS
|
25
26
|
format :json
|
@@ -39,7 +40,25 @@ class EasyMeli::AuthorizationClient
|
|
39
40
|
HTTParty::Request.new(:get, country_auth_url(country_code), query: params).uri.to_s
|
40
41
|
end
|
41
42
|
|
42
|
-
def create_token(code, redirect_uri)
|
43
|
+
def self.create_token(code, redirect_uri, logger: nil)
|
44
|
+
response = self.new(logger: logger).create_token_with_response(code, redirect_uri)
|
45
|
+
if response.success?
|
46
|
+
response.to_h
|
47
|
+
else
|
48
|
+
raise EasyMeli::AuthenticationError.new('Error Creating Token', response)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.refresh_token(refresh_token, logger: nil)
|
53
|
+
response = self.new(logger: logger).refresh_token_with_response(refresh_token)
|
54
|
+
if response.success?
|
55
|
+
response.to_h[EasyMeli::AuthorizationClient::ACCESS_TOKEN_KEY]
|
56
|
+
else
|
57
|
+
raise EasyMeli::AuthenticationError.new('Error Refreshing Token', response)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def create_token_with_response(code, redirect_uri)
|
43
62
|
query_params = merge_auth_params(
|
44
63
|
grant_type: 'authorization_code',
|
45
64
|
code: code,
|
@@ -48,7 +67,7 @@ class EasyMeli::AuthorizationClient
|
|
48
67
|
post_auth(query_params)
|
49
68
|
end
|
50
69
|
|
51
|
-
def
|
70
|
+
def refresh_token_with_response(refresh_token)
|
52
71
|
query_params = merge_auth_params(
|
53
72
|
grant_type: 'refresh_token',
|
54
73
|
refresh_token: refresh_token
|
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.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Northam
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|