binance 0.1.0 → 0.2.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: 3b852561bdc9a9eb605328a725709a5fece13fa1
4
- data.tar.gz: ceb6ce5f24ee3d5669e3983d8142f89d0868d82f
3
+ metadata.gz: 4e8027ac747e3eb75ffc8a423995838465231899
4
+ data.tar.gz: 7ef223ce562fe69837f2ee6538f00af35a4358cf
5
5
  SHA512:
6
- metadata.gz: 16a0f30a732bd4706542ec6c3c98fb72605b01dd1856a1b606489c816d5b25f666cda95473d36450c6d2e0ea5074b987fe0b70c13d9e41ffadb7edb7d87e1834
7
- data.tar.gz: 8e3552354be204f4b55fdf825dfa7486d1464bae2308c2bec00d7c5b4b36db5ea46027e533ae9e76baf87947d7780c91e6bb590e3a95f78e4fb8abb44b80653c
6
+ metadata.gz: cf06724334d5d49f968c2f9658ea506689f390e89fc175fae7bf074271ada8b09c328d92c8588c0c5cba02e05000bf15d032efa2d94780675f076e2c7bdf27df
7
+ data.tar.gz: 6678caae06e9e9ebd40c162c003deacf313a99d52d493db2bf49f5d861f106795a1f56fff2a66fa28c2ce0e9ce3b32ff586fd9e4ac8460321b50989800172327
@@ -1,5 +1,6 @@
1
1
 
2
2
  require 'faraday'
3
+ require_relative 'rest/api_endpoints'
3
4
  require_relative 'rest/public_api'
4
5
  require_relative 'rest/account_api'
5
6
  require_relative 'rest/withdraw_api'
@@ -10,13 +11,32 @@ module Binance
10
11
  class REST
11
12
  BASE_URL = 'https://www.binance.com'.freeze
12
13
 
13
- include REST::Public_API
14
- include REST::Account_API
15
- include REST::Withdraw_API
14
+ # Gets populated by the different APIs that get extended by the instances
15
+ @api = {}
16
+
17
+ class << self
18
+ attr_accessor :api
19
+ end
20
+
21
+ attr_reader :api_key, :secret_key
16
22
 
17
23
  def initialize(api_key: '', secret_key: '')
18
24
  @api_key = api_key
19
25
  @secret_key = secret_key
26
+
27
+ extend Public_API
28
+ extend Account_API
29
+ extend Withdraw_API
30
+ end
31
+
32
+ def request(api, method, endpoint, options = {})
33
+ conn = REST.api[api].call
34
+ response = conn.send(method) do |req|
35
+ req.url API_ENDPOINTS[endpoint]
36
+ req.params.merge! options
37
+ end
38
+
39
+ response.body
20
40
  end
21
41
 
22
42
  def self.add_query_param(query, key, value)
@@ -9,71 +9,49 @@ module Binance
9
9
  class REST
10
10
  # API endpoints that require a timestamp and signature
11
11
  module Account_API
12
- def account_api
13
- Faraday.new(url: "#{BASE_URL}/api/v3") do |conn|
14
- conn.request :json
15
- conn.response :json, content_type: /\bjson$/
16
- conn.headers['X-MBX-APIKEY'] = @api_key
17
- conn.use TimestampRequestMiddleware
18
- conn.use SignRequestMiddleware, @secret_key
19
- conn.adapter Faraday.default_adapter
12
+ def self.extended(base)
13
+ REST.api[:account] = lambda do
14
+ Faraday.new(url: "#{BASE_URL}/api") do |conn|
15
+ conn.request :json
16
+ conn.response :json, content_type: /\bjson$/
17
+ conn.headers['X-MBX-APIKEY'] = base.api_key
18
+ conn.use TimestampRequestMiddleware
19
+ conn.use SignRequestMiddleware, base.secret_key
20
+ conn.adapter Faraday.default_adapter
21
+ end
20
22
  end
21
23
  end
22
24
 
23
25
  def create_order(options)
24
- response = account_api.post do |req|
25
- req.url 'order'
26
- req.params.merge! options
27
- end
28
- response.body
26
+ request :account, :post, 'order', options
27
+ end
28
+
29
+ def create_test_order(options)
30
+ request :account, :post, 'order/test', options
29
31
  end
30
32
 
31
33
  def query_order(options)
32
- response = account_api.get do |req|
33
- req.url 'order'
34
- req.params.merge! options
35
- end
36
- response.body
34
+ request :account, :get, 'order', options
37
35
  end
38
36
 
39
37
  def cancel_order(options)
