Module: Fix::Protocol::TypeConversions

Included in:
Field
Defined in:
lib/fix/protocol/type_conversions.rb

Overview

Defines helper methods to convert to and from FIX data types

Instance Method Summary collapse

Instance Method Details

#dump_integer(i) ⇒ String

Dumps an integer to a string

Parameters:

  • i (Fixnum)

    An integer

Returns:

  • (String)

    It's string representation


48
49
50
# File 'lib/fix/protocol/type_conversions.rb', line 48

def dump_integer(i)
  i.to_s
end

#dump_timestamp(dt) ⇒ String

Outputs a DateTime object as a FIX-formatted timestamp

Parameters:

  • dt (DateTime)

    An UTC date and time

Returns:

  • (String)

    A FIX-formatted timestamp


28
29
30
# File 'lib/fix/protocol/type_conversions.rb', line 28

def dump_timestamp(dt)
  dt.utc.strftime('%Y%m%d-%H:%M:%S')
end

#dump_yn_bool(b) ⇒ String

Dumps a boolean to a Y/N FIX string

Parameters:

  • b (Boolean)

    A boolean

Returns:

  • (String)

    'Y' if the parameter is true, 'N' otherwise


58
59
60
# File 'lib/fix/protocol/type_conversions.rb', line 58

def dump_yn_bool(b)
  b ? 'Y' : 'N'
end

#parse_integer(str) ⇒ Fixnum

Parses an integer

Parameters:

  • str (String)

    An integer as a string

Returns:

  • (Fixnum)

    The parsed integer


38
39
40
# File 'lib/fix/protocol/type_conversions.rb', line 38

def parse_integer(str)
  str && str.to_i
end

#parse_timestamp(str) ⇒ Time

Parses a FIX-formatted timestamp into a Time instance, milliseconds are discarded

Parameters:

  • str (String)

    A FIX-formatted timestamp

Returns:

  • (Time)

    An UTC date and time


15
16
17
18
19
20
# File 'lib/fix/protocol/type_conversions.rb', line 15

def parse_timestamp(str)
  if m = str.match(/\A([0-9]{4})([0-9]{2})([0-9]{2})-([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]{3})?\Z/)
    elts = m.to_a.map(&:to_i)
    Time.new(elts[1], elts[2], elts[3], elts[4], elts[5], elts[6], 0)
  end
end

#parse_yn_bool(str) ⇒ Boolean

Parses a string into a boolean value

Parameters:

  • str (String)

    The string to parse

Returns:

  • (Boolean)

    true if the string is 'Y', false otherwise


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

def parse_yn_bool(str)
  !!(str == 'Y')
end