Method: XDR::Concerns::ConvertsToXDR#from_xdr

Defined in:
lib/xdr/concerns/converts_to_xdr.rb

#from_xdr(string, encoding = 'raw') ⇒ Object

Deserializes an object from the provided string of bytes

Parameters:

  • string (String)

    the bytes to read from

Returns:

  • (Object)

    the deserialized value



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 63

def from_xdr(string, encoding='raw')
  raw = case encoding
        when 'raw' ; string
        when 'base64' ; Base64.strict_decode64(string)
        when 'hex' ; [string].pack("H*")
        else
          raise  ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
        end

  io = StringIO.new(raw)
  result = read(io)

  if io.pos != io.length
    raise  ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?"
  end

  result
end