class Mixed < Part

Inherits from
Part

Represents a multipart/mixed message. A composite part that contains multiple nested parts with a boundary separator.

Definitions

def self.mime_type

Returns the MIME type for mixed multipart data.

Signature

returns String

The MIME type "multipart/mixed".

Implementation

def self.mime_type
	"multipart/mixed"
end

def initialize(headers = {}, parts = [], boundary: Multipart.secure_boundary, mime_type: self.class.mime_type)

Initialize a new multipart/mixed container.

Signature

parameter headers Hash

Headers for the multipart container.

parameter parts Array

The parts to include in this multipart container.

parameter boundary String

The boundary string to use for separating parts.

parameter mime_type String

The MIME type to use for this container.

Implementation

def initialize(headers = {}, parts = [], boundary: Multipart.secure_boundary, mime_type: self.class.mime_type)
	super(headers)
	
	@boundary = boundary
	@parts = parts
	@headers["content-type"] = "#{mime_type}; boundary=#{@boundary}"
end

attr :boundary

Signature

attribute String

The boundary string used to separate parts.

attr :parts

Signature

attribute Array(Part)

The parts of the body.

def call(writable, boundary = nil)

Writes the multipart container and all its parts to the writable stream. This method serializes the multipart container, including all nested parts, with appropriate boundaries between them.

Signature

parameter writable IO

The writable stream to write the multipart data to.

parameter boundary String | Nil

The parent boundary string, if this is a nested multipart.

Implementation

def call(writable, boundary = nil)
	return if @parts.empty?
	
	first = true
	initial_boundary = "--#{@boundary}\r\n".freeze
	middle_boundary = "\r\n--#{@boundary}\r\n".freeze
	
	# Write each part:
	@parts.each do |part|
		if first
			# Write the initial boundary:
			writable.write(initial_boundary)
			first = false
		else
			# Write the boundary before each part:
			writable.write(middle_boundary)
		end
		
		self.write_headers(writable, part.headers)
		part.call(writable, middle_boundary)
	end
	
	unless first
		# Write the final boundary:
		writable.write("\r\n--#{@boundary}--\r\n")
	end
end