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



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



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

def content
  @content
end

#content_idString?



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

def content_id
  @content_id
end

#content_typeString



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

def content_type
  @content_type
end

#nameString



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



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



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



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



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