Class: MailboxGem::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/mailbox_gem/message.rb

Overview

Wraps a Mail::Message captured off the delivery path.

We wrap rather than expose Mail::Message directly so the controller/views depend on a small, stable interface instead of the mail gem's API - html_part/text_part branching on multipart? lives here, once, not in ERB.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ Message

Returns a new instance of Message.



12
13
14
15
16
17
18
19
20
# File 'lib/mailbox_gem/message.rb', line 12

def initialize(mail)
  @id = SecureRandom.uuid
  # Time.now, not Time.current: Time.current follows the host app's
  # Time.zone (UTC by default in Rails), which has nothing to do with
  # what a developer watching their own dev server actually wants here -
  # their own machine's clock. Time.now always reads the OS's local zone.
  @captured_at = Time.now
  @mail = mail
end

Instance Attribute Details

#captured_atObject (readonly)

Returns the value of attribute captured_at.



10
11
12
# File 'lib/mailbox_gem/message.rb', line 10

def captured_at
  @captured_at
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/mailbox_gem/message.rb', line 10

def id
  @id
end

#mailObject (readonly)

Returns the value of attribute mail.



10
11
12
# File 'lib/mailbox_gem/message.rb', line 10

def mail
  @mail
end

Instance Method Details

#attachmentsObject



58
59
60
# File 'lib/mailbox_gem/message.rb', line 58

def attachments
  mail.attachments
end

#bccObject



38
39
40
# File 'lib/mailbox_gem/message.rb', line 38

def bcc
  Array(mail.bcc).join(", ")
end

#ccObject



34
35
36
# File 'lib/mailbox_gem/message.rb', line 34

def cc
  Array(mail.cc).join(", ")
end

#fromObject



26
27
28
# File 'lib/mailbox_gem/message.rb', line 26

def from
  Array(mail.from).join(", ")
end

#html_bodyObject



42
43
44
45
46
47
48
# File 'lib/mailbox_gem/message.rb', line 42

def html_body
  if mail.multipart?
    mail.html_part&.decoded
  elsif mail.mime_type == "text/html"
    mail.body.decoded
  end
end

#matches?(query) ⇒ Boolean

Plain substring match, not a query language - the fields searched are exactly the ones shown in the index table, so results stay predictable.

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/mailbox_gem/message.rb', line 68

def matches?(query)
  return true if query.blank?

  [ subject, from, to ].any? { |field| field.to_s.downcase.include?(query.downcase) }
end

#sourceObject



62
63
64
# File 'lib/mailbox_gem/message.rb', line 62

def source
  mail.to_s
end

#subjectObject



22
23
24
# File 'lib/mailbox_gem/message.rb', line 22

def subject
  mail.subject
end

#text_bodyObject



50
51
52
53
54
55
56
# File 'lib/mailbox_gem/message.rb', line 50

def text_body
  if mail.multipart?
    mail.text_part&.decoded
  elsif mail.mime_type.nil? || mail.mime_type == "text/plain"
    mail.body.decoded
  end
end

#toObject



30
31
32
# File 'lib/mailbox_gem/message.rb', line 30

def to
  Array(mail.to).join(", ")
end