class Alphapoint::WebSocket
Attributes
address[RW]
Public Class Methods
new(address = nil, &block)
click to toggle source
# File lib/alphapoint/web_socket.rb, line 9 def initialize(address = nil, &block) if (Alphapoint.configuration.nil? || Alphapoint.configuration.address.nil? || Alphapoint.configuration.address.empty?) && address.nil? raise AlphapointError, "Pass or configure an address to conect on WebSocket" end @ws = nil @address = address || Alphapoint.configuration.address @nextIValue = 2 @avaliable_functions = [ "GetInstrument", "GetInstruments", "GetProduct", "GetProducts", "SendOrder", "SubscribeLevel1", "WebAuthenticateUser", "GetOrderFee" ] @response = {} @unsub_actions = [] alpha_self = self @thread = Thread.new do EM.run do @ws = Faye::WebSocket::Client.new(@address) @ws.on :open do |event| p [:open, "Websocket connected to #{@address}"] end @ws.on :message do |event| alpha_self.delegate_message(JSON.parse(event.data).with_indifferent_access) end @ws.on :error do |event| p [:error, event.inspect] end @ws.on :close do |event| p [:close, event.code, event.reason] end end end trap(:INT) { EM.stop } trap(:TERM){ EM.stop } while not EM.reactor_running?; end while not EM.defers_finished?; end end
Public Instance Methods
build_request(function_name, payload,type = 0, &block)
click to toggle source
# File lib/alphapoint/web_socket.rb, line 64 def build_request(function_name, payload,type = 0, &block) frame = { 'm': type, 'i': @nextIValue, 'n': function_name, 'o': JSON.generate(payload) } @response[@nextIValue] = block @nextIValue += 2 @ws.send(JSON.generate(frame)) end
delegate_message(data)
click to toggle source
Finds the action responsible for the received message
# File lib/alphapoint/web_socket.rb, line 78 def delegate_message(data) received_action = @response[data['i']] if !received_action.nil? && received_action.is_a?(Proc) received_action.call(JSON.parse(data['o'])) @response[data['i']] = nil else raise "Error: Received message has no correspondent id" end end
get_quotes(payload = { OMSId: 1 }, &block)
click to toggle source
# File lib/alphapoint/web_socket.rb, line 89 def get_quotes(payload = { OMSId: 1 }, &block) wait_handshake quotes = Alphapoint::GetQuotes.new(self) quotes.execute(payload) do |res| block.call(res) end end
method_missing(m, *args, &block)
click to toggle source
# File lib/alphapoint/web_socket.rb, line 99 def method_missing(m, *args, &block) wait_handshake function_name = m.to_s.camelcase respond_action = @avaliable_functions.select{ |func| func == function_name } if respond_action.size > 0 puts "Delegating to action: #{m}" payload = args[0] || {} type = args[1].to_i || 0 build_request(function_name, payload, type) do |response| block.call(response) end else raise "Method #{m} not implemented yet" end end
Private Instance Methods
wait_handshake()
click to toggle source
# File lib/alphapoint/web_socket.rb, line 119 def wait_handshake sleep(0.5) end