Module: XDR::Concerns::ConvertsToXDR

Includes:
ReadsBytes
Included in:
Array, Bool, Double, Enum, Float, Hyper, Int, Opaque, Option, Quadruple, String, Struct, Union, UnsignedHyper, UnsignedInt, VarArray, VarOpaque, Void
Defined in:
lib/xdr/concerns/converts_to_xdr.rb

Instance Method Summary collapse

Instance Method Details

#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

#read(io) ⇒ Object

Reads from the provided IO an instance of the implementing class

Parameters:

  • io (IO)

    the io to read from

Returns:

  • (Object)

    the deserialized value

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 21

def read(io)
  raise NotImplementedError, "implement in including class"
end

#to_xdr(val, encoding = 'raw') ⇒ String

Serialized the provided val to xdr, returning a string of the serialized data

Parameters:

  • val (Object)

    the value to serialize

Returns:

  • (String)

    the produced bytes



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 42

def to_xdr(val, encoding='raw')
  raw = StringIO.
    new.
    tap{|io| write(val, io)}.
    string.force_encoding("ASCII-8BIT")

  case encoding
  when 'raw' ; raw
  when 'base64' ; Base64.strict_encode64(raw)
  when 'hex' ; raw.unpack("H*").first
  else
    raise  ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
  end
end

#valid?(value) ⇒ Boolean

Returns true if the value provided is compatible with this serializer class

Parameters:

  • value (Object)

    the value to test

Returns:

  • (Boolean)

    true if valid, false otherwise

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 31

def valid?(value)
  raise NotImplementedError, "implement in including class"
end

#write(val, io) ⇒ Object

Serialized the provided ‘val` to xdr and writes it to `io`

Parameters:

  • val (Object)

    The object to serialize

  • io (IO)

    an IO object to write to

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 12

def write(val, io)
  raise NotImplementedError, "implement in including class"
end