Module: DSIEmailHelper

Defined in:
lib/dsiemailhelper.rb,
lib/dsiemailhelper/version.rb

Constant Summary collapse

HTTPS_REGEXP =
/https?:\/\/[\S]+/
VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.connectObject



10
11
12
13
14
15
# File 'lib/dsiemailhelper.rb', line 10

def self.connect
  # using connect! because only it will throw an exception if an error occurs
  # during the connection process with gmail
  raise "Please ensure QA_INBOX_EMAIL and QA_INBOX_PASSWORD env vars are configured" unless ENV['QA_INBOX_EMAIL'] && ENV['QA_INBOX_PASSWORD']
  @inbox = Gmail.connect!(ENV['QA_INBOX_EMAIL'], ENV['QA_INBOX_PASSWORD'])
end

.filter_messages_by_time(messages, time, acceptable_offset = 0) ⇒ Object



40
41
42
43
44
45
# File 'lib/dsiemailhelper.rb', line 40

def self.filter_messages_by_time(messages, time, acceptable_offset=0)
  messages.delete_if { | message |
    message.message.date.to_time.to_f <= time.to_time.round.to_f - acceptable_offset
  }
  messages
end

.find_email_by_gmail_search(search_string, start_time, acceptable_offset = 0) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dsiemailhelper.rb', line 22

def self.find_email_by_gmail_search(search_string, start_time, acceptable_offset=0)
  message = nil
  Timeout.timeout(120) do
    while message.nil?
      message = @inbox
      sleep 0.5
    end
    message = filter_messages_by_time(@inbox.inbox.find(gm: search_string), start_time, acceptable_offset)[-1]
    message
  end
rescue Timeout::Error => e
  puts "Search string passed: #{search_string}"
  puts "Start time passed: #{start_time} (offset: #{acceptable_offset})."
  puts "Messages matching search string: #{@inbox.inbox.find(gm: search_string)}"
  puts "Message: #{message || @inbox.inbox.find(gm: search_string)}"
  raise e
end

.find_with_filters(*args) ⇒ Object



71
72
73
# File 'lib/dsiemailhelper.rb', line 71

def self.find_with_filters(*args)
  @inbox.inbox.find(*args)
end

.get_all_emailsObject



17
18
19
20
# File 'lib/dsiemailhelper.rb', line 17

def self.get_all_emails
  # returns an array of Message objects
  @inbox.inbox.emails
end

.get_email_body(message) ⇒ Object



47
48
49
50
# File 'lib/dsiemailhelper.rb', line 47

def self.get_email_body(message)
  # expects a Message object and returns the body of the email without encoding
  message.text_part.body.decoded
end

.get_most_recent_emailObject



56
57
58
59
60
61
# File 'lib/dsiemailhelper.rb', line 56

def self.get_most_recent_email
  # Noticed the most recent email in the inbox folder always has the highest index,
  # so we can use the size of the array to determine what the most recent message is
  messages = get_all_emails
  messages[messages.length - 1]
end


63
64
65
66
67
68
69
# File 'lib/dsiemailhelper.rb', line 63

def self.get_newest_password_reset_link(email)
  message = find_email_by_gmail_search("subject: Update Your Password to: #{email}", Date.today)
  body = get_email_body(message)
  # TODO: Archiving doesn't seem to be working, need to look into why
  message.delete!
  parse_link_from_body(body)
end


52
53
54
# File 'lib/dsiemailhelper.rb', line 52

def self.parse_link_from_body(decoded_message)
  decoded_message.scan(HTTPS_REGEXP)
end