Class: PecRuby::NestedPostacertMessage

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

Overview

Simplified message class for nested postacert emails

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ NestedPostacertMessage

Returns a new instance of NestedPostacertMessage.



90
91
92
# File 'lib/pec_ruby/attachment.rb', line 90

def initialize(mail)
  @mail = mail
end

Instance Attribute Details

#mailObject (readonly)

Returns the value of attribute mail.



88
89
90
# File 'lib/pec_ruby/attachment.rb', line 88

def mail
  @mail
end

Instance Method Details

#attachmentsObject



156
157
158
159
160
# File 'lib/pec_ruby/attachment.rb', line 156

def attachments
  return [] unless @mail&.attachments

  @mail.attachments.map { |att| Attachment.new(att) }
end

#bodyObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pec_ruby/attachment.rb', line 110

def body
  # Try to get text/plain first, then text/html
  text_part = extract_text_part(@mail, "text/plain")
  html_part = extract_text_part(@mail, "text/html")
  selected_part = text_part || html_part

  return nil unless selected_part

  raw_body = selected_part.body.decoded
  charset = selected_part.charset || 
            selected_part.content_type_parameters&.[]("charset") || 
            "UTF-8"
  
  content = raw_body.dup.force_encoding(charset).encode("UTF-8")
  
  {
    content: content,
    content_type: selected_part.mime_type,
    charset: charset
  }
end

#body_htmlObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/pec_ruby/attachment.rb', line 144

def body_html
  html_part = extract_text_part(@mail, "text/html")
  return nil unless html_part

  raw_body = html_part.body.decoded
  charset = html_part.charset || 
            html_part.content_type_parameters&.[]("charset") || 
            "UTF-8"
  
  raw_body.dup.force_encoding(charset).encode("UTF-8")
end

#body_textObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/pec_ruby/attachment.rb', line 132

def body_text
  text_part = extract_text_part(@mail, "text/plain")
  return nil unless text_part

  raw_body = text_part.body.decoded
  charset = text_part.charset || 
            text_part.content_type_parameters&.[]("charset") || 
            "UTF-8"
  
  raw_body.dup.force_encoding(charset).encode("UTF-8")
end

#dateObject



106
107
108
# File 'lib/pec_ruby/attachment.rb', line 106

def date
  @mail.date
end

#fromObject



98
99
100
# File 'lib/pec_ruby/attachment.rb', line 98

def from
  @mail.from&.first
end

#has_nested_postacerts?Boolean

Check if this nested message has any nested postacert.eml files

Returns:

  • (Boolean)


179
180
181
# File 'lib/pec_ruby/attachment.rb', line 179

def has_nested_postacerts?
  !nested_postacerts.empty?
end

#nested_postacertsObject

Find any nested postacert.eml files in this message’s attachments



174
175
176
# File 'lib/pec_ruby/attachment.rb', line 174

def nested_postacerts
  attachments.select(&:postacert?)
end

#subjectObject



94
95
96
# File 'lib/pec_ruby/attachment.rb', line 94

def subject
  @mail.subject
end

#summaryObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/pec_ruby/attachment.rb', line 162

def summary
  {
    subject: subject,
    from: from,
    to: to,
    date: date,
    attachments_count: attachments.size,
    nested_postacerts_count: nested_postacerts.size
  }
end

#toObject



102
103
104
# File 'lib/pec_ruby/attachment.rb', line 102

def to
  @mail.to || []
end