Class: IB::Messages::Outgoing::AbstractMessage

Inherits:
AbstractMessage show all
Defined in:
lib/ib/messages/outgoing/abstract_message.rb

Instance Attribute Summary

Attributes inherited from AbstractMessage

#created_at, #data

Instance Method Summary collapse

Methods inherited from AbstractMessage

data_map, message_id, #message_id, message_type, #message_type, properties?, #request_id, #to_human, version

Constructor Details

#initialize(data = {}) ⇒ AbstractMessage

Returns a new instance of AbstractMessage.



12
13
14
15
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 12

def initialize data={}
  @data = data
  @created_at = Time.now
end

Instance Method Details

#encodeObject

If the version is zero, omit its apperance (for historical data)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 52

def encode
					## create a proper request_id  and erase :id and :ticker_id if nessesary
					if self.class.properties?.include?(:request_id) 
						@data[:request_id] = if  @data[:request_id].blank? && @data[:ticker_id].blank? && @data[:id].blank?
									 rand(9999)  
								 else
									 @data[:id] || @data[:ticker_id] || @data[:request_id] 
								 end
						@data[:id] = @data[:ticker_id] = nil
					end
 [
	   self.class.version.zero? ? self.class.message_id : [ self.class.message_id, self.class.version ],
   @data[:id] || @data[:ticker_id] ||# @data[:request_id] ||  # id, ticker_id, local_id, order_id
   @data[:local_id] || @data[:order_id] || [],								# do not appear in data_map
   self.class.data_map.map do |(field, default_method, args)| # but request_id does
     case
     when default_method.nil?
       @data[field]
	
     when default_method.is_a?(Symbol) # method name with args
       @data[field].send default_method, *args 

     when default_method.respond_to?(:call) # callable with args
       default_method.call @data[field], *args

     else # default
       @data[field].nil? ? default_method : @data[field] # may be false still
     end
   end
   ]
  # TWS wants to receive booleans as 1 or 0
end

#preprocessObject

Pre-process encoded message Array before sending into socket, such as changing booleans into 0/1 and stuff



41
42
43
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 41

def preprocess
  self.encode.flatten.map {|data| data == true ? 1 : data == false ? 0 : data }
end

#send_to(socket) ⇒ Object

This causes the message to send itself over the server socket in server. “server” is the @server instance variable from the IB object. You can also use this to e.g. get the server version number.

Subclasses can either override this method for precise control over how stuff gets sent to the server, or else define a method encode() that returns an Array of elements that ought to be sent to the server by calling to_s on each one and postpending a ‘0’.



26
27
28
29
30
31
32
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 26

def send_to socket
	  ### debugging of outgoing Messages
	#  puts "------sendto ---------(debugging output in outgoing/abstract_message)" 
	#   puts socket.prepare_message( self.preprocess).inspect.split('\x00')[3..-1].inspect
	#  puts "------sendto ---------"
  socket.send_messages self.preprocess #.each {|data| socket.write_data data}
end

#to_sObject

Same message representation as logged by TWS into API messages log file



35
36
37
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 35

def to_s
  self.preprocess.join('-')
end