stocks_exchange_api_client 0.1.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f5276b14f88f028f92918ff4d93cd54272a744b136521333d540f8db5903dbc
4
- data.tar.gz: fd9564868f88d9ff67a923dfd53848e238dff2f36949a45ce237e84c5e80c3d4
3
+ metadata.gz: 2d0847320eac0c013dbd66653b3973d7be3c23be6e51b554ca2db079cf822746
4
+ data.tar.gz: afa8667073e56d959c3a6c1e1608364c8e91164d1f99202ad18e3d7f3933f108
5
5
  SHA512:
6
- metadata.gz: 44e409f86d0e545c749af69ae65351ef6a4b07daa30711371c2a5b1f4a15d4be0f35c231dcb93fe1037a4fd7e5888fcb94d63224ca079ef5012351bcf7d47c38
7
- data.tar.gz: 4012da2f6c11387904bf508024742a0c123f5bf5eb9b7b096493060e9751a13199631da6d0fc91e2f172f98405a60629e6e2b390b327639d2b554dd04f01b2a4
6
+ metadata.gz: 7f220621254bc69a3d94a00842e3b411560986c43fb22d9f17192e7f6ec57f9e0ea7c996d052e275293442e7808ff99b5966e935cd443b4a41876f04ce52ba6d
7
+ data.tar.gz: a8e1df7d4cd84ae62061ef0802cbab5f11d8faa58254ea7d2d3a63bc66424b5d770c45e5a652ec5823c2b10148291fe72b1043edda8fb8efde0bbd5896dbf9c2
data/.gitignore CHANGED
@@ -49,3 +49,5 @@ build-iPhoneSimulator/
49
49
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
50
  .rvmrc
51
51
  /.idea
52
+ Gemfile.lock
53
+ settings.json
data/README.md CHANGED
@@ -14,10 +14,12 @@ The base URL for all the requests other than public methods is
14
14
  ```
15
15
  https://app.stocks.exchange/api2
16
16
  https://app.stex.com/api2
17
+ https://api3.stex.com
17
18
  ```
18
19
 
19
20
  ## Getting started
