Class: Simrpc::Message::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/simrpc/message.rb

Overview

Simrpc::Message formatter helper module

Class Method Summary collapse

Class Method Details

.format_with_fixed_size(size, data) ⇒ Object

Helper to format a data field of a fixed size,



61
62
63
64
65
# File 'lib/simrpc/message.rb', line 61

def self.format_with_fixed_size(size, data)
  # we will only accept the first <size> characters of data
  # FIXME throw exception if data.size < size
  data.to_s[0..size-1]
end

.format_with_size(data) ⇒ Object

Helper method to format a data field, prepending a size to it



38
39
40
41
42
# File 'lib/simrpc/message.rb', line 38

def self.format_with_size(data)
  # currently size is set to a 8 digit int
  len = "%08d" % data.to_s.size
  len + data.to_s
end

.parse_from_formatted(data, data_class = nil) ⇒ Object

Helper method to parse a data field off the front of a data sequence, using the formatted size. Returns parsed data field and remaining data sequence. If optional class is given, the from_s method will be invoked w/ the parsed data field and returned with the remaining data sequence instead



52
53
54
55
56
57
58
# File 'lib/simrpc/message.rb', line 52

def self.parse_from_formatted(data, data_class = nil)
   len = data[0...8].to_i
   parsed = data[8...8+len]
   remaining = data[8+len...data.size]
   return parsed, remaining if data_class.nil?
   return data_class.from_s(parsed), remaining
end

.parse_from_formatted_with_fixed_size(size, data, data_class = nil) ⇒ Object

Helper to parse data of a fixed size and return it and the remaining data. If optional class is given, the from_s method will be invoked w/ the parsed data field and returned w/ the remaining data sequence instead



70
71
72
73
74
75
76
# File 'lib/simrpc/message.rb', line 70

def self.parse_from_formatted_with_fixed_size(size, data, data_class = nil)
  # FIXME throw exception if data.size < size
  parsed = data[0..size-1]
  remaining = data[size...data.size]
  return parsed, remaining if data_class.nil?
  return data_class.from_s(parsed), remaining
end