binance-ruby 1.1.2 → 1.2

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
  SHA256:
3
- metadata.gz: c0b8a2d2b8699dd1ab11f5c4927f492f1083178f6c490b2f128bec3e17519142
4
- data.tar.gz: d0f3a90df602d2218abb7a479ee239b25c45a3c556350cd1a044d0fff2e1d123
3
+ metadata.gz: 8ce807af0caee4eca993399b1ae95a56a6b55af597be3d08581724d717f81d62
4
+ data.tar.gz: 32668b0a998b6cad851be4ba03b0f9299721a6d8ae1a48f1eec93b674ca2d47e
5
5
  SHA512:
6
- metadata.gz: 7e79efbfe09d9046dbec1de668d00e34811170eb5fa323f4b1999bc5586f960d9f7790093e753b861f6c2b7cf0af687cb3b12f3afa020a99d67426930dbec331
7
- data.tar.gz: 16cfb386ab4ea3bfd87067b3cffbeca50145b7108453bff5edf95b84a1fe90a0f2a7caf21e8927b233624c86263f7fe8c6df3f691d10b4fffa628f20445d5089
6
+ metadata.gz: 93b84cfdcf54bf21f114d6f5b11cc993dccebfa61f67b0e49c79197eefe57744d4ce448196deed94a74d025718d34f1f078c74eb0142d6ff5cf8a33179eb733f
7
+ data.tar.gz: 5c0c6caf1f7d8dd8a2be8771ae8bdba8f671dc1fb425685c23dba324f4e3daa9a58269fe6b1ad17f923ab920a397e45e3b250e765ee51a4a4df4735a8cd55559
data/lib/binance-ruby.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "active_support/core_ext/string"
2
2
  require "awrence"
3
3
  require "httparty"
4
+ require "faye/websocket"
4
5
 
5
6
  require "binance/api"
6
7
  require "binance/api/account"
@@ -13,3 +14,4 @@ require "binance/api/margin/order"
13
14
  require "binance/api/order"
14
15
  require "binance/api/request"
15
16
  require "binance/api/version"
17
+ require "binance/websocket"
@@ -0,0 +1,46 @@
1
+ module Binance
2
+ module Api
3
+ class MarketStream
4
+ class << self
5
+ def connect(api_key: nil, api_secret_key: nil)
6
+ EM.run {
7
+ ws = Faye::WebSocket::Client.new("wss://stream.binance.com:9443/stream?streams=<streamName1>/<streamName2>/<streamName3>")
8
+
9
+ ws.on :open do |event|
10
+ p [:open]
11
+ ws.send("Hello, world!")
12
+ end
13
+
14
+ ws.on :message do |event|
15
+ p [:message, event.data]
16
+ end
17
+
18
+ ws.on :close do |event|
19
+ p [:close, event.code, event.reason]
20
+ ws = nil
21
+ end
22
+ }
23
+ end
24
+
25
+ # It's recommended to send a ping about every 30 minutes.
26
+ def keepalive!(listen_key: nil, api_key: nil, api_secret_key: nil)
27
+ raise Error.new(message: "listen_key is required") if listen_key.nil?
28
+ Request.send!(api_key_type: :none, method: :put, path: "/api/v1/userDataStream",
29
+ params: { listenKey: listen_key }, security_type: :user_stream)
30
+ end
31
+
32
+ def start!(api_key: nil, api_secret_key: nil)
33
+ Request.send!(api_key_type: :none, method: :post, path: "/api/v1/userDataStream",
34
+ security_type: :user_stream, api_key: api_key, api_secret_key: api_secret_key)
35
+ end
36
+
37
+ def stop!(listen_key: nil, api_key: nil, api_secret_key: nil)
38
+ raise Error.new(message: "listen_key is required") if listen_key.nil?
39
+ Request.send!(api_key_type: :none, method: :delete, path: "/api/v1/userDataStream",
40
+ params: { listenKey: listen_key }, security_type: :user_stream,
41
+ api_key: api_key, api_secret_key: api_secret_key)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,5 @@
1
1
  module Binance
2
2
  module Api
3
- VERSION = "1.1.2"
3
+ VERSION = "1.2"
4
4
  end
5
5
  end
