Class: Intrinio::Realtime::Client
- Inherits:
-
Object
- Object
- Intrinio::Realtime::Client
- Defined in:
- lib/intrinio-realtime.rb
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
-
#initialize(options, on_trade, on_quote) ⇒ Client
constructor
A new instance of Client.
- #join(*channels) ⇒ Object
- #leave(*channels) ⇒ Object
- #leave_all ⇒ Object
- #on_quote(on_quote) ⇒ Object
- #on_trade(on_trade) ⇒ Object
- #provider ⇒ Object
Constructor Details
#initialize(options, on_trade, on_quote) ⇒ Client
Returns a new instance of Client.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/intrinio-realtime.rb', line 96 def initialize(, on_trade, on_quote) raise "Options parameter is required" if .nil? || !.is_a?(Hash) @stop = false @messages = Queue.new raise "Unable to create queue." if @messages.nil? @on_trade = on_trade @on_quote = on_quote @api_key = [:api_key] raise "API Key was formatted invalidly." if @api_key && !valid_api_key?(@api_key) unless @api_key @username = [:username] @password = [:password] raise "API Key or Username and password are required" if @username.nil? || @username.empty? || @password.nil? || @password.empty? end @provider = [:provider] unless @provider @provider = REALTIME end raise "Provider must be 'REALTIME' or 'MANUAL'" unless PROVIDERS.include?(@provider) @ip_address = [:ip_address] raise "Missing option ip_address while in MANUAL mode." if @provider == MANUAL and (@ip_address.nil? || @ip_address.empty?) @trades_only = [:trades_only] if @trades_only.nil? @trades_only = false end @thread_quantity = [:threads] unless @thread_quantity @thread_quantity = 4 end @threads = [] @channels = [] @channels = parse_channels([:channels]) if [:channels] bad_channels = @channels.select{|x| !x.is_a?(String)} raise "Invalid channels to join: #{bad_channels}" unless bad_channels.empty? if [:logger] == false @logger = nil elsif ![:logger].nil? @logger = [:logger] else @logger = Logger.new($stdout) @logger.level = Logger::INFO end @ready = false @joined_channels = [] @heartbeat_timer = nil @selfheal_timer = nil @selfheal_backoffs = Array.new(SELF_HEAL_BACKOFFS) @ws = nil end |
Instance Method Details
#connect ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/intrinio-realtime.rb', line 189 def connect raise "Must be run from within an EventMachine run loop" unless EM.reactor_running? return warn("Already connected!") if @ready debug "Connecting..." catch :fatal do begin @closing = false @ready = false refresh_token() refresh_websocket() rescue StandardError => e error("Connection error: #{e} \n#{e.backtrace.join("\n")}") try_self_heal() end end end |
#disconnect ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/intrinio-realtime.rb', line 207 def disconnect EM.cancel_timer(@heartbeat_timer) if @heartbeat_timer EM.cancel_timer(@selfheal_timer) if @selfheal_timer @ready = false @closing = true @channels = [] @joined_channels = [] @ws.close() if @ws @stop = true sleep(2) @threads.each { |thread| if !thread.nil? && (!thread.pending_interrupt? || thread.status == "run" || thread.status == "Sleeping") then thread.join(7) elsif !thread.nil? then thread.kill end } @threads = [] @stop = false info "Connection closed" end |
#join(*channels) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/intrinio-realtime.rb', line 160 def join(*channels) channels = parse_channels(channels) nonconforming = channels.select{|x| !x.is_a?(String)} return error("Invalid channels to join: #{nonconforming}") unless nonconforming.empty? @channels.concat(channels) @channels.uniq! debug "Joining channels #{channels}" refresh_channels() end |
#leave(*channels) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/intrinio-realtime.rb', line 172 def leave(*channels) channels = parse_channels(channels) nonconforming = channels.find{|x| !x.is_a?(String)} return error("Invalid channels to leave: #{nonconforming}") unless nonconforming.empty? channels.each{|c| @channels.delete(c)} debug "Leaving channels #{channels}" refresh_channels() end |
#leave_all ⇒ Object
183 184 185 186 187 |
# File 'lib/intrinio-realtime.rb', line 183 def leave_all @channels = [] debug "Leaving all channels" refresh_channels() end |
#on_quote(on_quote) ⇒ Object
233 234 235 |
# File 'lib/intrinio-realtime.rb', line 233 def on_quote(on_quote) @on_quote = on_quote end |
#on_trade(on_trade) ⇒ Object
229 230 231 |
# File 'lib/intrinio-realtime.rb', line 229 def on_trade(on_trade) @on_trade = on_trade end |
#provider ⇒ Object
156 157 158 |
# File 'lib/intrinio-realtime.rb', line 156 def provider @provider end |