Module: Cborb

Defined in:
lib/cborb.rb,
lib/cborb/errors.rb,
lib/cborb/version.rb

Defined Under Namespace

Modules: Decoding Classes: DecodingError, Error, InvalidByteSequenceError

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.decode(cbor, concatenated: false) ⇒ Object

The shorthand to decode CBOR

Parameters:

  • cbor (String)
  • concatenated (Boolean) (defaults to: false)

    Whether “cbor” param is constructed by concatenated CBOR byte string. If it’s true, this method returns instance of Cborb::Decoding::Concatenated

Returns:

  • (Object)

    decoded data(Array, Hash, etc…)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cborb.rb', line 43

def decode(cbor, concatenated: false)
  results = Decoding::Concatenated.new
  loop do
    decoder = Decoding::Decoder.new
    decoder.decode(cbor)

    raise Cborb::InvalidByteSequenceError unless decoder.finished?

    results << decoder.result

    if decoder.remaining_bytes.empty?
      break
    elsif !concatenated
      raise Cborb::InvalidByteSequenceError
    end

    cbor = decoder.remaining_bytes
  end

  concatenated ? results : results.first
end