Class: Fix::Protocol::Message

Inherits:
MessagePart show all
Defined in:
lib/fix/protocol/message.rb

Overview

Represents an instance of a FIX message

Constant Summary collapse

DEFAULT_VERSION =

Default version for when we do not specify anything

'FIX.4.4'
@@expected_version =
DEFAULT_VERSION

Instance Attribute Summary

Attributes inherited from MessagePart

#delegations, #name, #parse_failure

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MessagePart

collection, field, inherited, #initialize_node, #node_for_name, #nodes, parent_delegate, parse, #parse, part, structure, unordered

Constructor Details

#initializeMessage

Returns a new instance of Message.



34
35
36
37
# File 'lib/fix/protocol/message.rb', line 34

def initialize
  super
  header.msg_type = MessageClassMapping.reverse_get(self.class)
end

Class Method Details

.version=(v) ⇒ Object



17
18
19
# File 'lib/fix/protocol/message.rb', line 17

def self.version=(v)
  @@expected_version = v
end

Instance Method Details

#dumpString

Dumps this message as a FIX protocol message, it will automatically calculate the body length and and checksum

Returns:

  • (String)

    The FIX message



45
46
47
48
49
50
51
52
# File 'lib/fix/protocol/message.rb', line 45

def dump
  if valid?
    dumped = super
    header.body_length = dumped.gsub(/^8=[^\x01]+\x01/, '').gsub(/^9=[^\x01]+\x01/, '').length
    dumped = super
    "#{dumped}10=#{'%03d' % (dumped.bytes.inject(&:+) % 256)}\x01"
  end
end

#errorsArray<String>

Returns the errors relevant to the message header

Returns:

  • (Array<String>)

    The errors on the message header



68
69
70
71
72
73
74
# File 'lib/fix/protocol/message.rb', line 68

def errors
  if (version == @@expected_version)
    super
  else
    [super, "Unsupported version: <#{version}>, expected <#{@@expected_version}>"].flatten
  end
end

#valid?Boolean

Whether this instance is ready to be dumped as a valid FIX message

Returns:

  • (Boolean)

    Whether there are errors present for this instance



59
60
61
# File 'lib/fix/protocol/message.rb', line 59

def valid?
  (errors.nil? || errors.empty?) && parse_failure.nil?
end