Class: PostmarkClient::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/postmark_client/models/attachment.rb

Overview

Represents an email attachment for the Postmark API.

Examples:

Creating a simple attachment

attachment = PostmarkClient::Attachment.new(
  name: "document.pdf",
  content: File.read("document.pdf"),
  content_type: "application/pdf"
)

Creating an inline image attachment

attachment = PostmarkClient::Attachment.new(
  name: "logo.png",
  content: File.read("logo.png"),
  content_type: "image/png",
  content_id: "cid:logo.png"
)

Creating from a file path

attachment = PostmarkClient::Attachment.from_file("path/to/file.pdf")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, content:, content_type:, content_id: nil, base64_encoded: false) ⇒ Attachment

Initialize a new attachment

Parameters:

  • name (String)

    the filename

  • content (String)

    the file content (will be Base64 encoded if not already)

  • content_type (String)

    the MIME type

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

    optional content ID for inline images

  • base64_encoded (Boolean) (defaults to: false)

    whether content is already Base64 encoded



45
46
47
48
49
50
# File 'lib/postmark_client/models/attachment.rb', line 45

def initialize(name:, content:, content_type:, content_id: nil, base64_encoded: false)
  @name = name
  @content = base64_encoded ? content : Base64.strict_encode64(content)
  @content_type = content_type
  @content_id = content_id
end

Instance Attribute Details

#contentString

Returns the Base64-encoded content of the attachment.

Returns:

  • (String)

    the Base64-encoded content of the attachment



30
31
32
# File 'lib/postmark_client/models/attachment.rb', line 30

def content
  @content
end

#content_idString?

Returns the content ID for inline attachments.

Returns:

  • (String, nil)

    the content ID for inline attachments



36
37
38
# File 'lib/postmark_client/models/attachment.rb', line 36

def content_id
  @content_id
end

#content_typeString

Returns the MIME type of the attachment.

Returns:

  • (String)

    the MIME type of the attachment



33
34
35
# File 'lib/postmark_client/models/attachment.rb', line 33

def content_type
  @content_type
end

#nameString

Returns the filename of the attachment.

Returns:

  • (String)

    the filename of the attachment



27
28
29
# File 'lib/postmark_client/models/attachment.rb', line 27

def name
  @name
end

Class Method Details

.detect_content_type(filename) ⇒ String

Detect content type from file extension

Parameters:

  • filename (String)

    the filename

Returns:

  • (String)

    the detected MIME type



97
98
99
100
101
# File 'lib/postmark_client/models/attachment.rb', line 97

def self.detect_content_type(filename)
  extension = File.extname(filename).downcase.delete(".")

  CONTENT_TYPES.fetch(extension, "application/octet-stream")
end

.from_file(file_path, content_type: nil, content_id: nil) ⇒ Attachment

Create an attachment from a file path

Parameters:

  • file_path (String)

    path to the file

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

    optional MIME type (auto-detected if not provided)

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

    optional content ID for inline images

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/postmark_client/models/attachment.rb', line 58

def self.from_file(file_path, content_type: nil, content_id: nil)
  name = File.basename(file_path)
  content = File.binread(file_path)
  detected_content_type = content_type || detect_content_type(name)

  new(
    name: name,
    content: content,
    content_type: detected_content_type,
    content_id: content_id
  )
end

Instance Method Details

#inline?Boolean

Check if this is an inline attachment

Returns:

  • (Boolean)

    true if this is an inline attachment



87
88
89
# File 'lib/postmark_client/models/attachment.rb', line 87

def inline?
  !content_id.nil?
end

#to_hHash

Convert the attachment to a hash for API requests

Returns:

  • (Hash)

    the attachment as a hash



74
75
76
77
78
79
80
81
82
# File 'lib/postmark_client/models/attachment.rb', line 74

def to_h
  hash = {
    "Name" => name,
    "Content" => content,
    "ContentType" => content_type
  }
  hash["ContentID"] = content_id if content_id
  hash
end