Class: Dcc::Extract::File

Inherits:
Object
  • Object
show all
Defined in:
lib/dcc/extract/file.rb

Overview

Dcc::Extract::File enumerates embedded files (dcc:byteDataType elements) inside a parsed DCC. Each entry carries the decoded binary payload, MIME type, file name, and the surrounding ring.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, file_name:, mime_type:, data:, ring:) ⇒ File

Returns a new instance of File.

Parameters:

  • name (String, nil)

    human-readable name (typically from dcc:name).

  • file_name (String, nil)

    original file name.

  • mime_type (String, nil)

    MIME type (e.g. text/plain).

  • data (String)

    binary payload (decoded from base64).

  • ring (Symbol)

    one of Dcc::Extract::Ring::ALL.



19
20
21
22
23
24
25
# File 'lib/dcc/extract/file.rb', line 19

def initialize(name:, file_name:, mime_type:, data:, ring:)
  @name = name
  @file_name = file_name
  @mime_type = mime_type
  @data = data
  @ring = ring
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/dcc/extract/file.rb', line 12

def data
  @data
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



12
13
14
# File 'lib/dcc/extract/file.rb', line 12

def file_name
  @file_name
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



12
13
14
# File 'lib/dcc/extract/file.rb', line 12

def mime_type
  @mime_type
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/dcc/extract/file.rb', line 12

def name
  @name
end

#ringObject (readonly)

Returns the value of attribute ring.



12
13
14
# File 'lib/dcc/extract/file.rb', line 12

def ring
  @ring
end

Class Method Details

.at(dcc, index) ⇒ Dcc::Extract::File?

Parameters:

  • dcc (Lutaml::Model::Serializable)
  • index (Integer)

    zero-based index.

Returns:



74
75
76
# File 'lib/dcc/extract/file.rb', line 74

def at(dcc, index)
  each(dcc)[index]
end

.each(dcc) {|| ... } ⇒ Array<Dcc::Extract::File>

Walk a parsed DCC and yield each embedded file in document order. Detects cycles (recursive list) via object_id tracking so the walk terminates for self-referential subtrees.

Parameters:

  • dcc (Lutaml::Model::Serializable)

Yield Parameters:

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dcc/extract/file.rb', line 43

def each(dcc, &block)
  list = []
  visited = ::Set.new

  collect = lambda do |node, ring|
    return unless node.is_a?(::Lutaml::Model::Serializable)
    return if visited.include?(node.object_id)

    visited << node.object_id

    new_ring =
      if admin_data?(node)
        Ring::ADMINISTRATIVE_DATA
      elsif measurement_list?(node)
        Ring::MEASUREMENT_RESULTS
      else
        ring
      end
    list << build_file(node, new_ring) if byte_data?(node)
    walk_children(node, new_ring, collect)
  end

  collect.call(dcc, nil)
  return list unless block_given?

  list.each(&block)
end

.in_ring(dcc, ring:) ⇒ Array<Dcc::Extract::File>

Parameters:

  • dcc (Lutaml::Model::Serializable)
  • ring (Symbol)

    one of Ring::ALL.

Returns:



81
82
83
# File 'lib/dcc/extract/file.rb', line 81

def in_ring(dcc, ring:)
  each(dcc).select { |f| f.ring == ring }
end

Instance Method Details

#text?Boolean

Returns true if the embedded payload is textual (text/*).

Returns:

  • (Boolean)

    true if the embedded payload is textual (text/*).



28
29
30
# File 'lib/dcc/extract/file.rb', line 28

def text?
  mime_type.to_s.start_with?("text/")
end

#to_sObject



32
33
34
# File 'lib/dcc/extract/file.rb', line 32

def to_s
  "File #{file_name || '(unnamed)'} [#{ring}] (#{mime_type}, #{data.bytesize} bytes)"
end