volabit 1.0.0 → 1.1.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
  SHA1:
3
- metadata.gz: 3a051bef9491dd7c9dd9e355908101508ecee078
4
- data.tar.gz: 4fe851e275af21008e9366687ed9ae2e4d5dfc76
3
+ metadata.gz: eee00815e91c6b9ef5675445fbec8583f0933994
4
+ data.tar.gz: b29d96bb6053d3afd0767cb0161d26dbb827f69a
5
5
  SHA512:
6
- metadata.gz: b54d6f318b4bc0bb1988c8cf8042e2e94443734564dfe587fc65837a1c79a4211ed1b796bd8d53c8940cc55499efc9ff7aed915e08297822820d75ee442ac9e4
7
- data.tar.gz: 09a5e247a2ac8ca02790d418a769376492d00124a519d8eed2cfb47d8e3e8cb10d4e7e1852e022776a41572af19814ff7e1bd33a035d9512b478e57f9ceeb413
6
+ metadata.gz: 8474154744464f82bd1d6c05608e6c7f2b812dc9235bc7be2284d639c3395d089040d19a96319f7b541fa4e390547126341693927c6918ed4868447b04cf5439
7
+ data.tar.gz: e004c534fbf1b59ab8f0db75acd69ca030e144a31ab1cd0aa427d1c6c02c5e373465671b7553e7a1b11de6fd062325b26fc0ab7e4b116254a644acea79017b9a
@@ -1,23 +1,35 @@
1
1
 
2
2
  # Change Log
3
+ You can see the supported releases on the [project GitHub repository](http://https://github.com/coincovemx/ruby-api-client/releases).
3
4
 
4
5
  ## To Do
5
6
  - Add specs.
6
7
  - Add proper docs.
7
8
  - Automatics test suite run. (Codeship)
8
- - Check methods that require special permissions and bitcoin/pesos funds.
9
9
 
10
- ## [Unreleased](unreleased)
10
+ ## Unreleased
11
11
  ### Added
12
12
  - No recent changes.
13
13
 
14
- ## [v0.0.1](v0.0.1)
14
+ ## v1.1.0
15
+ ### Added
16
+ - Better token handling with `tokens`, `request_tokens`, `ùse_tokens` and `refresh_tokens` available from the client public methods.
17
+ - Aliases for the methods with name changes.
18
+ - `send_money` (`send_funds`) method replaces the `send` method to avoid the clash with the `Òbject#send` method.
19
+ - ArgumentError exceptions for params on methods that use them to build the resource URL.
20
+
21
+ ###Fixed
22
+ - Refreshing the tokens now correctly sets the newly obtained tokens.
23
+ - `Client#new_payment` docs tell that the method is available to all the users.
24
+
25
+ ### Removed
26
+ - `send` method. Use `send_money` method from now on.
27
+
28
+ ## v1.0.0
29
+ ### Added
15
30
  - OAuth2 integration.
16
31
  - Exchange rates wrappers.
17
32
  - Users wrappers.
18
33
  - Transactions wrappers.
19
34
  - Slips wrappers.
20
35
  - Merchants wrappers.
21
-
22
- [v0.0.1]: https://github.com/coincovemx/api-client/tree/v0.0.1
23
- [unreleased]: http://https://github.com/coincovemx/api-client/tree/development
@@ -1,4 +1,3 @@
1
- Copyright (c) 2015 David O' Rojo
2
1
 
3
2
  MIT License
4
3
 
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
+
1
2
  [![Gem Version](https://badge.fury.io/rb/volabit.svg)](http://badge.fury.io/rb/volabit)
2
3
 
3
4
  # Volabit
4
5
 
5
- Volabit's API Ruby library. Integrate the Volabit services in your apps with ease.
6
+ Volabit's API library for Ruby. Integrate the Volabit services in your apps with ease.
6
7
 
7
8
  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].
8
9
 
@@ -20,8 +21,8 @@ And then execute:
20
21
 
21
22
  Or install it yourself as:
22
23
 
23
- $ git clone https://github.com/coincovemx/api-client.git
24
- $ cd api-client
24
+ $ git clone https://github.com/coincovemx/ruby-api-client.git
25
+ $ cd ruby-api-client
25
26
  $ bundle
26
27
  $ gem build volabit.gemspec
27
28
  $ gem install volabit-[version].gem
@@ -31,9 +32,9 @@ Or install it yourself as:
31
32
  1) Instance a new Volabit client object.
32
33
 
33
34
  ```ruby
34
- app_id = 'The registered API for your APP.'
35
- secret = 'The registered secret for your APP.'
36
- callback = 'The registered callback URL for your APP'
35
+ app_id = 'The registered API for your APP.'
36
+ secret = 'The registered secret for your APP.'
37
+ callback = 'The registered callback URL for your APP'
37
38
 
38
39
  volabit_client = Volabit::Client.new(app_id, secret, callback)
39
40
  ```
@@ -44,6 +45,13 @@ Note that by default the Volabit client uses the **production** environment. If
44
45
  volabit_client.sandbox true
45
46
  ```
46
47
 
48
+ You can also provide this parameter upon client instantiation.
49
+
50
+ ```ruby
51
+ environment = 'sandbox'
52
+ volabit_client = Volabit::Client.new(app_id, secret, callback, environment)
53
+ ```
54
+
47
55
  2) Get the URL that will allow the user to authorize your app to use his/her account. (It should be opened in a browser.)
48
56
 
49
57
  ```ruby
@@ -53,16 +61,16 @@ auth_url = volabit_client.authorize
53
61
  3) After you get the authorization code (sent at the callback URL that you provided), you'll use it to get the refresh and access tokens. This code can be used only once, so be sure to store the token object for later use or your app will have to be reauthorized.
