lib/dsiemailhelper.rb in dsiemailhelper-0.0.1 vs lib/dsiemailhelper.rb in dsiemailhelper-0.0.2
- old
+ new
@@ -1,69 +1,70 @@
-require 'timeout'
-require 'gmail'
-
-
-module DSIEmailHelper
-
- HTTPS_REGEXP = /https?:\/\/[\S]+/
-
- 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
-
- def self.get_all_emails
- # returns an array of Message objects
- @inbox.inbox.emails
- end
-
- def self.find_email_by_gmail_search(search_string, start_time, acceptable_offset=0)
- message = nil
- Timeout.timeout(120) do
- while filter_messages_by_time(@inbox.inbox.find(gm: search_string), start_time, acceptable_offset).length < 1
- 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
-
- 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
-
- 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
-
- def self.parse_link_from_body(decoded_message)
- decoded_message.scan(HTTPS_REGEXP)
- end
-
- 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
-
- 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
-
-end
+require "dsiemailhelper/version"
+require 'timeout'
+require 'gmail'
+
+
+module DSIEmailHelper
+
+ HTTPS_REGEXP = /https?:\/\/[\S]+/
+
+ 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
+
+ def self.get_all_emails
+ # returns an array of Message objects
+ @inbox.inbox.emails
+ end
+
+ def self.find_email_by_gmail_search(search_string, start_time, acceptable_offset=0)
+ message = nil
+ Timeout.timeout(120) do
+ while filter_messages_by_time(@inbox.inbox.find(gm: search_string), start_time, acceptable_offset).length < 1
+ 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
+
+ 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
+
+ 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
+
+ def self.parse_link_from_body(decoded_message)
+ decoded_message.scan(HTTPS_REGEXP)
+ end
+
+ 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
+
+ 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
+
+end