Class: Protocol::Multipart::Mixed

Inherits:
Part
  • Object
show all
Defined in:
lib/protocol/multipart/mixed.rb

Overview

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

Direct Known Subclasses

FormData

Instance Attribute Summary collapse

Attributes inherited from Part

#headers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Part

#The headers as name/value pairs.=

Constructor Details

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

Initialize a new multipart/mixed container.



27
28
29
30
31
32
33
# File 'lib/protocol/multipart/mixed.rb', line 27

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

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



36
37
38
# File 'lib/protocol/multipart/mixed.rb', line 36

def boundary
  @boundary
end

#partsObject (readonly)

Returns the value of attribute parts.



39
40
41
# File 'lib/protocol/multipart/mixed.rb', line 39

def parts
  @parts
end

#The boundary string used to separate parts.(boundarystringusedtoseparateparts.) ⇒ Object (readonly)



36
# File 'lib/protocol/multipart/mixed.rb', line 36

attr :boundary

#The parts of the body.(partsofthebody.) ⇒ Object (readonly)



39
# File 'lib/protocol/multipart/mixed.rb', line 39

attr :parts

Class Method Details

.mime_typeObject

Returns the MIME type for mixed multipart data.



17
18
19
# File 'lib/protocol/multipart/mixed.rb', line 17

def self.mime_type
  "multipart/mixed"
end

Instance Method Details

#call(writable, boundary = nil) ⇒ Object

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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/protocol/multipart/mixed.rb', line 55

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