class IOPart < Part
- Inherits from
Part- Extends
Escape: .escape_field_name, .unescape_field_name
A part that contains IO content (files, streams, etc.).
Definitions
def self.open(path, mime_type: nil, name: nil, headers: {})
Opens a file and creates an IOPart for it.
Signature
-
parameter
pathString The file path to open.
-
parameter
mime_typeString | Nil The MIME type of the file, or nil to use application/octet-stream.
-
parameter
nameString | Nil The name to use for the file, or nil to use the filename.
-
parameter
headersHash Additional headers for the part.
-
returns
IOPart A new IOPart instance for the file.
Implementation
def self.open(path, mime_type: nil, name: nil, headers: {})
io = File.open(path, "rb")
name ||= File.basename(path)
headers = headers.merge(
"content-disposition" => "attachment; filename=\"#{escape_field_name name}\"",
"content-type" => mime_type || "application/octet-stream"
)
return new(headers, io)
end
def initialize(headers, io)
Initialize a new IOPart with the given headers and IO object.
Signature
-
parameter
headersHash Headers for the part.
-
parameter
ioIO The IO object containing the part's data.
Implementation
def initialize(headers, io)
super(headers)
@io = io
end
attr_reader :io
The underlying IO object.
Signature
-
attribute
IO The IO object containing the part's data.
def call(writable, boundary)
Write the part's content to the provided writable stream.
Signature
-
parameter
writableIO The destination stream to write to.
-
parameter
boundaryString The boundary string for the multipart message.
Implementation
def call(writable, boundary)
while chunk = @io.read(8192)
break if chunk.empty?
writable.write(chunk)
end
end