bitfinex-rb 0.0.9 → 0.0.10

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
  SHA1:
3
- metadata.gz: f98705af5686620a50f19b70af4517b27929805f
4
- data.tar.gz: 773bd8c26607e2b97b793e55cec026263db4431a
3
+ metadata.gz: 46ee71e43fbb35d759d014160cc96741cdc47792
4
+ data.tar.gz: ef983747170bef41e16726e5da0861a18f69ae5e
5
5
  SHA512:
6
- metadata.gz: d27cde05902958afbd58a906706f4d2a1bd6b23daffe391d40a4d711f8bca51bc057665668ad621f92788b2f3b33f1540ae2a4908cf4d5599f843654b3f2a54e
7
- data.tar.gz: 7dd15690bbffc2793114a7567bdf3d7a104a940ecd9e4b36689f9eebab49fe67936e63b9496b36155c6543bc21bf56eadf46bfd4d8763844e5de7a2aff6037bd
6
+ metadata.gz: 35c46bb6615f8d165e82962ea07a1a77a33fa643e2fbe272786e0f9fca39ece845506e72d93f664a800d2975625d808296055f81d5eb212702d69ecce2c9a5f6
7
+ data.tar.gz: d6769f99ae45bacfe5aaac84d3a14b00f66f9c16abaa2664f6334bccc8c67a416378526ac27f5c336c463ff8eb0335873d0d6c7602f28c6b4ce16740c25f2cf6
@@ -0,0 +1 @@
1
+ require_relative "./bitfinex.rb"
@@ -20,11 +20,16 @@ module Bitfinex
20
20
  end
21
21
 
22
22
  class Configuration
23
- attr_accessor :api_endpoint, :debug, :debug_connection, :secret, :api_key, :websocket_api_endpoint, :rest_timeout, :rest_open_timeout
23
+ attr_accessor :api_endpoint, :debug, :debug_connection, :secret
24
+ attr_accessor :api_key, :websocket_api_endpoint, :rest_timeout
25
+ attr_accessor :reconnect, :reconnect_after, :rest_open_timeout
26
+
24
27
  def initialize
25
28
  self.api_endpoint = "https://api.bitfinex.com/v1/"
26
- self.websocket_api_endpoint = "wss://api2.bitfinex.com:3000/ws"
29
+ self.websocket_api_endpoint = "wss://api.bitfinex.com/ws"
27
30
  self.debug = false
31
+ self.reconnect = true
32
+ self.reconnect_after = 60
28
33
  self.rest_timeout = 30
29
34
  self.rest_open_timeout = 30
30
35
  self.debug_connection = false
@@ -6,6 +6,7 @@ module Bitfinex
6
6
  class InvalidAuthKeyError < ClientError; end
7
7
  class BlockMissingError < ParamsError; end
8
8
  class ServerError < Exception; end # Error reported back by Binfinex server
9
+ class ConnectionClosed < Exception; end
9
10
  class BadRequestError < ServerError; end
10
11
  class NotFoundError < ServerError; end
11
12
  class ForbiddenError < ServerError; end
@@ -1,3 +1,3 @@
1
1
  module Bitfinex
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -10,7 +10,7 @@ module Bitfinex
10
10
  listen
11
11
  ws_client.run!
12
12
  end
13
-
13
+
14
14
  def ws_send(msg)
15
15
  ws_client.send msg
16
16
  end
@@ -28,7 +28,7 @@ module Bitfinex
28
28
  add_callback(:auth, &block)
29
29
  save_channel_id(:auth, 0)
