class RemoteRuby::Unmarshaler

Unmarshals variables from given stream

Constants

UnmarshalError

Attributes

stream[R]
terminator[R]

Public Class Methods

new(stream, terminator = nil) click to toggle source
# File lib/remote_ruby/unmarshaler.rb, line 6
def initialize(stream, terminator = nil)
  @stream = stream
  @terminator = terminator
end

Public Instance Methods

unmarshal() click to toggle source
# File lib/remote_ruby/unmarshaler.rb, line 11
def unmarshal
  res = {}

  until stream.eof?
    line = stream.readline

    break if terminator && line == terminator

    var = read_var(line)
    res[var.first] = var[1]
  end

  res
end

Private Instance Methods

read_var(line) click to toggle source
# File lib/remote_ruby/unmarshaler.rb, line 30
def read_var(line)
  varname, length = read_var_header(line)
  data = read_var_data(length)
  [varname.to_sym, data]
rescue ArgumentError => e
  raise UnmarshalError,
        "Could not resolve type for #{varname} variable: #{e.message}"
rescue TypeError
  raise UnmarshalError, 'Incorrect data format'
end
read_var_data(length) click to toggle source

rubocop:disable Security/MarshalLoad

# File lib/remote_ruby/unmarshaler.rb, line 52
def read_var_data(length)
  data = stream.read(length.to_i)
  Marshal.load(data)
end
read_var_header(line) click to toggle source
# File lib/remote_ruby/unmarshaler.rb, line 41
def read_var_header(line)
  varname, length = line.split(':')

  if varname.nil? || length.nil?
    raise UnmarshalError, "Incorrect header '#{line}'"
  end

  [varname, length]
end