Class: PostmarkClient::Attachment
- Inherits:
-
Object
- Object
- PostmarkClient::Attachment
- Defined in:
- lib/postmark_client/models/attachment.rb
Overview
Represents an email attachment for the Postmark API.
Instance Attribute Summary collapse
-
#content ⇒ String
The Base64-encoded content of the attachment.
-
#content_id ⇒ String?
The content ID for inline attachments.
-
#content_type ⇒ String
The MIME type of the attachment.
-
#name ⇒ String
The filename of the attachment.
Class Method Summary collapse
-
.detect_content_type(filename) ⇒ String
Detect content type from file extension.
-
.from_file(file_path, content_type: nil, content_id: nil) ⇒ Attachment
Create an attachment from a file path.
Instance Method Summary collapse
-
#initialize(name:, content:, content_type:, content_id: nil, base64_encoded: false) ⇒ Attachment
constructor
Initialize a new attachment.
-
#inline? ⇒ Boolean
Check if this is an inline attachment.
-
#to_h ⇒ Hash
Convert the attachment to a hash for API requests.
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
#content ⇒ String
Returns the Base64-encoded content of the attachment.
30 31 32 |
# File 'lib/postmark_client/models/attachment.rb', line 30 def content @content end |
#content_id ⇒ String?
Returns 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_type ⇒ String
Returns the MIME type of the attachment.
33 34 35 |
# File 'lib/postmark_client/models/attachment.rb', line 33 def content_type @content_type end |
#name ⇒ String
Returns 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
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_h ⇒ Hash
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 |