Class: Uniword::Infrastructure::MimePackager

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/infrastructure/mime_packager.rb

Overview

Serializes Mhtml::Document to MHTML MIME format.

Rebuilds the MIME multipart structure from the document model, encoding content appropriately (QP for text, base64 for binary).

Examples:

Serialize to file

packager = MimePackager.from_document(document)
packager.package("output.doc")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ MimePackager

Create packager from an Mhtml::Document.

Parameters:



22
23
24
25
# File 'lib/uniword/infrastructure/mime_packager.rb', line 22

def initialize(document)
  @document = document
  @boundary = document.boundary || generate_boundary
end

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



17
18
19
# File 'lib/uniword/infrastructure/mime_packager.rb', line 17

def boundary
  @boundary
end

#documentObject (readonly)

Returns the value of attribute document.



17
18
19
# File 'lib/uniword/infrastructure/mime_packager.rb', line 17

def document
  @document
end

Class Method Details

.from_document(document) ⇒ Object

Create packager from document (class method for compatibility).



28
29
30
# File 'lib/uniword/infrastructure/mime_packager.rb', line 28

def self.from_document(document)
  new(document)
end

Instance Method Details

#build_mime_contentString

Build complete MIME content as string.

Returns:

  • (String)

    The complete MHTML content



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/uniword/infrastructure/mime_packager.rb', line 45

def build_mime_content
  parts = []

  # MIME global header
  parts << "MIME-Version: 1.0"
  parts << "Content-Type: multipart/related; boundary=\"#{boundary}\""
  parts << ""

  # HTML part (main document)
  parts << build_part(document.html_part) if document.html_part

  # All other parts (XML, images, theme, etc.)
  document.parts.each do |part|
    next if part == document.html_part

    parts << build_part(part)
  end

  # Final boundary
  parts << "--#{boundary}--"
  parts << ""

  parts.join("\r\n")
end

#package(output_path) ⇒ Object

Package document to MHTML file.

Parameters:

  • output_path (String)

    The output file path

Raises:

  • (ArgumentError)


35
36
37
38
39
40
# File 'lib/uniword/infrastructure/mime_packager.rb', line 35

def package(output_path)
  raise ArgumentError, "Output path cannot be nil" if output_path.nil?
  raise ArgumentError, "Output path cannot be empty" if output_path.empty?

  File.binwrite(output_path, build_mime_content.encode("UTF-8"))
end