Class: IB::Messages::Outgoing::AbstractMessage
- Inherits:
-
AbstractMessage
- Object
- AbstractMessage
- IB::Messages::Outgoing::AbstractMessage
- Defined in:
- lib/ib/messages/outgoing/abstract_message.rb
Instance Attribute Summary
Attributes inherited from AbstractMessage
Instance Method Summary collapse
-
#encode ⇒ Object
Encode message content into (possibly, nested) Array of values.
-
#initialize(data = {}) ⇒ AbstractMessage
constructor
A new instance of AbstractMessage.
-
#preprocess ⇒ Object
Pre-process encoded message Array before sending into socket, such as changing booleans into 0/1 and stuff.
-
#send_to(socket) ⇒ Object
This causes the message to send itself over the server socket in server.
-
#to_s ⇒ Object
Same message representation as logged by TWS into API messages log file.
Methods inherited from AbstractMessage
data_map, message_id, #message_id, message_type, #message_type, #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
#encode ⇒ Object
Encode message content into (possibly, nested) Array of values. At minimum, encoded Outgoing message contains message_id and version. Most messages also contain (ticker, request or order) :id. Then, content of @data Hash is encoded per instructions in data_map. This method may be modified by message subclasses!
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 46 def encode [self.class., self.class.version, @data[:id] || @data[:ticker_id] || @data[:request_id] || @data[:local_id] || @data[:order_id] || [], self.class.data_map.map do |(field, default_method, args)| 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 |
#preprocess ⇒ Object
Pre-process encoded message Array before sending into socket, such as changing booleans into 0/1 and stuff
37 38 39 |
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 37 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 |
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 26 def send_to socket self.preprocess.each {|data| socket.write_data data} end |
#to_s ⇒ Object
Same message representation as logged by TWS into API messages log file
31 32 33 |
# File 'lib/ib/messages/outgoing/abstract_message.rb', line 31 def to_s self.preprocess.join('-') end |