40
- response = account_api.delete do |req|
41
- req.url 'order'
42
- req.params.merge! options
43
- end
44
- response.body
38
+ request :account, :delete, 'order', options
45
39
  end
46
40
 
47
41
  def open_orders(options)
48
- response = account_api.get do |req|
49
- req.url 'openOrders'
50
- req.params.merge! options
51
- end
52
- response.body
42
+ request :account, :get, 'openOrders', options
53
43
  end
54
44
 
55
45
  def all_orders(options)
56
- response = account_api.get do |req|
57
- req.url 'allOrders'
58
- req.params.merge! options
59
- end
60
- response.body
46
+ request :account, :get, 'allOrders', options
61
47
  end
62
48
 
63
49
  def account_info(options = {})
64
- response = account_api.get do |req|
65
- req.url 'account'
66
- req.params.merge! options
67
- end
68
- response.body
50
+ request :account, :get, 'account', options
69
51
  end
70
52
 
71
53
  def account_trade_list(options)
72
- response = account_api.get do |req|
73
- req.url 'myTrades'
74
- req.params.merge! options
75
- end
76
- response.body
54
+ request :account, :get, 'myTrades', options
77
55
  end
78
56
  end
79
57
  end
@@ -0,0 +1,36 @@
1
+
2
+ module Binance
3
+ module Client
4
+ class REST
5
+ API_ENDPOINTS = {
6
+ # Public API Endpoints
7
+ 'ping' => 'v1/ping',
8
+ 'time' => 'v1/time',
9
+ 'exchangeInfo' => 'v1/exchangeInfo',
10
+ 'products' => '/exchange/public/products',
11
+ 'depth' => 'v1/depth',
12
+ 'trades' => 'v1/trades',
13
+ 'historicalTrades' => 'v1/historicalTrades',
14
+ 'aggTrades' => 'v1/aggTrades',
15
+ 'klines' => 'v1/klines',
16
+ '24hr' => 'v1/ticker/24hr',
17
+ 'price' => 'v3/ticker/price',
18
+ 'bookTicker' => 'v3/ticker/bookTicker',
19
+
20
+ # Account API Endpoints
21
+ 'order' => 'v3/order',
22
+ 'order/test' => 'v3/order/test',
23
+ 'openOrders' => 'v3/openOrders',
24
+ 'allOrders' => 'v3/allOrders',
25
+ 'account' => 'v3/account',
26
+ 'myTrades' => 'v3/myTrades',
27
+
28
+ # Withdraw API Endpoints
29
+ 'withdraw' => 'v3/withdraw.html',
30
+ 'depositHistory' => 'v3/depositHistory.html',
31
+ 'withdrawHistory' => 'v3/withdrawHistory.html',
32
+ 'depositAddress' => 'v3/depositAddress.html'
33
+ }.freeze
34
+ end
35
+ end
36
+ end
@@ -7,68 +7,72 @@ module Binance
7
7
  class REST
8
8
  # API endpoints that don't require any type of authentication
9
9
  module Public_API
10
- def public_api
11
- Faraday.new(url: "#{BASE_URL}/api/v1") do |conn|
12
- conn.request :json
13
- conn.response :json, content_type: /\bjson$/
14
- conn.adapter Faraday.default_adapter
10
+ def self.extended(_base)
11
+ REST.api[:public] = lambda do
12
+ Faraday.new(url: "#{BASE_URL}/api") do |conn|
13
+ conn.request :json
14
+ conn.response :json, content_type: /\bjson$/
15
+ conn.adapter Faraday.default_adapter
16
+ end
15
17
  end
16
18
  end
17
19
 
18
20
  def ping
19
- public_api.get('ping').body
21
+ request :public, :get, 'ping'
20
22
  end
21
23
 
22
24
  def time
23
- public_api.get('time').body
25
+ request :public, :get, 'time'
24
26
  end
25
27
 
26
28
  def exchange_info
27
- public_api.get('exchangeInfo').body
29
+ request :public, :get, 'exchangeInfo'
28
30
  end
29
31
 
30
32
  def products
31
- public_api.get('/exchange/public/product').body
33
+ request :public, :get, 'products'
32
34
  end
33
35
 
34
36
  def depth(options)
35
- response = public_api.get do |req|
36
- req.url 'depth'
37
- req.params.merge! options
38
- end
39
- response.body
37
+ request :public, :get, 'depth', options
38
+ end
39
+
40
+ def trades(options)
41
+ request :public, :get, 'trades', options
42
+ end
43
+
44
+ def historical_trades(options)
45
+ request :public, :get, 'historicalTrades', options
40
46
  end
