Class: Alpaca::News::Api::Realtime::News
- Inherits:
-
Object
- Object
- Alpaca::News::Api::Realtime::News
- Defined in:
- lib/alpaca/news/api/realtime/news.rb
Overview
Alpaca Realtime API News class
Direct Known Subclasses
Class Method Summary collapse
-
.run(news = :all, options = {}, &block) ⇒ Object
(also: stream, subscribe, watch)
Start auth, subscribe with block and unsubscribe within EM block.
-
.run_with_eventmachine(news = :all, options = {}, &block) ⇒ Object
Auth, subscribe with block and unsubscribe within EM block.
-
.run_without_eventmachine(news = :all, options = {}, &block) ⇒ Object
Auth, subscribe with block and unsubscribe without EM.
Instance Method Summary collapse
-
#auth(options = {}) ⇒ Alpaca::News::Api::Realtime::News
Send auth action with key and secret to the websocket server.
-
#close { ... } ⇒ Alpaca::News::Api::Realtime::News
(also: #on_close)
On close callback.
-
#error {|Faye::WebSocket::API::ErrorEvent| ... } ⇒ Alpaca::News::Api::Realtime::News
(also: #on_error)
On error callback.
-
#initialize ⇒ News
constructor
Create Realtime news object.
-
#subscribe(news = :all) {|Alpaca::News::Api::Models::News| ... } ⇒ Alpaca::News::Api::Realtime::News
Send subscribe command.
-
#unsubscribe(news = :all) ⇒ Alpaca::News::Api::Realtime::News
Send unsubscribe command.
Constructor Details
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
97 98 99 |
# File 'lib/alpaca/news/api/realtime/news.rb', line 97 def run(news = :all, = {}, &block) run_with_eventmachine(news, , &block) end |
.run_with_eventmachine(news = :all, options = {}, &block) ⇒ Object
Auth, subscribe with block and unsubscribe within EM block
111 112 113 |
# File 'lib/alpaca/news/api/realtime/news.rb', line 111 def run_with_eventmachine(news = :all, = {}, &block) EventMachine.run { run_without_eventmachine(news, , &block) } end |
.run_without_eventmachine(news = :all, options = {}, &block) ⇒ Object
Auth, subscribe with block and unsubscribe without EM
122 123 124 125 126 |
# File 'lib/alpaca/news/api/realtime/news.rb', line 122 def run_without_eventmachine(news = :all, = {}, &block) (realtime = new).auth() 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
24 25 26 27 28 29 |
# File 'lib/alpaca/news/api/realtime/news.rb', line 24 def auth( = {}) key = [:key] || Alpaca::News::Api.configure.key_id secret = [: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
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
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
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. 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
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 |