Class: PecRuby::Attachment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @mail_attachment = mail_attachment
end

Instance Attribute Details

#mail_attachmentObject (readonly)

Returns the value of attribute mail_attachment.



7
8
9
# File 'lib/pec_ruby/attachment.rb', line 7

def mail_attachment
  @mail_attachment
end

Instance Method Details

#as_postacert_messageObject

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 as_postacert_message
  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.message}"
  end
end

#contentObject



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

def content
  @mail_attachment.decoded
end

#filenameObject



13
14
15
# File 'lib/pec_ruby/attachment.rb', line 13

def filename
  @mail_attachment.filename || "unnamed_file"
end

#mime_typeObject



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

Returns:

  • (Boolean)


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

#sizeObject



21
22
23
# File 'lib/pec_ruby/attachment.rb', line 21

def size
  content.bytesize
end

#size_kbObject



25
26
27
# File 'lib/pec_ruby/attachment.rb', line 25

def size_kb
  (size / 1024.0).round(1)
end

#size_mbObject



29
30
31
# File 'lib/pec_ruby/attachment.rb', line 29

def size_mb
  (size / 1024.0 / 1024.0).round(2)
end

#summaryObject



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_sObject



59
60
61
# File 'lib/pec_ruby/attachment.rb', line 59

def to_s
  "#{filename} (#{mime_type}, #{size_kb} KB)"
end