@@ -0,0 +1,89 @@
1
+ module Binance
2
+ class WebSocket < Faye::WebSocket::Client
3
+ def initialize
4
+ super "wss://stream.binance.com:9443/stream", nil, ping: 180
5
+
6
+ @request_id_inc = 0
7
+
8
+ on :open do |event|
9
+ p [:open]
10
+ end
11
+
12
+ on :message do |event|
13
+ process_data(event.data)
14
+ end
15
+
16
+ on :close do |event|
17
+ p [:close, event.code, event.reason]
18
+ end
19
+ end
20
+
21
+ # stream name: <symbol>@kline_<interval>
22
+ #
23
+ # {
24
+ # "e": "kline", // Event type
25
+ # "E": 123456789, // Event time
26
+ # "s": "BNBBTC", // Symbol
27
+ # "k": {
28
+ # "t": 123400000, // Kline start time
29
+ # "T": 123460000, // Kline close time
30
+ # "s": "BNBBTC", // Symbol
31
+ # "i": "1m", // Interval
32
+ # "f": 100, // First trade ID
33
+ # "L": 200, // Last trade ID
34
+ # "o": "0.0010", // Open price
35
+ # "c": "0.0020", // Close price
36
+ # "h": "0.0025", // High price
37
+ # "l": "0.0015", // Low price
38
+ # "v": "1000", // Base asset volume
39
+ # "n": 100, // Number of trades
40
+ # "x": false, // Is this kline closed?
41
+ # "q": "1.0000", // Quote asset volume
42
+ # "V": "500", // Taker buy base asset volume
43
+ # "Q": "0.500", // Taker buy quote asset volume
44
+ # "B": "123456" // Ignore
45
+ # }
46
+ # }
47
+ def candlesticks!(symbols, interval, &on_receive)
48
+ symbols_fmt = symbols.is_a?(String) ? [symbols] : symbols
49
+ @candlesticks_handler = on_receive
50
+ subscribe(symbols_fmt.map { |s| "#{s.downcase}@kline_#{interval}" })
51
+ end
52
+
53
+ private
54
+
55
+ def process_data(data)
56
+ json = JSON.parse(data, symbolize_names: true)
57
+ if json[:error]
58
+ raise Error.new("(#{json[:code]}) #{json[:msg]}")
59
+ elsif json.key?(:result)
60
+ # Binance stream connected successfully
61
+ else
62
+ case json[:data][:e]&.to_sym
63
+ when :kline
64
+ @candlesticks_handler&.call(json[:stream], json[:data])
65
+ end
66
+ end
67
+ end
68
+
69
+ def request_id
70
+ @request_id_inc += 1
71
+ end
72
+
73
+ def subscribe(streams)
74
+ send({
75
+ method: "SUBSCRIBE",
76
+ params: streams,
77
+ id: request_id,
78
+ }.to_json)
79
+ end
80
+
81
+ def unsubscribe(streams)
82
+ send({
83
+ method: "UNSUBSCRIBE",
84
+ params: streams,
85
+ id: request_id,
86
+ }.to_json)
87
+ end
88
+ end
89
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binance-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Peterson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-28 00:00:00.000000000 Z
11
+ date: 2021-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov
@@ -164,6 +164,34 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0.15'
167
+ - !ruby/object:Gem::Dependency
168
+ name: faye-websocket
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.11'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.11'
181
+ - !ruby/object:Gem::Dependency
182
+ name: eventmachine
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '1.2'
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '1.2'
167
195
  description: Ruby wrapper for the Binance API.
168
196
  email:
169
197
  - hello@jakenberg.io
@@ -180,9 +208,11 @@ files:
180
208
  - lib/binance/api/margin.rb
181
209
  - lib/binance/api/margin/account.rb
182
210
  - lib/binance/api/margin/order.rb
211
+ - lib/binance/api/market_stream.rb
183
212
  - lib/binance/api/order.rb
184
213
  - lib/binance/api/request.rb
185
214
  - lib/binance/api/version.rb
215
+ - lib/binance/websocket.rb
186
216
  homepage: https://github.com/jakenberg/binance-ruby
187
217
  licenses:
188
218
  - MIT
@@ -206,5 +236,5 @@ requirements: []
206
236
  rubygems_version: 3.1.6
207
237
  signing_key:
208
238
  specification_version: 4
209
- summary: binance-ruby-1.1.2
239
+ summary: binance-ruby-1.2
210
240
  test_files: []