Class: Howitzer::MailAdapters::Gmail

Inherits:
Abstract
  • Object
show all
Defined in:
lib/howitzer/mail_adapters/gmail.rb

Overview

This class represents Gmail mail adapter

Instance Attribute Summary

Attributes inherited from Abstract

#message

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#initialize, #sender_email

Constructor Details

This class inherits a constructor from Howitzer::MailAdapters::Abstract

Class Method Details

.find(recipient, subject, wait:) ⇒ Object

Finds an email in storage

Parameters:

  • recipient (String)

    an email

  • subject (String)
  • wait (Integer)

    how much time is required to wait an email

Raises:



15
16
17
18
19
20
21
# File 'lib/howitzer/mail_adapters/gmail.rb', line 15

def self.find(recipient, subject, wait:)
  message = {}
  retryable(find_retry_params(wait)) { message = get_message(recipient, subject) }
  return new(message) if message.present?
  raise Howitzer::EmailNotFoundError,
        "Message with subject '#{subject}' for recipient '#{recipient}' was not found."
end

Instance Method Details

#html_bodyString

Returns html body of the email message

Returns:

  • (String)

    html body of the email message



31
32
33
# File 'lib/howitzer/mail_adapters/gmail.rb', line 31

def html_body
  message.html_part.to_s
end

#mail_fromString

Returns an email address specified in `From` field

Returns:

  • (String)

    an email address specified in `From` field



43
44
45
# File 'lib/howitzer/mail_adapters/gmail.rb', line 43

def mail_from
  "#{message.from[0]['mailbox']}@#{message.from[0]['host']}"
end

#mime_partArray

Returns attachments

Returns:

  • (Array)

    attachments



61
62
63
# File 'lib/howitzer/mail_adapters/gmail.rb', line 61

def mime_part
  message.attachments
end

#mime_part!Array

Returns attachments

Returns:

  • (Array)

    attachments

Raises:



68
69
70
71
72
# File 'lib/howitzer/mail_adapters/gmail.rb', line 68

def mime_part!
  files = mime_part
  return files if files.present?
  raise Howitzer::NoAttachmentsError, 'No attachments were found.'
end

#plain_text_bodyString

Returns plain text body of the email message

Returns:

  • (String)

    plain text body of the email message



25
26
27
# File 'lib/howitzer/mail_adapters/gmail.rb', line 25

def plain_text_body
  message.body.to_s
end

#received_timeString

Returns when email was received

Returns:

  • (String)

    when email was received



55
56
57
# File 'lib/howitzer/mail_adapters/gmail.rb', line 55

def received_time
  Time.parse(message.date).strftime('%F %T')
end

#recipientsArray

Returns recipient emails

Returns:

  • (Array)

    recipient emails



49
50
51
# File 'lib/howitzer/mail_adapters/gmail.rb', line 49

def recipients
  message.to.inject([]) { |ar, to| ar << "#{to['mailbox']}@#{to['host']}" }
end

#textString

Returns stripped text

Returns:

  • (String)

    stripped text



37
38
39
# File 'lib/howitzer/mail_adapters/gmail.rb', line 37

def text
  message.text_part.to_s
end