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

  • encoding (:raw|:hex|:base64) (defaults to: "raw")

    decode the input before deserialization

Returns:

  • (Object)

    the deserialized value



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

def from_xdr(string, encoding = "raw")
  raw = case String(encoding)
        when "raw" then string
        when "hex" then [string].pack("H*")
        when "base64" then Base64.strict_decode64(string)
        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

This method is abstract.

Reads from the provided IO an instance of the implementing class

Parameters:

  • io (#read)

    the io to read from

Returns:

  • (Object)

    the deserialized value

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 23

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

  • encoding (:raw|:hex|:base64) (defaults to: "raw")

    encode the result with specified codec

Returns:

  • (String)

    the produced bytes



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

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

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

#valid?(value) ⇒ Boolean

This method is abstract.

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)


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

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

#write(val, io) ⇒ void

This method is abstract.

This method returns an undefined value.

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

Parameters:

  • val (Object)

    The object to serialize

  • io (#write)

    an IO object to write to

Raises:

  • (NotImplementedError)


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

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