Class: Alpaca::News::Api::Realtime::Client

Inherits:
Base
  • Object
show all
Defined in:
lib/alpaca/news/api/realtime/client.rb

Overview

Realtime client class

Instance Method Summary collapse

Methods inherited from Base

#client

Constructor Details

#initialize(host = nil, options = {}) ⇒ Client

Create new client object

Parameters:

  • host (String) (defaults to: nil)

    Client host

  • options (Hash) (defaults to: {})

    Client options



15
16
17
# File 'lib/alpaca/news/api/realtime/client.rb', line 15

def initialize(host = nil, options = {})
  @websocket = Faye::WebSocket::Client.new("wss://#{host}/v1beta1/news", [], options)
end

Instance Method Details

#close {|Faye::WebSocket::API::Event| ... } ⇒ Object Also known as: on_close

Websocket close callback

Yields:

  • (Faye::WebSocket::API::Event)

    Event object



49
50
51
# File 'lib/alpaca/news/api/realtime/client.rb', line 49

def close(&block)
  @websocket.on(:close, &block)
end

#error {|Faye::WebSocket::API::ErrorEvent| ... } ⇒ Object Also known as: on_error

Websocket message callback

Yields:

  • (Faye::WebSocket::API::ErrorEvent)

    Error event object



40
41
42
# File 'lib/alpaca/news/api/realtime/client.rb', line 40

def error(&block)
  @websocket.on(:error, &block)
end

#message {|Hash| ... } ⇒ Object Also known as: on_message

Websocket message callback

Yields:

  • (Hash)

    Message data



29
30
31
32
33
# File 'lib/alpaca/news/api/realtime/client.rb', line 29

def message(&block)
  @websocket.on(:message) do |event|
    block.call(event.data)
  end
end

#send(payload = {}) ⇒ Object

Send payload to the websocket server



21
22
23
# File 'lib/alpaca/news/api/realtime/client.rb', line 21

def send(payload = {})
  @websocket.send(payload.to_json)
end