Class: Uniword::Infrastructure::MimePackager
- Inherits:
-
Object
- Object
- Uniword::Infrastructure::MimePackager
- 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).
Instance Attribute Summary collapse
-
#boundary ⇒ Object
readonly
Returns the value of attribute boundary.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Class Method Summary collapse
-
.from_document(document) ⇒ Object
Create packager from document (class method for compatibility).
Instance Method Summary collapse
-
#build_mime_content ⇒ String
Build complete MIME content as string.
-
#initialize(document) ⇒ MimePackager
constructor
Create packager from an Mhtml::Document.
-
#package(output_path) ⇒ Object
Package document to MHTML file.
Constructor Details
#initialize(document) ⇒ MimePackager
Create packager from an Mhtml::Document.
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
#boundary ⇒ Object (readonly)
Returns the value of attribute boundary.
17 18 19 |
# File 'lib/uniword/infrastructure/mime_packager.rb', line 17 def boundary @boundary end |
#document ⇒ Object (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_content ⇒ String
Build complete MIME content as string.
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.
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 |