Class: Skyfall::CarArchive

Inherits:
Object
  • Object
show all
Defined in:
lib/skyfall/car_archive.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CarArchive

Returns a new instance of CarArchive.



33
34
35
36
37
38
# File 'lib/skyfall/car_archive.rb', line 33

def initialize(data)
  @sections = []
  @buffer = StringIO.new(data)

  read_header(@buffer)
end

Instance Attribute Details

#rootsObject (readonly)

Returns the value of attribute roots.



31
32
33
# File 'lib/skyfall/car_archive.rb', line 31

def roots
  @roots
end

#sectionsObject (readonly)

Returns the value of attribute sections.



31
32
33
# File 'lib/skyfall/car_archive.rb', line 31

def sections
  @sections
end

Class Method Details

.convert_data(object) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/skyfall/car_archive.rb', line 65

def self.convert_data(object)
  if object.is_a?(Hash)
    object.each do |k, v|
      if v.is_a?(Hash) || v.is_a?(Array)
        convert_data(v)
      elsif v.is_a?(CBOR::Tagged)
        object[k] = make_cid_link(v)
      elsif v.is_a?(String) && v.encoding == Encoding::ASCII_8BIT
        object[k] = make_bytes(v)
      end
    end
  elsif object.is_a?(Array)
    object.each_with_index do |v, i|
      if v.is_a?(Hash) || v.is_a?(Array)
        convert_data(v)
      elsif v.is_a?(CBOR::Tagged)
        object[i] = make_cid_link(v)
      elsif v.is_a?(String) && v.encoding == Encoding::ASCII_8BIT
        object[i] = make_bytes(v)
      end
    end
  else
    raise DecodeError, "Unexpected value type in record: #{object}"
  end
end

.make_bytes(data) ⇒ Object



95
96
97
# File 'lib/skyfall/car_archive.rb', line 95

def self.make_bytes(data)
  { '$bytes' => Base64.encode64(data).chomp.gsub(/=+$/, '') }
end


91
92
93
# File 'lib/skyfall/car_archive.rb', line 91

def self.make_cid_link(cid)
  { '$link' => CID.from_cbor_tag(cid) }
end

Instance Method Details

#inspectObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/skyfall/car_archive.rb', line 99

def inspect
  vars = instance_variables.map { |v|
    if v == :@sections && @buffer
      "#{v}=[...]"
    else
      "#{v}=#{instance_variable_get(v).inspect}"
    end
  }

  "#<#{self.class}:0x#{object_id} #{vars.join(", ")}>"
end

#section_with_cid(cid) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/skyfall/car_archive.rb', line 40

def section_with_cid(cid)
  if section = @sections.detect { |s| s.cid == cid }
    return section.body
  end

  if @buffer
    while !@buffer.eof?
      section = read_section(@buffer)
      return section.body if section.cid == cid
    end
  end

  @buffer = nil
  nil
end