Class: MailboxGem::Message
- Inherits:
-
Object
- Object
- MailboxGem::Message
- 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
-
#captured_at ⇒ Object
readonly
Returns the value of attribute captured_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#mail ⇒ Object
readonly
Returns the value of attribute mail.
Instance Method Summary collapse
- #attachments ⇒ Object
- #bcc ⇒ Object
- #cc ⇒ Object
- #from ⇒ Object
- #html_body ⇒ Object
-
#initialize(mail) ⇒ Message
constructor
A new instance of Message.
-
#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.
- #source ⇒ Object
- #subject ⇒ Object
- #text_body ⇒ Object
- #to ⇒ Object
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_at ⇒ Object (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 |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/mailbox_gem/message.rb', line 10 def id @id end |
#mail ⇒ Object (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
#attachments ⇒ Object
58 59 60 |
# File 'lib/mailbox_gem/message.rb', line 58 def mail. end |
#bcc ⇒ Object
38 39 40 |
# File 'lib/mailbox_gem/message.rb', line 38 def bcc Array(mail.bcc).join(", ") end |
#cc ⇒ Object
34 35 36 |
# File 'lib/mailbox_gem/message.rb', line 34 def cc Array(mail.cc).join(", ") end |
#from ⇒ Object
26 27 28 |
# File 'lib/mailbox_gem/message.rb', line 26 def from Array(mail.from).join(", ") end |
#html_body ⇒ Object
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.
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 |
#source ⇒ Object
62 63 64 |
# File 'lib/mailbox_gem/message.rb', line 62 def source mail.to_s end |
#subject ⇒ Object
22 23 24 |
# File 'lib/mailbox_gem/message.rb', line 22 def subject mail.subject end |
#text_body ⇒ Object
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 |
#to ⇒ Object
30 31 32 |
# File 'lib/mailbox_gem/message.rb', line 30 def to Array(mail.to).join(", ") end |