Class: Postpeek::Metadata::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/postpeek/metadata/extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ Extractor

Returns a new instance of Extractor.



6
7
8
# File 'lib/postpeek/metadata/extractor.rb', line 6

def initialize(mail)
  @mail = mail
end

Instance Method Details

#attachmentsObject



70
71
72
73
74
75
76
77
78
# File 'lib/postpeek/metadata/extractor.rb', line 70

def attachments
  mail.attachments.map do |att|
    {
      filename: att.filename,
      content_type: att.mime_type,
      size: att.body.decoded.bytesize
    }
  end
end

#bccObject



26
27
28
# File 'lib/postpeek/metadata/extractor.rb', line 26

def bcc
  normalise_addresses(mail.bcc)
end

#ccObject



22
23
24
# File 'lib/postpeek/metadata/extractor.rb', line 22

def cc
  normalise_addresses(mail.cc)
end

#content_typeObject



40
41
42
43
44
# File 'lib/postpeek/metadata/extractor.rb', line 40

def content_type
  return nil unless mail.content_type

  mail.mime_type
end

#encodingObject



58
59
60
# File 'lib/postpeek/metadata/extractor.rb', line 58

def encoding
  mail.content_transfer_encoding&.to_s
end

#fromObject



14
15
16
# File 'lib/postpeek/metadata/extractor.rb', line 14

def from
  normalise_addresses(mail.from)
end

#localeObject



62
63
64
65
66
67
68
# File 'lib/postpeek/metadata/extractor.rb', line 62

def locale
  header_val = mail["X-Mailer-Locale"]&.value

  return header_val if header_val && !header_val.empty?

  defined?(I18n) ? I18n.locale&.to_s : nil
end

#message_idObject



80
81
82
# File 'lib/postpeek/metadata/extractor.rb', line 80

def message_id
  mail.message_id
end

#partsObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/postpeek/metadata/extractor.rb', line 46

def parts
  return [] unless mail.multipart?

  mail.parts.map do |part|
    {
      content_type: part.mime_type,
      encoding: part.content_transfer_encoding,
      charset: part.charset
    }
  end
end

#reply_toObject



30
31
32
# File 'lib/postpeek/metadata/extractor.rb', line 30

def reply_to
  normalise_addresses(mail.reply_to).first
end

#sent_atObject



34
35
36
37
38
# File 'lib/postpeek/metadata/extractor.rb', line 34

def sent_at
  return nil unless mail.date

  mail.date.to_time.utc.iso8601
end

#subjectObject



10
11
12
# File 'lib/postpeek/metadata/extractor.rb', line 10

def subject
  mail.subject
end

#toObject



18
19
20
# File 'lib/postpeek/metadata/extractor.rb', line 18

def to
  normalise_addresses(mail.to)
end

#to_hObject



84
85
86
87
88
89
90
91
# File 'lib/postpeek/metadata/extractor.rb', line 84

def to_h
  {
    subject: subject, from: from, to: to, cc: cc, bcc: bcc,
    reply_to: reply_to, sent_at: sent_at, content_type: content_type,
    parts: parts, encoding: encoding, locale: locale,
    attachments: attachments, message_id: message_id
  }
end