Class: Hanami::Mailer::Attachment

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename:, content:, content_type: nil, inline: false) ⇒ Attachment

Initialize a new attachment

Parameters:

  • filename (String)

    the filename for the attachment

  • content (String, IO)

    the attachment content

  • content_type (String, nil) (defaults to: nil)

    optional MIME type

  • inline (Boolean) (defaults to: false)

    whether this is an inline attachment

Raises:

  • (ArgumentError)

    if filename or content is missing



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

#contentObject (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_idObject (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_typeObject (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

#filenameObject (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

Parameters:

  • input (Attachment, Hash)

    attachment or hash with attachment attributes

Returns:

Raises:

  • (ArgumentError)

    if input cannot be coerced



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

Parameters:

  • filename (String)

    the filename to resolve

  • attachment_paths (Array<String>)

    paths to search for the file

  • inline (Boolean) (defaults to: false)

    whether this is an inline attachment

Returns:

Raises:



66
67
68
69
70
# File 'lib/hanami/mailer/attachment.rb', line 66

def from_file(filename, attachment_paths:, inline: false)
  content = read_attachment_file(filename, attachment_paths)

  new(filename:, content:, inline:)
end

Instance Method Details

#inline?Boolean

Returns true if this is an inline attachment.

Returns:

  • (Boolean)


118
# File 'lib/hanami/mailer/attachment.rb', line 118

def inline? = @inline