Class: Email

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/howitzer/email.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Email

Creates new email with message

Parameters:

  • message - email message


16
17
18
# File 'lib/howitzer/email.rb', line 16

def initialize(message)
  @message = message
end

Class Method Details

.find(recipient, subject) ⇒ Object

Search mail by recepient and subject.

Parameters:

  • recepient - recepient's email address

  • subject - email subject


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/howitzer/email.rb', line 41

def self.find(recipient, subject)
  message = {}
  retryable(timeout: settings.timeout_small, sleep: settings.timeout_short, silent: true, logger: log, on: Howitzer::EmailNotFoundError) do
    events = Mailgun::Connector.instance.client.get("#{Mailgun::Connector.instance.domain}/events", event: 'stored')
    event = events.to_h['items'].find do |hash|
      hash['message']['recipients'].first == recipient && hash['message']['headers']['subject'] == subject
    end
    if event
      message = Mailgun::Connector.instance.client.get("domains/#{Mailgun::Connector.instance.domain}/messages/#{event['storage']['key']}").to_h
    else
      raise Howitzer::EmailNotFoundError.new('Message not received yet, retry...')
    end
  end
  log.error Howitzer::EmailNotFoundError, "Message with subject '#{subject}' for recipient '#{recipient}' was not found." if message.empty?
  new(message)
end

.find_by_recipient(recipient) ⇒ Object

Search mail by recepient

Parameters:

  • recepient - recepient's email address


28
29
30
# File 'lib/howitzer/email.rb', line 28

def self.find_by_recipient(recipient)
  find(recipient, self::SUBJECT)
end

Instance Method Details

#get_mime_partObject

Allows to get email MIME attachment


126
127
128
129
130
131
132
133
# File 'lib/howitzer/email.rb', line 126

def get_mime_part
  files = @message['attachments']
  if files.empty?
    log.error Howitzer::NoAttachmentsError, 'No attachments where found.'
    return
  end
  files
end

#html_bodyObject

Returns html body of email message


72
73
74
# File 'lib/howitzer/email.rb', line 72

def html_body
  @message['stripped-html']
end

#mail_fromObject

Returns who has send email data in format: User Name <[email protected]>


90
91
92
# File 'lib/howitzer/email.rb', line 90

def mail_from
  @message['From']
end

#plain_text_bodyObject

Returns plain text body of email message


63
64
65
# File 'lib/howitzer/email.rb', line 63

def plain_text_body
  @message['body-plain']
end

#received_timeObject

Returns email received time in format:


108
109
110
# File 'lib/howitzer/email.rb', line 108

def received_time
  @message['Received'][/\w+, \d+ \w+ \d+ \d+:\d+:\d+ -\d+ \(\w+\)$/]
end

#recipientsObject

Returns array of recipients who has received current email


99
100
101
# File 'lib/howitzer/email.rb', line 99

def recipients
  @message['To'].split ', '
end

#sender_emailObject

Returns sender user email


117
118
119
# File 'lib/howitzer/email.rb', line 117

def sender_email
  @message['sender']
end

#textObject

Returns mail text


81
82
83
# File 'lib/howitzer/email.rb', line 81

def text
  @message['stripped-text']
end