20
- -[Documentation](http://help.stex.com/api-integration).
21
+ - [Documentation](http://help.stex.com/api-integration).
22
+ - [Sandbox API V3](https://apidocs.stex.com).
21
23
 
22
24
  To get started with the Ruby API client, here's a snippet for creating a client with existing credentials:
23
25
  > In order to use the API functions, you must have an API key and API secret, which is generated in the user profile.
@@ -29,7 +31,7 @@ gem install stocks_exchange_api_client
29
31
 
30
32
  After install use for example this code!
31
33
 
32
- ### Example
34
+ ### Example Example API V2
33
35
  ```ruby
34
36
  require 'stocks_exchange_api_client'
35
37
 
@@ -109,6 +111,64 @@ puts StocksExchangeApiClient::Public.trade_history('BTC_USDT')
109
111
  # Params Pair
110
112
  puts StocksExchangeApiClient::Public.order_book('BTC_USDT')
111
113
  ```
114
+
115
+ ### Example V3
116
+ ```ruby
117
+ StocksExchangeApiClient.configure do |conf|
118
+ conf.url = 'https://api3.stex.com'
119
+ conf.use_version = 3
120
+ conf.option = {
121
+ client_id: '',
122
+ client_secret: '',
123
+ token_object: {
124
+ access_token: '',
125
+ refresh_token: ''
126
+ },
127
+ access_token_url: 'https://api3.stex.com/oauth/token',
128
+ scope: 'trade profile reports withdrawal'
129
+ }
130
+ end
131
+
132
+ puts StocksExchangeApiClient::PrivateApiV3.profile_info
133
+ ```
134
+ ## Lists Methods
135
+ - [Sandbox API V3](https://apidocs.stex.com).
136
+ ```
137
+ profile_info() // Get general information about the current user.
138
+ wallets() // Get a list of user wallets.
139
+ wallets_by_id() // Single wallet information
140
+ add_wallets_by_currency_id() // Create a wallet for given currency
141
+ get_wallets_address() // Get deposit address for given wallet
142
+ new_wallets_address() // Create new deposit address
143
+ deposits() // Get a list of deposits made by user
144
+ deposits_by_id() // Get deposit by id
145
+ withdrawals() // Get a list of withdrawals made by user
146
+ withdrawals_by_id() // Get withdrawal by id
147
+ add_withdrawal() // Create withdrawal request
148
+ cancel_withdrawal_by_id() // Cancel unconfirmed withdrawal
149
+ reports_orders() // Get past orders
150
+ reports_orders_by_id() // Get specified order details
151
+ all_trading_orders() // List your currently open orders
152
+ delete_all_trading_orders() // Delete all active orders
153
+ trading_orders_by_pair() // List your currently open orders for given currency pair
154
+ delete_trading_orders_by_pair() // Delete active orders for given currency pair
155
+ add_trading_orders_by_pair() // Create new order and put it to the orders processing queue
156
+ trading_order_by_id() // Get a single order
157
+ delete_trading_order_by_id() // Cancel order
158
+
159
+ currencies() // Available Currencies
160
+ currencies_by_id() // Get currency info
161
+ markets() // Available markets
162
+ pairs_list_by_code() // Available currency pairs
163
+ pairs_list_by_id() // Get currency pair information
164
+ ticker() // Tickers list for all currency pairs
165
+ ticker_by_pair_id() // Ticker for currency pair
166
+ trades() // Trades for given currency pair
167
+ orderbook_by_pair_id() // Orderbook for given currency pair
168
+ chart() // A list of candles for given currency pair
169
+ ping() // Test API is working and get server time
170
+ ```
171
+
112
172
  ## Common Errors
113
173
  ### Here is a list with common errors and their descriptions:
114
174
  1. Invalid Key - not generated key or the key does not correspond to the a user
@@ -2,42 +2,44 @@ require 'openssl'
2
2
  require 'httparty'
3
3
  require 'json'
4
4
  require 'ostruct'
5
-
5
+ require 'date'
6
6
 
7
7
  # Version
8
8
  require 'stocks_exchange_api_client/version'
9
9
 
10
- # Private API method
10
+ # Private API V2 method
11
11
  require 'stocks_exchange_api_client/private'
12
+ # Private API V3 method
13
+ require 'stocks_exchange_api_client/private_v3'
12
14
 
13
- # Public API method
15
+ # Public API V2 method
14
16
  require 'stocks_exchange_api_client/public'
17
+ # Private API V3 method
18
+ require 'stocks_exchange_api_client/public_v3'
15
19
 
16
20
  # Configuration
17
21
  require 'stocks_exchange_api_client/configuration'
18
22
 
19
-
20
23
  module StocksExchangeApiClient
21
-
22
24
  METHOD_NAME = {
23
- get_info: 'GetInfo',
24
- active_orders: 'ActiveOrders',
25
- trade: 'Trade',
26
- cancel_order: 'CancelOrder',
27
- trade_history: 'TradeHistory',
28
- trade_register_history: 'TradeRegisterHistory',
29
- user_history: 'UserHistory',
30
- trans_history: 'TransHistory',
31
- grafic: 'Grafic',
32
- generate_wallets: 'GenerateWallets',
33
- deposit: 'Deposit',
34
- withdraw: 'Withdraw'
25
+ get_info: 'GetInfo',
26
+ active_orders: 'ActiveOrders',
27
+ trade: 'Trade',
28
+ cancel_order: 'CancelOrder',
29
+ trade_history: 'TradeHistory',
30
+ trade_register_history: 'TradeRegisterHistory',
31
+ user_history: 'UserHistory',
32
+ trans_history: 'TransHistory',
33
+ grafic: 'Grafic',
34
+ generate_wallets: 'GenerateWallets',
35
+ deposit: 'Deposit',
36
+ withdraw: 'Withdraw'
35
37
  }.freeze
36
38
 
37
- HEX_ALGORITHM = 'sha512'
39
+ HEX_ALGORITHM = 'sha512'.freeze
40
+ JSON_SETTINGS = 'settings.json'.freeze
38
41
 
39
42
  class << self
40
-
41
43
  def configure
42
44
  yield(configuration)
43
45
  end
@@ -45,14 +47,16 @@ module StocksExchangeApiClient
45
47
  def make_api_request(method = :post, params = {}, type)
46
48
  configuration.validate!
47
49
  if method == :post
48
- params.merge!({method: type, nonce: Time.now.to_i})
50
+ params[:method] = type
51
+ params[:nonce] = Time.now.to_i
49
52
  encode_www_form = URI.encode_www_form(params)
50
- sign = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new(HEX_ALGORITHM), configuration.api_secret, encode_www_form)
53
+ sign = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new(HEX_ALGORITHM), configuration.option[:api_secret], encode_www_form)
51
54
  response = JSON.parse(
52
- HTTParty.post(configuration.url, body: params, headers: {
53
- :Sign => sign,
54
- :Key => configuration.api_key
55
- }).body)
55
+ HTTParty.post(configuration.url, body: params, headers: {
56
+ Sign: sign,
57
+ Key: configuration.option[:api_key]
58
+ }).body
59
+ )
56
60
  response = OpenStruct.new(response)
57
61
  end
58
62
  if method == :get
@@ -61,8 +65,65 @@ module StocksExchangeApiClient
61
65
  response
62
66
  end
63
67
 
68
+ def make_api_request_v3(url, params = {}, method = :get, _type = :url)
69
+ configuration.validate!
70
+ post_data = URI.encode_www_form(params)
71
+ url_full = "#{configuration.url}/#{url}#{post_data == '' ? '' : '?'}#{post_data}"
72
+ if method == :post
73
+ response = HTTParty.post(url_full, body: params, headers: {
74
+ 'Authorization' => "Bearer #{get_token}",
75
+ 'Accept' => 'application/json',
76
+ 'User-Agent' => 'stocks.exchange-client'
77
+ }).body
78
+ end
79
+ if method == :get
80
+ response = HTTParty.get(url_full, headers: {
81
+ 'Authorization' => "Bearer #{get_token}",
82
+ 'Accept' => 'application/json',
83
+ 'User-Agent' => 'stocks.exchange-client'
84
+ }).body
85
+ end
86
+ if method == :delete
87
+ response = HTTParty.delete(url_full, headers: {
88
+ 'Authorization' => "Bearer #{get_token}",
89
+ 'Accept' => 'application/json',
90
+ 'User-Agent' => 'stocks.exchange-client'
91
+ }).body
92
+ end
93
+ response
94
+ end
95
+
96
+ def get_token
97
+ if File.exist?(JSON_SETTINGS)
98
+ current_token = JSON.parse(File.read(JSON_SETTINGS))
99
+ else
100
+ current_token = { 'access_token' => configuration.option[:token_object][:access_token],
101
+ 'refresh_token' => configuration.option[:token_object][:refresh_token],
102
+ 'expires_in' => nil,
103
+ 'expires_in_date' => nil }
104
+ end
105
+ if !current_token.nil? && !current_token['expires_in_date'].nil?
106
+ return current_token['access_token'] if DateTime.parse(current_token['expires_in_date']).to_datetime > DateTime.now.to_datetime
107
+ end
108
+ begin
109
+ response = HTTParty.post(configuration.option[:access_token_url], body: {
110
+ grant_type: 'refresh_token',
111
+ refresh_token: current_token['refresh_token'],
112
+ client_id: configuration.option[:client_id],
113
+ client_secret: configuration.option[:client_secret],
114
+ scope: configuration.option[:scope]
115
+ }).body
116
+ current_token = JSON.parse(response)
117
+ current_token['expires_in_date'] = Time.at(Time.now.to_i + current_token['expires_in']).to_s
118
+ File.write(JSON_SETTINGS, current_token.to_json)
119
+ current_token['access_token']
120
+ rescue StandardError => e
121
+ puts "Rescued: #{e.inspect}"
122
+ end
123
+ end
124
+
64
125
  def configuration
65
126
  @configuration ||= Configuration.new
66
127
  end
67
128
  end
68
- end
129
+ end
@@ -1,18 +1,25 @@
1
1
  module StocksExchangeApiClient
2
-
3
2
  class Configuration
4
-
5
- URL = 'https://app.stocks.exchange/api2'
6
-
7
- attr_accessor :url, :api_key, :api_secret
3
+ URL_V2 = 'https://app.stocks.exchange/api2'.freeze
4
+ URL_V3 = 'https://api3.stex.com'.freeze
5
+ attr_accessor :url, :option, :use_version
8
6
 
9
7
  def initialize
10
- @url ||= URL
8
+ @use_version ||= 2
9
+ @url ||= URL_V2
11
10
  end
12
11
 
13
12
  def validate!
14
- raise Errors::ConfigurationError unless api_key && api_secret
13
+ if use_version == 2
14
+ unless option[:api_key] && option[:api_secret]
15
+ raise Errors::ConfigurationError
16
+ end
17
+ end
18
+ if use_version == 3
19
+ unless option[:client_id] && option[:client_secret] && option[:token_object][:access_token] && option[:token_object][:refresh_token]
20
+ raise Errors::ConfigurationError
21
+ end
22
+ end
15
23
  end
16
24
  end
17
-
18
25
  end
@@ -0,0 +1,119 @@
1
+ module StocksExchangeApiClient
2
+ class PrivateApiV3
3
+ class << self
4
+ def profile_info
5
+ StocksExchangeApiClient.make_api_request_v3('/profile/info')
6
+ end
7
+
8
+ def wallets
9
+ StocksExchangeApiClient.make_api_request_v3('/profile/wallets')
10
+ end
11
+
12
+ def wallets_by_id(wallet_id)
13
+ url = "/profile/wallets/#{wallet_id}"
14
+ StocksExchangeApiClient.make_api_request_v3(url)
15
+ end
16
+
17
+ def add_wallets_by_currency_id(currency_id)
18
+ url = "/profile/wallets/#{currency_id}"
19
+ StocksExchangeApiClient.make_api_request_v3(url, {}, :post)
20
+ end
21
+
22
+ def get_wallets_address(wallet_id)
23
+ url = "/profile/wallets/address/#{wallet_id}"
24
+ StocksExchangeApiClient.make_api_request_v3(url)
25
+ end
26
+
27
+ def new_wallets_address(wallet_id)
28
+ url = "/profile/wallets/address/#{wallet_id}"
29
+ StocksExchangeApiClient.make_api_request_v3(url, {}, :post)
30
+ end
31
+
32
+ def deposits(params)
33
+ url = '/profile/deposits'
34
+ StocksExchangeApiClient.make_api_request_v3(url, params)
35
+ end
36
+
37
+ def deposits_by_id(id)
38
+ url = "/profile/deposits/#{id}"
39
+ StocksExchangeApiClient.make_api_request_v3(url)
40
+ end
41
+
42
+ def withdrawals(params)
43
+ url = '/profile/withdrawals'
44
+ StocksExchangeApiClient.make_api_request_v3(url, params)
45
+ end
46
+
47
+ def withdrawals_by_id(id)
48
+ url = "/profile/withdrawals/#{id}"
49
+ StocksExchangeApiClient.make_api_request_v3(url)
50
+ end
51
+
52
+ def add_withdrawal(currency_id, amount, address, additional_address)
53
+ url = '/profile/withdraw'
54
+ params = {
55
+ 'currency_id' => currency_id,
56
+ 'amount' => amount,
57
+ 'address' => address
58
+ }
59
+ params['additional_address'] = additional_address unless additional_address.nil?
60
+ StocksExchangeApiClient.make_api_request_v3(url, params, :post)
61
+ end
62
+
63
+ def cancel_withdrawal_by_id(id)
64
+ url = "/profile/withdraw/#{id}"
65
+ StocksExchangeApiClient.make_api_request_v3(url, {}, :delete)
66
+ end
67
+
68
+ def reports_orders(params)
69
+ url = '/reports/orders'
70
+ StocksExchangeApiClient.make_api_request_v3(url, params)
71
+ end
72
+
73
+ def reports_orders_by_id(order_id)
74
+ url = "/reports/orders/#{order_id}"
75
+ StocksExchangeApiClient.make_api_request_v3(url, {})
76
+ end
77
+
78
+ def all_trading_orders
79
+ url = '/trading/orders'
80
+ StocksExchangeApiClient.make_api_request_v3(url, {})
81
+ end
82
+
83
+ def delete_all_trading_orders
84
+ url = '/trading/orders'
85
+ StocksExchangeApiClient.make_api_request_v3(url, {}, :delete)
86
+ end
87
+
88
+ def trading_orders_by_pair(currency_pair_id)
89
+ url = "/trading/orders/#{currency_pair_id}"
90
+ StocksExchangeApiClient.make_api_request_v3(url, {})
91
+ end
92
+
93
+ def delete_trading_orders_by_pair(currency_pair_id)
94
+ url = "/trading/orders/#{currency_pair_id}"
95
+ StocksExchangeApiClient.make_api_request_v3(url, {}, :delete)
96
+ end
97
+
98
+ def add_trading_orders_by_pair(currency_pair_id, type, amount, price)
99
+ url = "/trading/orders/#{currency_pair_id}"
100
+ params = {
101
+ 'type' => type,
102
+ 'amount' => amount,
103
+ 'price' => price
104
+ }
105
+ StocksExchangeApiClient.make_api_request_v3(url, params, :post)
106
+ end
107
+
108
+ def trading_order_by_id(order_id)
109
+ url = "/trading/order/#{order_id}"
110
+ StocksExchangeApiClient.make_api_request_v3(url, {})
111
+ end
112
+
113
+ def delete_trading_order_by_id(order_id)
114
+ url = "/trading/order/#{order_id}"
115
+ StocksExchangeApiClient.make_api_request_v3(url, {}, :delete)
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,58 @@
1
+ module StocksExchangeApiClient
2
+ class PublicApiV3
3
+ class << self
4
+ def ping
5
+ StocksExchangeApiClient.make_api_request_v3('/public/ping')
6
+ end
7
+
8
+ def currencies
9
+ StocksExchangeApiClient.make_api_request_v3('/public/currencies')
10
+ end
11
+
12
+ def currencies_by_id(currency_id)
13
+ url = "/public/currencies/#{currency_id}"
14
+ StocksExchangeApiClient.make_api_request_v3(url)
15
+ end
16
+
17
+ def markets
18
+ StocksExchangeApiClient.make_api_request_v3('/public/markets')
19
+ end
20
+
21
+ def pairs_list_by_code(code = 'ALL')
22
+ url = "/public/currency_pairs/list/#{code}"
23
+ StocksExchangeApiClient.make_api_request_v3(url)
24
+ end
25
+
26
+ def pairs_list_by_id(id)
27
+ url = "/public/currency_pairs/#{id}"
28
+ StocksExchangeApiClient.make_api_request_v3(url)
29
+ end
30
+
31
+ def ticker
32
+ StocksExchangeApiClient.make_api_request_v3('/public/ticker')
33
+ end
34
+
35
+ def ticker_by_pair_id(id)
36
+ url = "/public/ticker/#{id}"
37
+ StocksExchangeApiClient.make_api_request_v3(url)
38
+ end
39
+
40
+ def trades(currency_pair_id, params)
41
+ url = "/public/trades/#{currency_pair_id}"
42
+ StocksExchangeApiClient.make_api_request_v3(url, params)
43
+ end
44
+
45
+ def orderbook_by_pair_id(id, params)
46
+ url = "/public/orderbook/#{id}"
47
+ StocksExchangeApiClient.make_api_request_v3(url, params)
48
+ end
49
+
50
+ def chart(currency_pair_id, time_start, time_end, params, candles_type)
51
+ url = "/public/chart/#{currency_pair_id}/#{candles_type.nil? ? '1' : candles_type}"
52
+ params['timeStart'] = time_start
53
+ params['timeEnd'] = time_end
54
+ StocksExchangeApiClient.make_api_request_v3(url, params)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module StocksExchangeApiClient
2
- VERSION = '0.1.2'
2
+ VERSION = '2.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stocks_exchange_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stocks Exchange
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-26 00:00:00.000000000 Z
11
+ date: 2019-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: openssl
@@ -128,7 +128,9 @@ files:
128
128
  - lib/stocks_exchange_api_client/configuration.rb
129
129
  - lib/stocks_exchange_api_client/errors/configuration_error.rb
130
130
  - lib/stocks_exchange_api_client/private.rb
131
+ - lib/stocks_exchange_api_client/private_v3.rb
131
132
  - lib/stocks_exchange_api_client/public.rb
133
+ - lib/stocks_exchange_api_client/public_v3.rb
132
134
  - lib/stocks_exchange_api_client/version.rb
133
135
  - stocks_exchange_api_client.gemspec
134
136
  homepage: https://github.com/StocksExchange/ruby-client#readme