30
30
  ws_safe_send({
31
- apiKey: config.api_key,
31
+ apiKey: config.api_key,
32
32
  authSig: sign(payload),
33
33
  authPayload: payload,
34
34
  event: 'auth'
@@ -40,8 +40,8 @@ module Bitfinex
40
40
  def ws_unauth
41
41
  ws_safe_send({event: 'unauth'})
42
42
  end
43
-
44
- private
43
+
44
+ private
45
45
 
46
46
  def ws_reset_channels
47
47
  @chan_ids = []
@@ -50,25 +50,30 @@ module Bitfinex
50
50
  end
51
51
 
52
52
  def ws_client
53
- @ws_client ||= WSClient.new(url:config.websocket_api_endpoint)
53
+ options = {
54
+ url: config.websocket_api_endpoint,
55
+ reconnect: config.reconnect,
56
+ reconnect_after: config.reconnect_after
57
+ }
58
+ @ws_client ||= WSClient.new(options)
54
59
  end
55
-
60
+
56
61
  def chan_ids
57
62
  @chan_ids ||= []
58
- end
59
-
63
+ end
64
+
60
65
  def ws_open
61
66
  @ws_open ||= false
62
67
  end
63
-
68
+
64
69
  def ws_registration_messages
65
70
  @ws_registration_messages ||= []
66
71
  end
67
-
72
+
68
73
  def callbacks
69
74
  @callbacks ||= {}
70
75
  end
71
-
76
+
72
77
  def add_callback(channel, &block)
73
78
  callbacks[channel] = { block: block, chan_id: nil }
74
79
  end
@@ -84,7 +89,7 @@ module Bitfinex
84
89
  else
85
90
  ws_registration_messages.push msg
86
91
  end
87
- end
92
+ end
88
93
 
89
94
  def register_channel(msg, &block)
90
95
  add_callback(fingerprint(msg),&block)
@@ -94,38 +99,38 @@ module Bitfinex
94
99
  ws_registration_messages.push msg.merge(event: 'subscribe')
95
100
  end
96
101
  end
97
-
102
+
98
103
  def fingerprint(msg)
99
104
  msg.reject{|k,v| [:event,'chanId','event'].include?(k) }.
100
105
  inject({}){|h, (k,v)| h[k.to_sym]=v.to_s; h}
101
106
  end
102
-
107
+
103
108
  def listen
104
109
  ws_client.on(:message) do |rmsg|
105
110
  msg = JSON.parse(rmsg)
106
111
  if msg.kind_of?(Hash) && msg["event"] == "subscribed"
107
- save_channel_id(fingerprint(msg), msg["chanId"])
112
+ save_channel_id(fingerprint(msg), msg["chanId"])
108
113
  elsif msg.kind_of?(Array)
109
114
  exec_callback_for(msg)
110
115
  end
111
116
  end
112
117
  end
113
-
118
+
114
119
  def save_channel_id(chan,id)
115
120
  callbacks[chan][:chan_id] = id
116
121
  chan_ids[id.to_i] = chan
117
122
  end
118
-
123
+
119
124
  def exec_callback_for(msg)
120
125
  return if msg[1] == 'hb' #ignore heartbeat
121
126
  id = msg[0]
122
127
  callbacks[chan_ids[id.to_i]][:block].call(msg)
123
128
  end
124
-
129
+
125
130
  def subscribe_to_channels
126
- ws_client.on(:open) do
131
+ ws_client.on(:open) do
127
132
  @ws_open = true
128
- ws_registration_messages.each do |msg|
133
+ ws_registration_messages.each do |msg|
129
134
  ws_client.send(msg)
130
135
  end
131
136
  end
@@ -134,15 +139,17 @@ module Bitfinex
134
139
  class WSClient
135
140
  def initialize(options = {})
136
141
  # set some defaults
137
- @url = options[:url] || 'ws://dev2.bitfinex.com:3001/ws'
138
- @reconnect = options[:reconenct] || false
142
+ @url = options[:url] || 'wss://api.bitfinex.com/ws'
143
+ @reconnect = options[:reconnect] || false
144
+ @reconnect_after = options[:reconnect_after] || 30
145
+ @stop = false
139
146
  end
140
-
147
+
141
148
  def on(msg, &blk)
142
149
  ivar = "@#{msg}_cb"
143
150
  instance_variable_set(ivar.to_sym, blk)
144
151
  end
145
-
152
+
146
153
  def run!
147
154
  if EventMachine.reactor_running?
148
155
  connect!
@@ -150,38 +157,51 @@ module Bitfinex
150
157
  EM.run { connect! }
151
158
  end
152
159
  end
153
-
160
+
154
161
  def stop!
162
+ @stop = true
155
163
  @ws.close
156
164
  end
157
-
165
+
158
166
  def connect!
167
+ @stop = false
159
168
  @ws = Faye::WebSocket::Client.new(@url)
160
169
  @ws.onopen = method(:ws_opened)
161
170
  @ws.onmessage = method(:ws_receive)
162
171
  @ws.onclose = method(:ws_closed)
163
172
  @ws.onerror = method(:ws_error)
164
173
  end
165
-
174
+
166
175
  def send(msg)
176
+ raise ConnectionClosed if stopped?
177
+ connect! unless alive?
167
178
  msg = msg.is_a?(Hash) ? msg.to_json : msg
168
179
  @ws.send(msg)
169
180
  end
170
-
181
+
182
+ def alive?
183
+ @ws && @ws.ready_state == Faye::WebSocket::API::OPEN
184
+ end
185
+
186
+ def stopped?
187
+ @stop
188
+ end
189
+
171
190
  private
172
-
191
+
173
192
  def ws_opened(event)
174
193
  @open_cb.call(event) if @open_cb
175
194
  end
176
-
195
+
177
196
  def ws_receive(event)
178
197
  @message_cb.call(event.data) if @message_cb
179
198
  end
180
-
181
- def ws_closed(_event)
182
- EM.stop
183
- end
184
-
199
+
200
+ def ws_closed(event)
201
+ return unless @reconnect
202
+ EM.add_timer(@reconnect_after){ connect! } unless @stop
203
+ end
204
+
185
205
  def ws_error(event)
186
206
  fail event
187
207
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitfinex-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bitfinex
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-27 00:00:00.000000000 Z
11
+ date: 2017-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -131,6 +131,7 @@ executables: []
131
131
  extensions: []
132
132
  extra_rdoc_files: []
133
133
  files:
134
+ - lib/bitfinex-api-rb.rb
134
135
  - lib/bitfinex.rb
135
136
  - lib/bitfinex/account_info.rb
136
137
  - lib/bitfinex/authenticated_rest.rb