41
47
 
42
48
  def agg_trades(options)
43
- response = public_api.get do |req|
44
- req.url 'aggTrades'
45
- req.params.merge! options
46
- end
47
- response.body
49
+ request :public, :get, 'aggTrades', options
48
50
  end
49
51
 
50
52
  def klines(options)
51
- response = public_api.get do |req|
52
- req.url 'klines'
53
- req.params.merge! options
54
- end
55
- response.body
53
+ request :public, :get, 'klines', options
56
54
  end
57
55
 
58
56
  def twenty_four_hour(options)
59
- response = public_api.get do |req|
60
- req.url 'ticker/24hr'
61
- req.params.merge! options
62
- end
63
- response.body
57
+ request :public, :get, '24hr', options
58
+ end
59
+
60
+ def price(options)
61
+ request :public, :get, 'price', options
64
62
  end
65
63
 
64
+ # Ensure backwards compatibility
66
65
  def all_prices
67
- public_api.get('ticker/allPrices').body
66
+ request :public, :get, 'price'
67
+ end
68
+
69
+ def book_ticker(options)
70
+ request :public, :get, 'bookTicker', options
68
71
  end
69
72
 
73
+ # Ensure backwards compatibility
70
74
  def all_book_tickers
71
- public_api.get('ticker/allBookTickers').body
75
+ request :public, :get, 'bookTicker'
72
76
  end
73
77
  end
74
78
  end
@@ -10,47 +10,33 @@ module Binance
10
10
  # API endpoints with require a timestamp and signature,
11
11
  # as well as requiring url_encoded parameters
12
12
  module Withdraw_API
13
- def withdraw_api
14
- Faraday.new(url: "#{BASE_URL}/wapi/v3") do |conn|
15
- conn.request :url_encoded
16
- conn.response :json, content_type: /\bjson$/
17
- conn.headers['X-MBX-APIKEY'] = @api_key
18
- conn.use TimestampRequestMiddleware
19
- conn.use SignRequestMiddleware, @secret_key
20
- conn.adapter Faraday.default_adapter
13
+ def self.extended(base)
14
+ REST.api[:withdraw] = lambda do
15
+ Faraday.new(url: "#{BASE_URL}/wapi") do |conn|
16
+ conn.request :url_encoded
17
+ conn.response :json, content_type: /\bjson$/
18
+ conn.headers['X-MBX-APIKEY'] = base.api_key
19
+ conn.use TimestampRequestMiddleware
20
+ conn.use SignRequestMiddleware, base.secret_key
21
+ conn.adapter Faraday.default_adapter
22
+ end
21
23
  end
22
24
  end
23
25
 
24
26
  def withdraw(options)
25
- response = withdraw_api.post do |req|
26
- req.url 'withdraw.html'
27
- req.params.merge! options
28
- end
29
- response.body
27
+ request :withdraw, :post, 'withdraw', options
30
28
  end
31
29
 
32
30
  def deposit_history(options = {})
33
- response = withdraw_api.get do |req|
34
- req.url 'depositHistory.html'
35
- req.params.merge! options
36
- end
37
- response.body
31
+ request :withdraw, :get, 'depositHistory', options
38
32
  end
39
33
 
40
34
  def withdraw_history(options = {})
41
- response = withdraw_api.get do |req|
42
- req.url 'withdrawHistory.html'
43
- req.params.merge! options
44
- end
45
- response.body
35
+ request :withdraw, :get, 'withdrawHistory', options
46
36
  end
47
37
 
48
38
  def deposit_address(options)
49
- response = withdraw_api.get do |req|
50
- req.url 'depositAddress.html'
51
- req.params.merge! options
52
- end
53
- response.body
39
+ request :withdraw, :get, 'depositAddress', options
54
40
  end
55
41
  end
56
42
  end
@@ -1,4 +1,4 @@
1
-
2
- module Binance
3
- VERSION = '0.1.0'.freeze
4
- end
1
+
2
+ module Binance
3
+ VERSION = '0.2.0'.freeze
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Ray Shisler III
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-28 00:00:00.000000000 Z
11
+ date: 2017-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,6 +92,7 @@ files:
92
92
  - lib/binance.rb
93
93
  - lib/binance/client/rest.rb
94
94
  - lib/binance/client/rest/account_api.rb
95
+ - lib/binance/client/rest/api_endpoints.rb
95
96
  - lib/binance/client/rest/public_api.rb
96
97
  - lib/binance/client/rest/sign_request_middleware.rb
97
98
  - lib/binance/client/rest/timestamp_request_middleware.rb