class Howitzer::MailAdapters::Gmail

This class represents Gmail mail adapter

Public Class Methods

find(recipient, subject, wait:) click to toggle source

Finds an email in storage @param recipient [String] an email @param subject [String] @param wait [Integer] how much time is required to wait an email @raise [EmailNotFoundError] if blank message

# 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

Private Class Methods

find_retry_params(wait) click to toggle source
# File lib/howitzer/mail_adapters/gmail.rb, line 84
def self.find_retry_params(wait)
  {
    timeout: wait,
    sleep: Howitzer.mail_sleep_time,
    silent: true,
    logger: Howitzer::Log,
    on: Howitzer::EmailNotFoundError
  }
end
get_message(recipient, subject) click to toggle source
# File lib/howitzer/mail_adapters/gmail.rb, line 76
def self.get_message(recipient, subject)
  message = Howitzer::GmailApi::Client.new.find_message(recipient, subject)
  raise Howitzer::EmailNotFoundError if message.blank?

  message
end

Public Instance Methods

html_body() click to toggle source

@return [String] html body of the email message

# File lib/howitzer/mail_adapters/gmail.rb, line 32
def html_body
  message.html_part.to_s
end
mail_from() click to toggle source

@return [String] an email address specified in ‘From` field

# File lib/howitzer/mail_adapters/gmail.rb, line 44
def mail_from
  "#{message.from[0]['mailbox']}@#{message.from[0]['host']}"
end
mime_part() click to toggle source

@return [Array] attachments

# File lib/howitzer/mail_adapters/gmail.rb, line 62
def mime_part
  message.attachments
end
mime_part!() click to toggle source

@raise [NoAttachmentsError] if no attachments present @return [Array] attachments

# File lib/howitzer/mail_adapters/gmail.rb, line 69
def mime_part!
  files = mime_part
  return files if files.present?

  raise Howitzer::NoAttachmentsError, 'No attachments were found.'
end
plain_text_body() click to toggle source

@return [String] plain text body of the email message

# File lib/howitzer/mail_adapters/gmail.rb, line 26
def plain_text_body
  message.body.to_s
end
received_time() click to toggle source

@return [String] when email was received

# File lib/howitzer/mail_adapters/gmail.rb, line 56
def received_time
  Time.parse(message.date).strftime('%F %T')
end
recipients() click to toggle source

@return [Array] recipient emails

# File lib/howitzer/mail_adapters/gmail.rb, line 50
def recipients
  message.to.inject([]) { |ar, to| ar << "#{to['mailbox']}@#{to['host']}" }
end
text() click to toggle source

@return [String] stripped text

# File lib/howitzer/mail_adapters/gmail.rb, line 38
def text
  message.text_part.to_s
end