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

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

Overview

Alpaca Realtime API News class

Direct Known Subclasses

Alpaca::News::Api::RealtimeNews

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNews

Create Realtime news object



12
13
14
# File 'lib/alpaca/news/api/realtime/news.rb', line 12

def initialize
  @client = Client.new(Alpaca::News::Api.configure.stream, Alpaca::News::Api.configure.client_options)
end

Class Method Details

.run(news = :all, options = {}, &block) ⇒ Object Also known as: stream, subscribe, watch

Start auth, subscribe with block and unsubscribe within EM block

Parameters:

  • news (String, Symbol, Array<String>) (defaults to: :all)

    List of tickers or :all

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

    Client auth options

Options Hash (options):

  • :key (String)

    Client key

  • :secret (String)

    Client secret



97
98
99
# File 'lib/alpaca/news/api/realtime/news.rb', line 97

def run(news = :all, options = {}, &block)
  run_with_eventmachine(news, options, &block)
end

.run_with_eventmachine(news = :all, options = {}, &block) ⇒ Object

Auth, subscribe with block and unsubscribe within EM block

Parameters:

  • news (String, Symbol, Array<String>) (defaults to: :all)

    List of tickers or :all

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

    Client auth options

Options Hash (options):

  • :key (String)

    Client key

  • :secret (String)

    Client secret



111
112
113
# File 'lib/alpaca/news/api/realtime/news.rb', line 111

def run_with_eventmachine(news = :all, options = {}, &block)
  EventMachine.run { run_without_eventmachine(news, options, &block) }
end

.run_without_eventmachine(news = :all, options = {}, &block) ⇒ Object

Auth, subscribe with block and unsubscribe without EM

Parameters:

  • news (String, Symbol, Array<String>) (defaults to: :all)

    List of tickers or :all

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

    Client auth options

Options Hash (options):

  • :key (String)

    Client key

  • :secret (String)

    Client secret



122
123
124
125
126
# File 'lib/alpaca/news/api/realtime/news.rb', line 122

def run_without_eventmachine(news = :all, options = {}, &block)
  (realtime = new).auth(options)
  realtime.subscribe(news, &block)
  realtime.unsubscribe(news)
end

Instance Method Details

#auth(options = {}) ⇒ Alpaca::News::Api::Realtime::News

Send auth action with key and secret to the websocket server

Parameters:

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

    Client auth options

Options Hash (options):

  • :key (String)

    Client key

  • :secret (String)

    Client secret

Returns:



24
25
26
27
28
29
# File 'lib/alpaca/news/api/realtime/news.rb', line 24

def auth(options = {})
  key = options[:key] || Alpaca::News::Api.configure.key_id
  secret = options[:secret] || Alpaca::News::Api.configure.secret_key
  @client.send(action: 'auth', key: key, secret: secret)
  self
end

#close { ... } ⇒ Alpaca::News::Api::Realtime::News Also known as: on_close

On close callback

Yields:

  • Error event object

Returns:



83
84
85
86
# File 'lib/alpaca/news/api/realtime/news.rb', line 83

def close(&block)
  @client.close(&block)
  self
end

#error {|Faye::WebSocket::API::ErrorEvent| ... } ⇒ Alpaca::News::Api::Realtime::News Also known as: on_error

On error callback

Yields:

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

    Error event object

Returns:



71
72
73
74
# File 'lib/alpaca/news/api/realtime/news.rb', line 71

def error(&block)
  @client.error(&block)
  self
end

#subscribe(news = :all) {|Alpaca::News::Api::Models::News| ... } ⇒ Alpaca::News::Api::Realtime::News

Send subscribe command

Parameters:

  • news (String, Symbol, Array<String>) (defaults to: :all)

    List of tickers or :all

Yields:

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/alpaca/news/api/realtime/news.rb', line 39

def subscribe(news = :all, &block)
  news = '*' if news == :all
  news = news.split(',') if news.is_a?(String)
  @client.send(action: 'subscribe', news: Array(news))
  @client.message do |data|
    objects = JSON.parse(data).map do |item|
      Alpaca::News::Api::Models::News.new(item) if item.delete('T') == 'n'
    end.select { |x| !x.nil? }
    next if objects.empty?
    block.call(Alpaca::News::Api::Models::Event.new(object: objects.first, objects: objects, type: :message))
  end
  self
end

#unsubscribe(news = :all) ⇒ Alpaca::News::Api::Realtime::News

Send unsubscribe command

Parameters:

  • news (String, Symbol, Array<String>) (defaults to: :all)

    List of tickers or :all

Returns:



59
60
61
62
63
# File 'lib/alpaca/news/api/realtime/news.rb', line 59

def unsubscribe(news = :all)
  news = '*' if news == :all
  @client.send(action: 'unsubscribe', news: Array(news))
  self
end