Class: Veriform::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/veriform/decoder.rb

Overview

Build Veriform::Objects from Veriform’s self-describing messages

Instance Method Summary collapse

Constructor Details

#initializeDecoder

Create a new decoder object which will construct a Veriform::Object tree



7
8
9
# File 'lib/veriform/decoder.rb', line 7

def initialize
  @stack = [Veriform::Object.new]
end

Instance Method Details

#begin_nestedObject

Push down the internal stack, constructing a new Veriform::Object



25
26
27
# File 'lib/veriform/decoder.rb', line 25

def begin_nested
  @stack << Veriform::Object.new
end

#binary(id, value) ⇒ Object

Add binary data to the current object

Raises:

  • (TypeError)


18
19
20
21
22
# File 'lib/veriform/decoder.rb', line 18

def binary(id, value)
  raise TypeError, "expected String, got #{value.class}" unless value.is_a?(String)
  raise EncodingError, "expected BINARY encoding, got #{value.encoding}" unless value.encoding == Encoding::BINARY
  @stack.last[id] = value
end

#end_nested(id) ⇒ Object

Complete the pushdown, adding the newly constructed object to the next one in the stack

Raises:



30
31
32
33
34
# File 'lib/veriform/decoder.rb', line 30

def end_nested(id)
  value = @stack.pop
  raise StateError, "not inside a nested message" if @stack.empty?
  @stack.last[id] = value
end

#finishObject

Finish decoding, returning the parent Veriform::Object

Raises:



37
38
39
40
41
# File 'lib/veriform/decoder.rb', line 37

def finish
  result = @stack.pop
  raise StateError, "objects remaining in stack" unless @stack.empty?
  result
end

#uint64(id, value) ⇒ Object

Add a uint64 to the current object

Raises:

  • (TypeError)


12
13
14
15
# File 'lib/veriform/decoder.rb', line 12

def uint64(id, value)
  raise TypeError, "expected Integer, got #{value.class}" unless value.is_a?(Integer)
  @stack.last[id] = value
end