Class: Intrinio::Realtime::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/intrinio-realtime.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/intrinio-realtime.rb', line 27

def initialize(options) 
  raise "Options parameter is required" if options.nil? || !options.is_a?(Hash)

  @api_key = options[:api_key]
  raise "API Key was formatted invalidly." if @api_key && !valid_api_key?(@api_key)

  unless @api_key
    @username = options[:username]
    @password = options[:password]
    raise "API Key or Username and password are required" if @username.nil? || @username.empty? || @password.nil? || @password.empty?
  end

  @provider = options[:provider]
  raise "Provider must be 'CRYPTOQUOTE', 'FXCM', 'IEX', or 'QUODD'" unless PROVIDERS.include?(@provider)

  @channels = []
  @channels = parse_channels(options[:channels]) if options[:channels]
  bad_channels = @channels.select{|x| !x.is_a?(String)}
  raise "Invalid channels to join: #{bad_channels}" unless bad_channels.empty?

  if options[:logger] == false
    @logger = nil 
  elsif !options[:logger].nil?
    @logger = options[:logger]
  else
    @logger = Logger.new($stdout)
    @logger.level = Logger::INFO
  end

  @quotes = EventMachine::Channel.new
  @ready = false
  @joined_channels = []
  @heartbeat_timer = nil
  @selfheal_timer = nil
  @selfheal_backoffs = Array.new(SELF_HEAL_BACKOFFS)
  @ws = nil
end

Instance Method Details

#connectObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/intrinio-realtime.rb', line 102

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

#disconnectObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/intrinio-realtime.rb', line 120

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
  info "Connection closed"
end

#join(*channels) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/intrinio-realtime.rb', line 69

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/intrinio-realtime.rb', line 81

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_allObject



92
93
94
95
96
# File 'lib/intrinio-realtime.rb', line 92

def leave_all
  @channels = []
  debug "Leaving all channels"
  refresh_channels()
end

#on_quote(&b) ⇒ Object



98
99
100
# File 'lib/intrinio-realtime.rb', line 98

def on_quote(&b)
  @quotes.subscribe(&b)
end

#providerObject



65
66
67
# File 'lib/intrinio-realtime.rb', line 65

def provider
  @provider
end