Module: Fix::Protocol

Defined in:
lib/fix/protocol.rb,
lib/fix/protocol/field.rb,
lib/fix/protocol/version.rb,
lib/fix/protocol/message.rb,
lib/fix/protocol/messages.rb,
lib/fix/protocol/message_part.rb,
lib/fix/protocol/parse_failure.rb,
lib/fix/protocol/unordered_part.rb,
lib/fix/protocol/messages/logon.rb,
lib/fix/protocol/messages/logout.rb,
lib/fix/protocol/messages/reject.rb,
lib/fix/protocol/type_conversions.rb,
lib/fix/protocol/messages/md_entry.rb,
lib/fix/protocol/messages/heartbeat.rb,
lib/fix/protocol/messages/instrument.rb,
lib/fix/protocol/message_class_mapping.rb,
lib/fix/protocol/messages/test_request.rb,
lib/fix/protocol/messages/md_entry_type.rb,
lib/fix/protocol/repeating_message_part.rb,
lib/fix/protocol/messages/resend_request.rb,
lib/fix/protocol/messages/market_data_request.rb,
lib/fix/protocol/messages/market_data_snapshot.rb,
lib/fix/protocol/messages/market_data_incremental_refresh.rb

Overview

Main protocol namespace

Defined Under Namespace

Modules: MessageClassMapping, Messages, TypeConversions Classes: Field, Message, MessagePart, ParseFailure, RepeatingMessagePart, UnorderedPart

Constant Summary collapse

VERSION =

The fix-protocol gem version string

'0.0.64'

Class Method Summary collapse

Class Method Details

.alias_namespace!Object

Alias the Fix::Protocol namespace to FP if possible


67
68
69
# File 'lib/fix/protocol.rb', line 67

def self.alias_namespace!
  Object.const_set(:FP, Protocol) unless Object.const_defined?(:FP)
end

.parse(str) ⇒ Fix::Protocol::Message

Parses a string into a Fix::Protocol::Message instance

Parameters:

  • str (String)

    A FIX message string

Returns:

  • (Fix::Protocol::Message)

    A Fix::Protocol::Message instance, or a Fix::Protocol::ParseFailure in case of failure


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/fix/protocol.rb', line 24

def self.parse(str)
  errors    = []
  msg_type  = str.match(/^8\=[^\x01]+\x019\=[^\x01]+\x0135\=([^\x01]+)\x01/)

  unless str.match(/^8\=[^\x01]+\x019\=[^\x01]+\x0135\=[^\x01]+\x01.+10\=[^\x01]+\x01/)
    FP::ParseFailure.new("Malformed message <#{str}>")
  else

    klass = MessageClassMapping.get(msg_type[1])

    unless klass
      errors << "Unknown message type <#{msg_type[1]}>"
    end

    # Check message length
    length = str.gsub(/10\=[^\x01]+\x01$/, '').gsub(/^8\=[^\x01]+\x019\=([^\x01]+)\x01/, '').length
    if length != $1.to_i
      errors << "Incorrect body length"
    end

    # Check checksum
    checksum = str.match(/10\=([^\x01]+)\x01/)[1]
    if checksum != ('%03d' % (str.gsub(/10\=[^\x01]+\x01/, '').bytes.inject(&:+) % 256))
      errors << "Incorrect checksum"
    end

    if errors.empty?
      msg = klass.parse(str)

      if msg.valid?
        msg
      else
        FP::ParseFailure.new(msg.errors)
      end
    else
      FP::ParseFailure.new(errors)
    end
  end
end