Skip to content

Commit

Permalink
Adds refresh_access_token to the authentication module.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelivan committed Feb 26, 2013
1 parent b27f105 commit 09de652
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ This gem has tests for a few methods. To check if it is working properly, just r
Changelog
---------

2.0.1

Added the refresh_access_token method to the authentication module.

2.0.0 (thanks leanucci and cavi21)

Implemented basic access to the collection search method. Changed the test credentials. Using Ruby 1.9 hash format. Added documentation for collection search.
Expand Down
13 changes: 10 additions & 3 deletions lib/mercadopago/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ def self.access_token(client_id, client_secret)
end

#
# TODO
# Receives the client credentials and a valid refresh token and requests a new access token.
#
def refresh_access_token
# TODO
# - client_id
# - client_secret
# - refresh_token
#
def self.refresh_access_token(client_id, client_secret, refresh_token)
payload = { grant_type: 'refresh_token', client_id: client_id, client_secret: client_secret, refresh_token: refresh_token }
headers = { content_type: 'application/x-www-form-urlencoded', accept: 'application/json' }

MercadoPago::Request.wrap_post('/oauth/token', payload, headers)
end

end
Expand Down
48 changes: 39 additions & 9 deletions lib/mercadopago/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,27 @@ class AccessError < Exception
#
class Client

attr_reader :token
attr_reader :access_token, :refresh_token

#
# Creates an instance and stores the access_token to make calls to the
# MercadoPago API.
#
# - client_id
# - client_secret
#
def initialize(client_id, client_secret)
response = MercadoPago::Authentication.access_token(client_id, client_secret)
load_tokens MercadoPago::Authentication.access_token(client_id, client_secret)
end

unless @token = response['access_token']
raise AccessError, response['message']
end
#
# Refreshes an access token.
#
# - client_id
# - client_secret
#
def refresh_access_token(client_id, client_secret)
load_tokens MercadoPago::Authentication.refresh_access_token(client_id, client_secret, @refresh_token)
end

#
Expand All @@ -42,7 +51,7 @@ def initialize(client_id, client_secret)
# - data: contains the data according to the payment preference that will be created.
#
def create_preference(data)
MercadoPago::Checkout.create_preference(@token, data)
MercadoPago::Checkout.create_preference(@access_token, data)
end

#
Expand All @@ -51,7 +60,7 @@ def create_preference(data)
# - preference_id: the id of the payment preference that will be retrieved.
#
def get_preference(preference_id)
MercadoPago::Checkout.get_preference(@token, preference_id)
MercadoPago::Checkout.get_preference(@access_token, preference_id)
end

#
Expand All @@ -60,7 +69,7 @@ def get_preference(preference_id)
# - payment_id: the id of the payment to be checked.
#
def notification(payment_id)
MercadoPago::Collection.notification(@token, payment_id)
MercadoPago::Collection.notification(@access_token, payment_id)
end

#
Expand All @@ -69,7 +78,28 @@ def notification(payment_id)
# - search_hash: the search hash to find collections.
#
def search(search_hash)
MercadoPago::Collection.search(@token, search_hash)
MercadoPago::Collection.search(@access_token, search_hash)
end

#
# Private methods.
#
private

#
# Loads the tokens from the authentication hash.
#
# - auth: the authentication hash returned by MercadoPago.
#
def load_tokens(auth)
mandatory_keys = %w{ access_token refresh_token }

if (auth.keys & mandatory_keys) == mandatory_keys
@access_token = auth['access_token']
@refresh_token = auth['refresh_token']
else
raise AccessError, auth['message']
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/mercadopago/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MercadoPago
VERSION = "2.0.0"
VERSION = "2.0.1"
end
12 changes: 11 additions & 1 deletion test/test_mercado_pago.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def test_that_authentication_fails_with_wrong_parameters
assert_equal "invalid_client", response['error']
end

def test_that_refresh_token_works
auth = MercadoPago::Authentication.access_token(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
refresh = MercadoPago::Authentication.refresh_access_token(CREDENTIALS[:client_id], CREDENTIALS[:client_secret], auth['refresh_token'])

assert refresh['access_token']
assert refresh['refresh_token']
assert refresh['access_token'] != auth['access_token']
assert refresh['refresh_token'] != auth['refresh_token']
end

# Using fake token
def test_that_request_fails_with_wrong_token
response = MercadoPago::Checkout.create_preference('fake_token', {})
Expand All @@ -60,7 +70,7 @@ def test_that_request_fails_with_wrong_token

def test_that_client_initializes_okay_with_valid_details
mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
assert mp_client.token
assert mp_client.access_token
end

def test_that_client_fails_with_wrong_details
Expand Down

0 comments on commit 09de652

Please sign in to comment.