Class: Alphapoint::WebSocket
- Inherits:
-
Object
- Object
- Alphapoint::WebSocket
- Defined in:
- lib/alphapoint/web_socket.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
Instance Method Summary collapse
- #build_request(function_name, payload, type = 0, &block) ⇒ Object
-
#delegate_message(data) ⇒ Object
Finds the action responsible for the received message.
- #get_quotes(payload = { OMSId: 1 }, &block) ⇒ Object
-
#initialize(address = nil, &block) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #method_missing(m, *args, &block) ⇒ Object
Constructor Details
#initialize(address = nil, &block) ⇒ WebSocket
Returns a new instance of WebSocket.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 |
# 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.(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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# 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 |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
7 8 9 |
# File 'lib/alphapoint/web_socket.rb', line 7 def address @address end |
Instance Method Details
#build_request(function_name, payload, type = 0, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# 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) ⇒ Object
Finds the action responsible for the received message
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/alphapoint/web_socket.rb', line 78 def (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) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# 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 |