Class: PecRuby::Attachment
- Inherits:
-
Object
- Object
- PecRuby::Attachment
- Defined in:
- lib/pec_ruby/attachment.rb
Instance Attribute Summary collapse
-
#mail_attachment ⇒ Object
readonly
Returns the value of attribute mail_attachment.
Instance Method Summary collapse
-
#as_postacert_message ⇒ Object
Parse this attachment as a postacert.eml if it is one Returns a PecRuby::Message-like object for the nested postacert.
- #content ⇒ Object
- #filename ⇒ Object
-
#initialize(mail_attachment) ⇒ Attachment
constructor
A new instance of Attachment.
- #mime_type ⇒ Object
-
#postacert? ⇒ Boolean
Check if this attachment is a postacert.eml file.
-
#save_to(path) ⇒ Object
Save attachment to file.
-
#save_to_dir(directory) ⇒ Object
Save attachment to directory with original filename.
- #size ⇒ Object
- #size_kb ⇒ Object
- #size_mb ⇒ Object
- #summary ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(mail_attachment) ⇒ Attachment
Returns a new instance of Attachment.
9 10 11 |
# File 'lib/pec_ruby/attachment.rb', line 9 def initialize() @mail_attachment = end |
Instance Attribute Details
#mail_attachment ⇒ Object (readonly)
Returns the value of attribute mail_attachment.
7 8 9 |
# File 'lib/pec_ruby/attachment.rb', line 7 def @mail_attachment end |
Instance Method Details
#as_postacert_message ⇒ Object
Parse this attachment as a postacert.eml if it is one Returns a PecRuby::Message-like object for the nested postacert
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pec_ruby/attachment.rb', line 71 def return nil unless postacert? begin # Parse the attachment content as an email message nested_mail = Mail.read_from_string(content) # Create a simplified message object for the nested postacert NestedPostacertMessage.new(nested_mail) rescue => e raise PecRuby::Error, "Failed to parse nested postacert.eml: #{e.}" end end |
#content ⇒ Object
33 34 35 |
# File 'lib/pec_ruby/attachment.rb', line 33 def content @mail_attachment.decoded end |
#filename ⇒ Object
13 14 15 |
# File 'lib/pec_ruby/attachment.rb', line 13 def filename @mail_attachment.filename || "unnamed_file" end |
#mime_type ⇒ Object
17 18 19 |
# File 'lib/pec_ruby/attachment.rb', line 17 def mime_type @mail_attachment.mime_type || "application/octet-stream" end |
#postacert? ⇒ Boolean
Check if this attachment is a postacert.eml file
64 65 66 67 |
# File 'lib/pec_ruby/attachment.rb', line 64 def postacert? filename&.downcase&.include?('postacert.eml') || (filename&.downcase&.end_with?('.eml') && mime_type&.include?('message')) end |
#save_to(path) ⇒ Object
Save attachment to file
38 39 40 |
# File 'lib/pec_ruby/attachment.rb', line 38 def save_to(path) File.binwrite(path, content) end |
#save_to_dir(directory) ⇒ Object
Save attachment to directory with original filename
43 44 45 46 47 |
# File 'lib/pec_ruby/attachment.rb', line 43 def save_to_dir(directory) path = File.join(directory, filename) save_to(path) path end |
#size ⇒ Object
21 22 23 |
# File 'lib/pec_ruby/attachment.rb', line 21 def size content.bytesize end |
#size_kb ⇒ Object
25 26 27 |
# File 'lib/pec_ruby/attachment.rb', line 25 def size_kb (size / 1024.0).round(1) end |
#size_mb ⇒ Object
29 30 31 |
# File 'lib/pec_ruby/attachment.rb', line 29 def size_mb (size / 1024.0 / 1024.0).round(2) end |
#summary ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/pec_ruby/attachment.rb', line 49 def summary { filename: filename, mime_type: mime_type, size: size, size_kb: size_kb, size_mb: size_mb } end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/pec_ruby/attachment.rb', line 59 def to_s "#{filename} (#{mime_type}, #{size_kb} KB)" end |