Class: Bitstamper::Websocket::Client
- Inherits:
-
Object
- Object
- Bitstamper::Websocket::Client
- Defined in:
- lib/bitstamper/websocket/client.rb
Instance Attribute Summary collapse
-
#application_key ⇒ Object
Returns the value of attribute application_key.
-
#callbacks ⇒ Object
Returns the value of attribute callbacks.
-
#channels ⇒ Object
Returns the value of attribute channels.
-
#enable_logging ⇒ Object
Returns the value of attribute enable_logging.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#subscriptions ⇒ Object
Returns the value of attribute subscriptions.
Instance Method Summary collapse
- #disable_pusher_logging ⇒ Object
- #generate_channels ⇒ Object
-
#initialize(application_key: ::Bitstamper.configuration.pusher_app_key, enable_logging: false) ⇒ Client
constructor
A new instance of Client.
- #loop! ⇒ Object
-
#received(channel, event, data) ⇒ Object
Implement your own callback to customize how data is processed.
- #start!(async: true) ⇒ Object
- #stop! ⇒ Object
- #subscribe!(type, product: nil) ⇒ Object
Constructor Details
#initialize(application_key: ::Bitstamper.configuration.pusher_app_key, enable_logging: false) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bitstamper/websocket/client.rb', line 7 def initialize(application_key: ::Bitstamper.configuration.pusher_app_key, enable_logging: false) self.socket = nil self.application_key = application_key self.enable_logging = enable_logging self.channels = { live_trades: [], order_book: [], diff_order_book: [], live_orders: [] } self.subscriptions = [] self.callbacks = { live_trades: -> (channel, event, data) { received(channel, event, data) }, order_book: -> (channel, event, data) { received(channel, event, data) }, diff_order_book: -> (channel, event, data) { received(channel, event, data) }, live_orders: { order_created: -> (channel, event, data) { received(channel, event, data) }, order_changed: -> (channel, event, data) { received(channel, event, data) }, order_deleted: -> (channel, event, data) { received(channel, event, data) } } } disable_pusher_logging if !self.enable_logging generate_channels end |
Instance Attribute Details
#application_key ⇒ Object
Returns the value of attribute application_key.
4 5 6 |
# File 'lib/bitstamper/websocket/client.rb', line 4 def application_key @application_key end |
#callbacks ⇒ Object
Returns the value of attribute callbacks.
5 6 7 |
# File 'lib/bitstamper/websocket/client.rb', line 5 def callbacks @callbacks end |
#channels ⇒ Object
Returns the value of attribute channels.
5 6 7 |
# File 'lib/bitstamper/websocket/client.rb', line 5 def channels @channels end |
#enable_logging ⇒ Object
Returns the value of attribute enable_logging.
4 5 6 |
# File 'lib/bitstamper/websocket/client.rb', line 4 def enable_logging @enable_logging end |
#socket ⇒ Object
Returns the value of attribute socket.
4 5 6 |
# File 'lib/bitstamper/websocket/client.rb', line 4 def socket @socket end |
#subscriptions ⇒ Object
Returns the value of attribute subscriptions.
5 6 7 |
# File 'lib/bitstamper/websocket/client.rb', line 5 def subscriptions @subscriptions end |
Instance Method Details
#disable_pusher_logging ⇒ Object
38 39 40 |
# File 'lib/bitstamper/websocket/client.rb', line 38 def disable_pusher_logging PusherClient.logger = Logger.new(nil) end |
#generate_channels ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/bitstamper/websocket/client.rb', line 42 def generate_channels ::Bitstamper.configuration.products.each do |product| self.channels[:live_trades] << {name: "live_trades_#{product}", product: product, events: ["trade"]} self.channels[:order_book] << {name: "order_book_#{product}", product: product, events: ["data"]} self.channels[:diff_order_book] << {name: "diff_order_book_#{product}", product: product, events: ["data"]} self.channels[:live_orders] << {name: "live_orders_#{product}", product: product, events: ["order_created", "order_changed", "order_deleted"]} end end |
#loop! ⇒ Object
78 79 80 81 82 |
# File 'lib/bitstamper/websocket/client.rb', line 78 def loop! loop do sleep(1) end end |
#received(channel, event, data) ⇒ Object
Implement your own callback to customize how data is processed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bitstamper/websocket/client.rb', line 85 def received(channel, event, data) parsed = case channel[:name] when /^live_trades/i ::Bitstamper::Models::LiveTrade.new(data.symbolize_keys.merge(product: channel[:product])) when /^order_book/i ::Bitstamper::Models::OrderBook.new(data) when /^diff_order_book/i ::Bitstamper::Models::OrderBook.new(data) when /^live_orders/i ::Bitstamper::Models::Order.new(data.merge(event: event)) end pp parsed end |
#start!(async: true) ⇒ Object
51 52 53 54 |
# File 'lib/bitstamper/websocket/client.rb', line 51 def start!(async: true) self.socket = PusherClient::Socket.new(self.application_key) self.socket.connect(async) end |
#stop! ⇒ Object
56 57 58 |
# File 'lib/bitstamper/websocket/client.rb', line 56 def stop! self.socket.disconnect end |
#subscribe!(type, product: nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bitstamper/websocket/client.rb', line 60 def subscribe!(type, product: nil) chnls = self.channels.fetch(type, []) chnls = chnls&.select { |channel| channel[:product].to_s == product.to_s } if !product.to_s.empty? && product.to_s != "all" chnls.each do |channel| if !self.subscriptions.include?(channel[:name]) self.socket.subscribe(channel[:name]) self.subscriptions << channel[:name] channel[:events].each do |event| self.socket[channel[:name]].bind(event) do |data| ws_received(channel, event, data) end end end end end |