Class: Oxygene::CARSection

Inherits:
Object
  • Object
show all
Defined in:
lib/oxygene/car_section.rb

Overview

Represents a single CID-addressed block from a CARArchive.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cid, data) ⇒ CARSection

Creates a section from its CID and encoded body.

Parameters:

  • cid (CID)

    content identifier of the section

  • data (String)

    raw CBOR body data



25
26
27
28
# File 'lib/oxygene/car_section.rb', line 25

def initialize(cid, data)
  @cid = cid
  @data = data
end

Instance Attribute Details

#cidCID (readonly)

Returns CID of this section.

Returns:

  • (CID)

    CID of this section



15
16
17
# File 'lib/oxygene/car_section.rb', line 15

def cid
  @cid
end

#dataString (readonly)

Returns raw CBOR body binary data.

Returns:

  • (String)

    raw CBOR body binary data



18
19
20
# File 'lib/oxygene/car_section.rb', line 18

def data
  @data
end

Instance Method Details

#decoded_bodyObject

Decodes the body from CBOR, without performing ATProto JSON specific conversions.

The result is decoded once and memoized. CID links are represented as CBOR::Tagged objects, and byte strings remain binary strings.

Returns:

  • (Object)

    section data decoded from CBOR



37
38
39
# File 'lib/oxygene/car_section.rb', line 37

def decoded_body
  @decoded_body ||= CBOR.decode(@data)
end

#json_bodyHash, Array Also known as: body

Decodes the body and converts it to an ATProto JSON compatible format.

The conversion involves:

  • replacing binary strings with $bytes objects with Base64-encoded data
  • converting CBOR CID tags to $link objects holding an Oxygene::CID

The result is decoded and stored separately from #decoded_body, so calling this method does not mutate the value returned by #decoded_body.

Returns:

  • (Hash, Array)

    decoded ATProto JSON compatible body

Raises:

  • (DecodeError)

    if the decoded top-level value is not a hash or array



53
54
55
# File 'lib/oxygene/car_section.rb', line 53

def json_body
  @json_body ||= CARArchive.convert_data(CBOR.decode(@data))
end