54
62
 
55
63
  ```ruby
56
- volabit_client.get_token 'The given authorization code.'
64
+ volabit_client.request_tokens 'The given authorization code.'
57
65
  ```
58
66
 
59
- 4) If you already have a `token` and a `refresh_token` you can use:
67
+ Or, if you already have a `token` and a `refresh_token` you can use:
60
68
 
61
69
  ```ruby
62
- volabit_client.use_token 'token', 'refresh_token'
70
+ volabit_client.use_tokens 'token', 'refresh_token'
63
71
  ```
64
72
 
65
- 5) You're ready to use our API. Just call any method listed [here][wiki].
73
+ 4) You're ready to use our API. Just call any method listed [here][wiki].
66
74
 
67
75
  ```ruby
68
76
  tickers = volabit_client.tickers
@@ -76,15 +84,16 @@ tickers = volabit_client.tickers
76
84
  # }
77
85
  ```
78
86
 
87
+ **Note**: You can obtain the current tokens used by the client with the method `Client#tokens`. Be sure to keep them **safe** withing your application for later use.
79
88
 
80
89
  ## Contributing
81
90
 
82
- 1. Fork it ( https://github.com/[my-github-username]/volabit/fork )
91
+ 1. Fork it ( https://github.com/[my-github-username]/ruby-api-client/fork )
83
92
  2. Create your feature branch (`git checkout -b my-new-feature`)
84
93
  3. Commit your changes (`git commit -am 'Add some feature'`)
85
94
  4. Push to the branch (`git push origin my-new-feature`)
86
95
  5. Create a new Pull Request
87
96
 
88
97
 
89
- [wiki]: https://github.com/coincovemx/api-client/wiki
98
+ [wiki]: https://github.com/coincovemx/ruby-api-client/wiki
90
99
  [api-docs]: https://coincovemx.github.io/
@@ -4,6 +4,8 @@ require_relative 'volabit/api'
4
4
  require_relative 'volabit/version'
5
5
 
6
6
  module Volabit
7
+
8
+ # Class that handles authorizations and API calls.
7
9
  class Client
8
10
  include Auth
9
11
  include API
@@ -8,30 +8,19 @@ module Volabit
8
8
  module API
9
9
  private
10
10
 
11
- # Request a resource using the GET method.
12
- def get_resource(resource, params = nil)
13
- raise no_token_error unless @token
14
- @token.refresh if @token.expired?
15
- response = @token.get(resource, params: params)
16
- JSON.parse response.body, :symbolize_names => true
17
- end
18
-
19
- def post_to_resource(resource, params)
20
- raise no_token_error unless @token
21
- @token.refresh if @token.expired?
22
- response = @token.post(resource, params: params)
23
- JSON.parse response.body, :symbolize_names => true
24
- end
11
+ # Request given resource using the provided method and params.
12
+ def resource(verb, resource, params = nil)
13
+ no_token_error unless @token
14
+ refresh_tokens if @token.expired?
25
15
 
26
- def delete_resource(resource)
27
- raise no_token_error unless @token
28
- @token.refresh if @token.expired?
29
- response = @token.delete(resource)
16
+ response = @token.send(verb, resource, params: params)
30
17
  JSON.parse response.body, :symbolize_names => true
31
18
  end
32
19
 
20
+ # Raises an exception intended when there is no OAuth2::AccessToken
21
+ # available.
33
22
  def no_token_error
34
- 'Error: you have to run get_token or set_token before use this method.'
23
+ raise StandardError, 'Call get_tokens or use_tokens before this method.'
35
24
  end
36
25
  end
37
26
  end
@@ -4,16 +4,25 @@ module Rates
4
4
  # Gets the exchange price from certain currency amount to other currency.
5
5
  # @note BTC units are expected in satoshis. Other currencies units are
6
6
  # expected in cents.
7
- def spot_prices(amount:, from: 'BTC', to: 'MXN')
8
- raise ArgumentError, 'Currencies must be different.' if from.eql? to
9
- get_resource 'api/v1/spot-prices', {
10
- currency_from: from, currency_to: to, amount: amount
7
+ def spot_prices(amount, from: 'BTC', to: 'MXN')
8
+ same_currencies_error if from.eql? to
9
+
10
+ resource :get, 'api/v1/spot-prices', {
11
+ currency_from: from,
12
+ currency_to: to,
13
+ amount: amount
11
14
  }
12
15
  end
13
16
 
14
17
  # Gets the exchange price list for the supported currencies.
15
18
  def tickers
16
- get_resource 'api/v1/tickers'
19
+ resource :get, 'api/v1/tickers'
20
+ end
21
+
22
+ private
23
+
24
+ def same_currencies_error
25
+ raise ArgumentError, 'Currencies must be different.'
17
26
  end
18
27
  end
19
28
 
@@ -1,28 +1,35 @@
1
1
 
2
2
  # Methods to manage slips in the user wallet.
3
3
  module Slips
4
- # Creates a slip you can use to load your wallet.
4
+ # Creates a slip that can be used to load the user wallet.
5
5
  def new_slip(currency:, amount:, type:)
6
- post_to_resource 'api/v1/users/me/slips', {
6
+ resource :post, 'api/v1/users/me/slips', {
7
7
  currency: currency,
8
8
  amount: amount,
9
9
  type: type
10
10
  }
11
11
  end
12
12
 
13
+ alias_method :create_slip, :new_slip
14
+
13
15
  # Gets the information of a specific slip.
14
16
  def get_slip_data(id:)
15
- get_resource "api/v1/users/me/slips/#{id}"
17
+ empty_param_error('id') if id.to_s.eql? ''
18
+ resource :get, "api/v1/users/me/slips/#{id}"
16
19
  end
17
20
 
21
+ alias_method :slip_data, :get_slip_data
22
+
18
23
  # Deletes a specific slip.
19
24
  def delete_slip(id:)
20
- delete_resource "api/v1/users/me/slips/#{id}"
25
+ empty_param_error('id') if id.to_s.eql? ''
26
+ resource :delete, "api/v1/users/me/slips/#{id}"
21
27
  end
22
28
 
23
29
  # Informs of a receipt used to load a wallet's slip.
24
30
  def report_receipt(id:, amount:, affiliation:, authorization:)
25
- post_to_resource "api/v1/users/me/slips/#{id}/report", {
31
+ empty_param_error('id') if id.to_s.eql? ''
32
+ resource :post, "api/v1/users/me/slips/#{id}/report", {
26
33
  amount: amount,
27
34
  affiliation_number: affiliation,
28
35
  authorization_number: authorization
@@ -31,7 +38,14 @@ module Slips
31
38
 
32
39
  # Lists the available options to load a slip.
33
40
  def get_load_methods
34
- get_resource 'api/v1/users/me/slips/methods'
41
+ resource :get, 'api/v1/users/me/slips/methods'
42
+ end
43
+
44
+ alias_method :load_methods, :get_load_methods
45
+
46
+ private
47
+ def empty_param_error(param)
48
+ raise ArgumentError, ":#{param} must not be nil or empty."
35
49
  end
36
50
  end
37
51
 
@@ -4,7 +4,7 @@ module Transactions
4
4
  # Instantly buy bitcoins using fiat balance.
5
5
  # @note The amount is expected in satoshis.
6
6
  def buy_bitcoins(amount:)
7
- post_to_resource 'api/v1/users/me/buys', {
7
+ resource :post, 'api/v1/users/me/buys', {
8
8
  amount: amount
9
9
  }
10
10
  end
@@ -12,27 +12,28 @@ module Transactions
12
12
  # Instantly sell bitcoins to get fiat balance.
13
13
  # @note The amount is expected in satoshis.
14
14
  def sell_bitcoins(amount:)
15
- post_to_resource 'api/v1/users/me/sells', {
15
+ resource :post, 'api/v1/users/me/sells', {
16
16
  amount: amount
17
17
  }
18
18
  end
19
19
 
20
20
  # Instantly send fiat or bitcoins to an address.
21
- # @note The amount is expected in satoshis for bitcoins and cents for fiat
21
+ # @note The amount is expected in satoshis for bitcoins and cents for
22
22
  # fiat currencies.
23
- def send(currency:, amount:, address:)
24
- post_to_resource 'api/v1/users/me/send', {
23
+ def send_money(currency:, amount:, address:)
24
+ resource :post, 'api/v1/users/me/send', {
25
25
  amount: amount,
26
26
  address: address,
27
27
  currency: currency
28
28
  }
29
29
  end
30
30
 
31
+ alias_method :send_funds, :send_money
32
+
31
33
  # Requests a special address to receive a bitcoin payment that will be
32
- # instantly converted to the designated currency.
33
- # @note This action requires special merchant permissions to be performed.
34
+ # converted to the designated currency.
34
35
  def new_payment(currency:, amount:)
35
- post_to_resource 'api/v1/users/me/green-addresses', {
36
+ resource :post, 'api/v1/users/me/green-addresses', {
36
37
  currency: currency,
37
38
  amount: amount
38
39
  }
@@ -1,10 +1,10 @@
1
1
 
2
2
  # API endpoints for user account management.
3
3
  module Users
4
- # Creates a new user account.
5
- # @note Special partner permissions are required to perform this action.
4
+ # Request the creation of a new user with the given params.
5
+ # @note This action requires partner privileges.
6
6
  def create_user(acceptance:, email:, pass: '')
7
- post_to_resource 'api/v1/users', {
7
+ resource :post, 'api/v1/users', {
8
8
  accepts_terms_of_service: acceptance,
9
9
  user: {
10
10
  email: email, password: pass
@@ -14,9 +14,9 @@ module Users
14
14
 
15
15
  alias_method :new_user, :create_user
16
16
 
17
- # Get the account information from the user that authorized the app.
17
+ # Gets the information details of the app user.
18
18
  def me
19
- get_resource 'api/v1/users/me'
19
+ resource :get, 'api/v1/users/me'
20
20
  end
21
21
 
22
22
  alias_method :user_data, :me
@@ -5,7 +5,7 @@ require_relative 'common'
5
5
 
6
6
  module Volabit
7
7
  include Common::Constants
8
- extend Common::Helpers
8
+ extend Common::Helpers
9
9
 
10
10
  # Methods to manage the OAuth2 authentication process.
11
11
  module Auth
@@ -19,53 +19,82 @@ module Volabit
19
19
  @oauth_client.auth_code.authorize_url redirect_uri: @url
20
20
  end
21
21
 
22
+ # Gets the information of the tokens currently set on the client.
23
+ #
24
+ # @return [Hash] including the access and refresh tokens and the
25
+ # expiration POSIX time, or an error message it one occurs
26
+ # while requesting the tokens.
27
+ def tokens
28
+ (@token.kind_of? OAuth2::AccessToken) ? @token.to_hash : @token
29
+ end
30
+
22
31
  # Requests and sets the access and refresh tokens to use the Volabit API
23
32
  # resources.
24
33
  #
25
34
  # @param auth_token [String]
26
- # @return [OAuth2::AccessToken] object that includes the credentials to
27
- # access the API and the methods to renew itself.
28
- def get_token(auth_code)
35
+ # @return [Hash] with the tokens information. (See #tokens).
36
+ def request_tokens(auth_code)
29
37
  @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
- }
38
+ tokens
37
39
  end
38
40
 
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
- }
41
+ alias_method :get_token, :request_tokens
42
+
43
+ # Loads the client with the provided token information.
44
+ #
45
+ # @param tokens_hash [Hash] that must contain at least the access and
46
+ # refresh tokens.
47
+ # @return [Hash] with the tokens information. (See #tokens).
48
+ def use_tokens(tokens_hash)
49
+ @token = OAuth2::AccessToken.from_hash @oauth_client, tokens_hash
50
+ retrieve_missing_token_info if @token.expires_at.nil?
51
+ tokens
52
+ end
53
+
54
+ alias_method :use_token, :use_tokens
55
+
56
+ # Triggers a refresh of the current tokens.
57
+ #
58
+ # @return [Hash] with the tokens information. (See #tokens).
59
+ # @note This method is provided as convenience, as the client checks the
60
+ # expiration of the tokens before each call to the API.
61
+ def refresh_tokens
62
+ @token = @token.refresh!
63
+ tokens
50
64
  end
51
65
 
52
66
  # Toggles the test environment with a boolean value.
53
67
  # @note Set it before requiring the user authorization or your app will need
54
- # to be reauthorized.)
68
+ # to be reauthorized.
55
69
  def sandbox(flag)
56
- @oauth_client.site = case flag
57
- when true then Volabit.site_for 'sandbox'
58
- when false then Volabit.site_for 'production'
59
- else
60
- @oauth_client.site
70
+ case flag
71
+ when true then @oauth_client.site = Volabit.site_for 'sandbox'
72
+ when false then @oauth_client.site = Volabit.site_for 'production'
61
73
  end
62
74
  end
63
75
 
64
76
  private
65
77
 
78
+ # Gets and sets information about the expiration time for provided
79
+ # tokens without it.
80
+ def retrieve_missing_token_info
81
+ response = JSON.parse @token.get('/oauth/token/info').body
82
+
83
+ if response['error']
84
+ response['hint'] = 'Reauthorization may be required.'
85
+ @token = response
86
+ else
87
+ time_left = response['expires_in_seconds']
88
+ @token.instance_variable_set '@expires_at', (Time.now.to_i + time_left)
89
+ end
90
+ end
91
+
66
92
  # Instances a new OAuth client to manage authorizations.
67
93
  def set_oauth_client(id, secret, env)
68
- @oauth_client = OAuth2::Client.new(id, secret, site: Volabit.site_for(env))
94
+ @oauth_client = OAuth2::Client.new(id, secret, {
95
+ site: Volabit.site_for(env),
96
+ raise_errors: false
97
+ })
69
98
  end
70
99
  end
71
100
  end
@@ -3,7 +3,8 @@ require_relative 'common/constants'
3
3
  require_relative 'common/helpers'
4
4
 
5
5
  module Volabit
6
- # Values and methods used by the library.
6
+ # Common values and methods used by the library.
7
7
  module Common
8
+ # See files under /lib/volabit/common/
8
9
  end
9
10
  end
@@ -1,3 +1,3 @@
1
1
  module Volabit
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: volabit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Volabit Team & Contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-11 00:00:00.000000000 Z
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -159,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  requirements: []
161
161
  rubyforge_project:
162
- rubygems_version: 2.2.2
162
+ rubygems_version: 2.4.6
163
163
  signing_key:
164
164
  specification_version: 4
165
165
  summary: Library for the Volabit API.