mercadopago 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -303,6 +303,10 @@ This gem has tests for a few methods. To check if it is working properly, just r
303
303
  Changelog
304
304
  ---------
305
305
 
306
+ 2.0.1
307
+
308
+ Added the refresh_access_token method to the authentication module.
309
+
306
310
  2.0.0 (thanks leanucci and cavi21)
307
311
 
308
312
  Implemented basic access to the collection search method. Changed the test credentials. Using Ruby 1.9 hash format. Added documentation for collection search.
@@ -20,10 +20,17 @@ module MercadoPago
20
20
  end
21
21
 
22
22
  #
23
- # TODO
23
+ # Receives the client credentials and a valid refresh token and requests a new access token.
24
24
  #
25
- def refresh_access_token
26
- # TODO
25
+ # - client_id
26
+ # - client_secret
27
+ # - refresh_token
28
+ #
29
+ def self.refresh_access_token(client_id, client_secret, refresh_token)
30
+ payload = { grant_type: 'refresh_token', client_id: client_id, client_secret: client_secret, refresh_token: refresh_token }
31
+ headers = { content_type: 'application/x-www-form-urlencoded', accept: 'application/json' }
32
+
33
+ MercadoPago::Request.wrap_post('/oauth/token', payload, headers)
27
34
  end
28
35
 
29
36
  end
@@ -22,18 +22,27 @@ module MercadoPago
22
22
  #
23
23
  class Client
24
24
 
25
- attr_reader :token
25
+ attr_reader :access_token, :refresh_token
26
26
 
27
27
  #
28
28
  # Creates an instance and stores the access_token to make calls to the
29
29
  # MercadoPago API.
30
30
  #
31
+ # - client_id
32
+ # - client_secret
33
+ #
31
34
  def initialize(client_id, client_secret)
32
- response = MercadoPago::Authentication.access_token(client_id, client_secret)
35
+ load_tokens MercadoPago::Authentication.access_token(client_id, client_secret)
36
+ end
33
37
 
34
- unless @token = response['access_token']
35
- raise AccessError, response['message']
36
- end
38
+ #
39
+ # Refreshes an access token.
40
+ #
41
+ # - client_id
42
+ # - client_secret
43
+ #
44
+ def refresh_access_token(client_id, client_secret)
45
+ load_tokens MercadoPago::Authentication.refresh_access_token(client_id, client_secret, @refresh_token)
37
46
  end
38
47
 
39
48
  #
@@ -42,7 +51,7 @@ module MercadoPago
42
51
  # - data: contains the data according to the payment preference that will be created.
43
52
  #
44
53
  def create_preference(data)
45
- MercadoPago::Checkout.create_preference(@token, data)
54
+ MercadoPago::Checkout.create_preference(@access_token, data)
46
55
  end
47
56
 
48
57
  #
@@ -51,7 +60,7 @@ module MercadoPago
51
60
  # - preference_id: the id of the payment preference that will be retrieved.
52
61
  #
53
62
  def get_preference(preference_id)
54
- MercadoPago::Checkout.get_preference(@token, preference_id)
63
+ MercadoPago::Checkout.get_preference(@access_token, preference_id)
55
64
  end
56
65
 
57
66
  #
@@ -60,7 +69,7 @@ module MercadoPago
60
69
  # - payment_id: the id of the payment to be checked.
61
70
  #
62
71
  def notification(payment_id)
63
- MercadoPago::Collection.notification(@token, payment_id)
72
+ MercadoPago::Collection.notification(@access_token, payment_id)
64
73
  end
65
74
 
66
75
  #
@@ -69,7 +78,28 @@ module MercadoPago
69
78
  # - search_hash: the search hash to find collections.
70
79
  #
71
80
  def search(search_hash)
72
- MercadoPago::Collection.search(@token, search_hash)
81
+ MercadoPago::Collection.search(@access_token, search_hash)
82
+ end
83
+
84
+ #
85
+ # Private methods.
86
+ #
87
+ private
88
+
89
+ #
90
+ # Loads the tokens from the authentication hash.
91
+ #
92
+ # - auth: the authentication hash returned by MercadoPago.
93
+ #
94
+ def load_tokens(auth)
95
+ mandatory_keys = %w{ access_token refresh_token }
96
+
97
+ if (auth.keys & mandatory_keys) == mandatory_keys
98
+ @access_token = auth['access_token']
99
+ @refresh_token = auth['refresh_token']
100
+ else
101
+ raise AccessError, auth['message']
102
+ end
73
103
  end
74
104
 
75
105
  end
@@ -1,3 +1,3 @@
1
1
  module MercadoPago
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -51,6 +51,16 @@ class TestMercadoPago < MiniTest::Unit::TestCase
51
51
  assert_equal "invalid_client", response['error']
52
52
  end
53
53
 
54
+ def test_that_refresh_token_works
55
+ auth = MercadoPago::Authentication.access_token(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
56
+ refresh = MercadoPago::Authentication.refresh_access_token(CREDENTIALS[:client_id], CREDENTIALS[:client_secret], auth['refresh_token'])
57
+
58
+ assert refresh['access_token']
59
+ assert refresh['refresh_token']
60
+ assert refresh['access_token'] != auth['access_token']
61
+ assert refresh['refresh_token'] != auth['refresh_token']
62
+ end
63
+
54
64
  # Using fake token
55
65
  def test_that_request_fails_with_wrong_token
56
66
  response = MercadoPago::Checkout.create_preference('fake_token', {})
@@ -60,7 +70,7 @@ class TestMercadoPago < MiniTest::Unit::TestCase
60
70
 
61
71
  def test_that_client_initializes_okay_with_valid_details
62
72
  mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
63
- assert mp_client.token
73
+ assert mp_client.access_token
64
74
  end
65
75
 
66
76
  def test_that_client_fails_with_wrong_details
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mercadopago
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-12-21 00:00:00.000000000 Z
13
+ date: 2013-02-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
@@ -123,3 +123,4 @@ specification_version: 3
123
123
  summary: Client for the MercadoPago API
124
124
  test_files:
125
125
  - test/test_mercado_pago.rb
126
+ has_rdoc: