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
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/hanami/mailer/attachment.rb', line 102 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.
90 91 92 |
# File 'lib/hanami/mailer/attachment.rb', line 90 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.
90 91 92 |
# File 'lib/hanami/mailer/attachment.rb', line 90 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.
90 91 92 |
# File 'lib/hanami/mailer/attachment.rb', line 90 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.
90 91 92 |
# File 'lib/hanami/mailer/attachment.rb', line 90 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/hanami/mailer/attachment.rb', line 37 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
66 67 68 69 70 |
# File 'lib/hanami/mailer/attachment.rb', line 66 def from_file(filename, attachment_paths:, inline: false) content = (filename, ) new(filename:, content:, inline:) end |
Instance Method Details
#inline? ⇒ Boolean
Returns true if this is an inline attachment.
118 |
# File 'lib/hanami/mailer/attachment.rb', line 118 def inline? = @inline |