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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89f477f8757f02eec3581b7336db18264543b27dd335b5eda3c532fcf1566cec
4
- data.tar.gz: dc55656ac52fcbe17a58659ce619fd4f20e90a8334f7ab3a3956f13a3a61caf4
3
+ metadata.gz: ee793dc5802586adac55653bb44e8183f2903583fe2463e57d3f6ba6f8f87810
4
+ data.tar.gz: bddc2f0e96a9d2080088af552aba50cdf02530d869d92837ddb8d1339a5450b0
5
5
  SHA512:
6
- metadata.gz: 276d56f9e9dbd6fe0f6d544220b7942442fdf372ed8d22c5d6e87c1901dec997d8c6e432041cda289d1e855df08f76b449483ceb0bfc932afe8436d97a23b3e0
7
- data.tar.gz: '0588f3f0451f110057e54ade0e1a68bbaaf8180c4aaf8cdf92e26c9e7616c475c73961d6c5552327bc350620676ce899dc6b21d058a650e719a6442976c1b25e'
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::AuthorizationClient.authorization_url('MX', 'your_redirect_url')
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::AuthorizationClient.new.create_token('the_code_in_the_redirect', 'the_same_redirect_url_as_above')
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
- response = EasyMeli::AuthorizationClient.new.refresh_token('a_refresh_token')
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::ApiClient.new(access_token)
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 when instantiating the `EasyMeli::ApiClient` or `EasyMeli::AuthorizationClient`. 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.
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::AuthorizationClient.new(logger: my_logger)
66
- EasyMeli::ApiClient.new(access_token, logger: my_logger)
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
- ### Complete example showing how to retrieve a user profile
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
- authorization_client = EasyMeli::AuthorizationClient.new
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
  ```
@@ -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
@@ -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 refresh_token(refresh_token)
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
@@ -1,3 +1,3 @@
1
1
  module EasyMeli
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
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.3.1
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-18 00:00:00.000000000 Z
11
+ date: 2020-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty