Class: Hanami::Mailer::Attachment
- Inherits:
-
Object
- Object
- Hanami::Mailer::Attachment
- Defined in:
- lib/hanami/mailer/attachment.rb
Overview
Represents an email attachment
Constant Summary collapse
- MIME_TYPES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Common MIME types for attachments
{ ".pdf" => "application/pdf", ".zip" => "application/zip", ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".png" => "image/png", ".gif" => "image/gif", ".txt" => "text/plain", ".csv" => "text/csv", ".doc" => "application/msword", ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".xls" => "application/vnd.ms-excel", ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }.freeze
Instance Attribute Summary collapse
- #content ⇒ Object readonly private
- #content_id ⇒ Object readonly private
- #content_type ⇒ Object readonly private
- #filename ⇒ Object readonly private
Class Method Summary collapse
-
.from(input) ⇒ Attachment
private
Coerces runtime attachment input into an Attachment.
-
.from_file(filename, attachment_paths:, inline: false) ⇒ Attachment
private
Resolve a static filename from attachment paths and return an Attachment.
Instance Method Summary collapse
-
#initialize(filename:, content:, content_type: nil, inline: false) ⇒ Attachment
constructor
Initialize a new attachment.
-
#inline? ⇒ Boolean
Returns true if this is an inline attachment.
Constructor Details
#initialize(filename:, content:, content_type: nil, inline: false) ⇒ Attachment
Initialize a new attachment
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/hanami/mailer/attachment.rb', line 106 def initialize(filename:, content:, content_type: nil, inline: false) raise ArgumentError, "filename is required" if filename.nil? || (filename.is_a?(String) && filename.empty?) raise ArgumentError, "content is required" if content.nil? @filename = filename @content = content @content_type = content_type || detect_content_type(filename) @inline = inline @content_id = inline ? filename : nil end |
Instance Attribute Details
#content ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
93 94 95 |
# File 'lib/hanami/mailer/attachment.rb', line 93 def content @content end |
#content_id ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
93 94 95 |
# File 'lib/hanami/mailer/attachment.rb', line 93 def content_id @content_id end |
#content_type ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
93 94 95 |
# File 'lib/hanami/mailer/attachment.rb', line 93 def content_type @content_type end |
#filename ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
93 94 95 |
# File 'lib/hanami/mailer/attachment.rb', line 93 def filename @filename end |
Class Method Details
.from(input) ⇒ Attachment
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Coerces runtime attachment input into an Attachment
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/hanami/mailer/attachment.rb', line 38 def from(input) case input when Attachment input when Hash # Extract keys explicitly rather than splatting so that missing keys arrive as nil, # letting the argument checks in #initialize raise clearer errors. new( filename: input[:filename], content: input[:content], content_type: input[:content_type], inline: input[:inline] ) else raise ArgumentError, "Cannot convert #{input.class} to Attachment" end end |
.from_file(filename, attachment_paths:, inline: false) ⇒ Attachment
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolve a static filename from attachment paths and return an Attachment
67 68 69 70 71 72 73 |
# File 'lib/hanami/mailer/attachment.rb', line 67 def from_file(filename, attachment_paths:, inline: false) content = (filename, ) # A nested name (e.g. "foo/bar.pdf") is resolved via the search paths, but only its # basename should reach the recipient as the attachment's filename and content-id. new(filename: File.basename(filename), content:, inline:) end |
Instance Method Details
#inline? ⇒ Boolean
Returns true if this is an inline attachment.
123 |
# File 'lib/hanami/mailer/attachment.rb', line 123 def inline? = @inline |