Skip to content

Commit 4f8f617

Browse files
committed
Merge pull request #16 from coincovemx/general-fixes
General fixes
2 parents d02ab91 + 59e3bcf commit 4f8f617

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
[![Gem Version](https://badge.fury.io/rb/volabit.svg)](http://badge.fury.io/rb/volabit)
2+
13
# Volabit
24

3-
Volabit's API library. Integrate the Volabit services in your apps with ease.
5+
Volabit's API Ruby library. Integrate the Volabit services in your apps with ease.
46

57
You can see the available methods on the [project wiki][wiki]. Details of the API use can be found on the [official page][api-docs].
68

@@ -36,7 +38,7 @@ callback = 'The registered callback URL for your APP'
3638
volabit_client = Volabit::Client.new(app_id, secret, callback)
3739
```
3840

39-
Note that the by default the Volabit client uses the **production** environment. If you want to use the **test** environment, set the sandbox flag to `true` before requesting the authorization code.
41+
Note that by default the Volabit client uses the **production** environment. If you want to use the **test** environment, set the sandbox flag to `true` before requesting the authorization code.
4042

4143
```ruby
4244
volabit_client.sandbox true
@@ -54,11 +56,16 @@ auth_url = volabit_client.authorize
5456
volabit_client.get_token 'The given authorization code.'
5557
```
5658

57-
4) With these tokens, you'll be ready to call the services. The methods will return a response object. The contend can be retrieved with the `body` property and will be in the JSON format.
59+
4) If you already have a `token` and a `refresh_token` you can use:
60+
61+
```ruby
62+
volabit_client.use_token 'token', 'refresh_token'
63+
```
64+
65+
5) You're ready to use our API. Just call any method listed [here][wiki].
5866

5967
```ruby
60-
response = volabit_client.tickers
61-
response.body
68+
tickers = volabit_client.tickers
6269
# => {
6370
# "btc_usd_buy":"226.06",
6471
# "btc_usd_sell":"226.56",

lib/volabit/api.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,28 @@ module API
1010

1111
# Request a resource using the GET method.
1212
def get_resource(resource, params = nil)
13+
raise no_token_error unless @token
1314
@token.refresh if @token.expired?
14-
@token.get(resource, params: params)
15+
response = @token.get(resource, params: params)
16+
JSON.parse response.body, :symbolize_names => true
1517
end
1618

1719
def post_to_resource(resource, params)
20+
raise no_token_error unless @token
1821
@token.refresh if @token.expired?
19-
@token.post(resource, params: params)
22+
response = @token.post(resource, params: params)
23+
JSON.parse response.body, :symbolize_names => true
2024
end
2125

2226
def delete_resource(resource)
27+
raise no_token_error unless @token
2328
@token.refresh if @token.expired?
24-
@token.delete(resource)
29+
response = @token.delete(resource)
30+
JSON.parse response.body, :symbolize_names => true
31+
end
32+
33+
def no_token_error
34+
'Error: you have to run get_token or set_token before use this method.'
2535
end
2636
end
2737
end

lib/volabit/auth.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ def authorize
2727
# access the API and the methods to renew itself.
2828
def get_token(auth_code)
2929
@token = @oauth_client.auth_code.get_token auth_code, redirect_uri: @url
30+
{
31+
:token => @token.token,
32+
:refresh_token => @token.refresh_token,
33+
:expires_in => @token.expires_in,
34+
:expires_at => @token.expires_at,
35+
:options => @token.options
36+
}
37+
end
38+
39+
def use_token(token, refresh_token)
40+
token = OAuth2::AccessToken.new @oauth_client, token, { :refresh_token => refresh_token }
41+
token_info = JSON.parse token.get('/oauth/token/info').body
42+
expires_in = token_info['expires_in_seconds']
43+
@token = OAuth2::AccessToken.new @oauth_client, token, {
44+
:refresh_token => refresh_token,
45+
:expires_in => expires_in,
46+
:mode => :header,
47+
:header_format => 'Bearer %s',
48+
:param_name => 'access_token'
49+
}
3050
end
3151

3252
# Toggles the test environment with a boolean value.

lib/volabit/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Volabit
2-
VERSION = "0.0.1"
2+
VERSION = "1.0.0"
33
end

0 commit comments

Comments
 (0)