class OnlyofficeGmailHelper::Gmail_helper

Main class of gem

Attributes

gmail[RW]

@return [Gmail] gmail object

label[RW]

@return [String] default label

password[RW]

@return [String] user password

timeout_for_mail[RW]

@return [Integer] default timeout for operation

user[RW]

@return [String] user name

Public Class Methods

new(user = EmailAccount::GMAIL_DEFAULT.login, password = EmailAccount::GMAIL_DEFAULT.password, timeout_for_mail = 10, label = nil) click to toggle source
# File lib/onlyoffice_gmail_helper.rb, line 51
def initialize(user = EmailAccount::GMAIL_DEFAULT.login, password = EmailAccount::GMAIL_DEFAULT.password, timeout_for_mail = 10, label = nil)
  @user = user
  @password = password
  @gmail = Gmail.new(user, password)
  @imap = @gmail.instance_variable_get(:@imap)
  @timeout_for_mail = timeout_for_mail
  @label = label
end

Public Instance Methods

archive_inbox() click to toggle source

Archive all inbox @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 300
def archive_inbox
  OnlyofficeLoggerHelper.log("Start achieving  all messaged in inbox on mail: #{@user}")
  @gmail.inbox.emails.each(&:archive!) if mail_inbox_count.nonzero?
  OnlyofficeLoggerHelper.log("Finished achieving  all messaged in inbox on mail: #{@user}")
end
check_messages_for_message_with_portal_address(message, current_portal_full_name, times: 300) click to toggle source

Check message for message with portal @param [String] message title @param [String] current_portal_full_name name @param [Integer] times to wait @return [Boolean] is messag found

# File lib/onlyoffice_gmail_helper.rb, line 232
def check_messages_for_message_with_portal_address(message, current_portal_full_name, times: 300)
  times.times do
    messages_array = mailbox.emails(:unread, search: current_portal_full_name.to_s)
    messages_array.each do |current_mail|
      next unless message_found?(current_mail.message.subject, message.title)

      OnlyofficeLoggerHelper.log('Email successfully found and removed')
      current_mail.delete!
      return true
    end
    sleep 1
  end
  false
end
check_unread_messages_for_message(mail_message) click to toggle source

Check unread messages for message @param [String] mail_message to find @return [Boolean] result

# File lib/onlyoffice_gmail_helper.rb, line 209
def check_unread_messages_for_message(mail_message)
  messages = get_unread_messages
  timer = 0
  message_found = false
  while timer < @timeout_for_mail && message_found == false
    messages.each do |current_unread_mail|
      # p current_unread_mail
      if current_unread_mail == mail_message
        delete_messages(current_unread_mail)
        return true
      end
    end
    messages = get_unread_messages
    timer += 1
  end
  false
end
delete_all_message_contains(contain_string) click to toggle source

@param [String] contain_string message to delete @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 308
def delete_all_message_contains(contain_string)
  OnlyofficeLoggerHelper.log("Messages containing #{contain_string} will be deleted")
  messages_array = mailbox.emails(:unread, search: contain_string)
  messages_array.each(&:delete!)
end
delete_all_messages() click to toggle source

Delete all messsages @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 291
def delete_all_messages
  OnlyofficeLoggerHelper.log("Start deleting all messaged on mail: #{@user}")
  mailbox.emails.each(&:delete!)
  @gmail.logout
  OnlyofficeLoggerHelper.log("Finished deleting all messaged on mail: #{@user}")
end
delete_from_sender(string) click to toggle source

Delete all mail from sender @param [String] string messanger @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 404
def delete_from_sender(string)
  mailbox.emails(from: string).each(&:delete!)
end
delete_message_with_portal_address(message, current_portal_full_name) click to toggle source

Delete message with portal address @param [String] message title to delete @param [String] current_portal_full_name to delete @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 251
def delete_message_with_portal_address(message, current_portal_full_name)
  300.times do
    messages_array = mailbox.emails(:unread, search: current_portal_full_name.to_s)
    messages_array.each do |current_mail|
      if message.title == current_mail.message.subject
        current_mail.delete!
        return true
      else
        begin
          current_mail.mark(:unread)
        rescue StandardError
          Exception
        end
      end
    end
  end
end
delete_messages(message) click to toggle source

Delete specific message @param [String] message title to delete @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 272
def delete_messages(message)
  message = [message] unless message.is_a?(Array)
  message.each do |message_to_delete|
    mailbox.emails(:unread).each do |current_mail|
      if message_to_delete.title == current_mail.message.subject
        current_mail.delete!
      else
        begin
          current_mail.mark(:unread)
        rescue StandardError
          Exception
        end
      end
    end
  end
end
get_body_by_subject_email(subject, portal_name) click to toggle source

Get body by email subject @param [String] subject to get @param [String] portal_name to filter @return [String] body

# File lib/onlyoffice_gmail_helper.rb, line 479
def get_body_by_subject_email(subject, portal_name)
  p 'get_body_by_subject_email'
  300.times do |current|
    p "current time: #{current}"
    messages_array = mailbox.emails(:unread, search: portal_name.to_s)
    messages_array.each do |current_mail|
      current_subject = current_mail.message.subject
      p "current_subject: #{current_subject}"
      if message_found?(current_subject, subject)
        body_text = current_mail.message.text_part.body.decoded.force_encoding('utf-8').encode('UTF-8').gsub(/\s+/, ' ').strip
        return body_text
      end
    end
  end
  nil
end
get_body_message_by_title(portal_address, subject, time = 300, delete = true) { |: { search: portal_address }| ... } click to toggle source

Get mail body by title @param [String] portal_address to filter @param [String] subject to find @param [Integer] time to wait @param [Boolean] delete if needed @return [MailMessage] found message

# File lib/onlyoffice_gmail_helper.rb, line 157
def get_body_message_by_title(portal_address, subject, time = 300, delete = true)
  start_time = Time.now
  flags = block_given? ? yield : { search: portal_address.to_s }
  while Time.now - start_time < time
    messages_array = mailbox.emails(:unread, flags)
    messages_array.each do |current_mail|
      next unless message_found?(current_mail.message.subject, subject)

      body = begin
        current_mail.html_part.body.decoded.force_encoding('utf-8').encode('UTF-8')
      rescue StandardError
        Exception
      end
      current_mail.delete! if delete
      return body
    end
  end
  nil
end
get_body_message_by_title_from_mail(current_portal_full, title1 = 'Welcome to ONLYOFFICE™ Portal!', title2 = 'Добро пожаловать на портал TeamLab!', delete = true, _to_mail = nil) click to toggle source

Get body message by title @param [String] current_portal_full portal name to search @param [String] title1 title to filter @param [String] title2 title to filter @param [Boolean] delete this message @param [Boolean] _to_mail unused @return [MailMessage] found message

# File lib/onlyoffice_gmail_helper.rb, line 120
def get_body_message_by_title_from_mail(current_portal_full, title1 = 'Welcome to ONLYOFFICE™ Portal!', title2 = 'Добро пожаловать на портал TeamLab!', delete = true, _to_mail = nil)
  mail_not_found = true
  attempt = 0
  while mail_not_found
    messages_array = mailbox.emails(:unread, search: current_portal_full.to_s)
    messages_array.each do |current_mail|
      current_subject = current_mail.message.subject
      a = current_subject.include? title1
      b = current_subject.include? title2
      if a || b
        current_subject = begin
          current_mail.html_part.body.decoded.force_encoding('utf-8').encode('UTF-8')
        rescue StandardError
          Exception
        end
        current_mail.delete! if current_subject == 'Welcome to Your TeamLab Portal!'
        if current_subject.include? current_portal_full
          current_mail.delete! if delete
          return current_subject
        end
      else
        raise "Message with title: #{title1} not found after #{attempt} attempt" if attempt == 10

        sleep 10
        attempt += 1
        current_mail.delete! if delete
      end
    end
  end
end
get_current_date(date_str) click to toggle source

received mail in format “Thu, 23 Jan 2014 15:34:57 +0400”. Day of week maymay not be present @param [String] date_str original string @return [Hash] date

# File lib/onlyoffice_gmail_helper.rb, line 201
def get_current_date(date_str)
  data_arr = date_str.split.reverse
  { day: data_arr[4].to_i, hour: data_arr[1].split(':')[0].to_i, minute: data_arr[1].split(':')[1].to_i }
end
get_labels() click to toggle source

If not returning nested levels please change content of file +/home/#{user}/.rvm/gems/#{gemset}/gems/gmail-0.4.0/lib/gmail/labels.rb+ to content of that file: github.com/jgrevich/gmail/blob/6ed88950bd631696aeb1bc4b9133b03d1ae4055f/lib/gmail/labels.rb @return [Array<String>] list of all labels

# File lib/onlyoffice_gmail_helper.rb, line 451
def get_labels
  @gmail.labels.all
end
get_unread_mails_with_tags() click to toggle source

Get list of unread mails with tags @return [Array<MailMessage>] result

# File lib/onlyoffice_gmail_helper.rb, line 457
def get_unread_mails_with_tags
  refresh
  array_of_mail = []
  mailbox.emails(:unread).each do |current_mail|
    current_title = current_mail.message.subject
    current_subject = begin
      current_mail.html_part.body.decoded.force_encoding('utf-8').encode('UTF-8')
    rescue StandardError
      Exception
    end
    current_mail.mark(:unread)
    reply_to = current_mail.reply_to[0] unless current_mail.reply_to.nil?
    current_tag = current_mail
    array_of_mail << MailMessage.new(current_title, current_subject, reply_to, current_tag)
  end
  array_of_mail
end
get_unread_messages() click to toggle source

@return [Array<Message>] list of underad messages

# File lib/onlyoffice_gmail_helper.rb, line 178
def get_unread_messages
  refresh
  array_of_mail = []
  mailbox.emails(:unread).reverse_each do |current_mail|
    current_title = current_mail.message.subject
    current_subject = begin
      current_mail.html_part.body.decoded
                  .force_encoding('utf-8').encode('UTF-8')
    rescue StandardError
      Exception
    end
    current_mail.mark(:unread)
    reply_to = current_mail.reply_to[0] unless current_mail.reply_to == []
    array_of_mail << MailMessage.new(current_title,
                                     current_subject,
                                     reply_to)
  end
  array_of_mail
end
logout() click to toggle source

Perform logout @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 76
def logout
  @gmail.logout
  @imap.disconnect until @imap.disconnected?
rescue StandardError
  Exception
end
mail_in_label_with_date(string, date_start, date_end, to = nil) click to toggle source

List all mail in label with date @param [String] string label @param [Date] date_start to find @param [Date] date_end to find @param [String] to whom message send @return [Array<MailMessage>] list of results

# File lib/onlyoffice_gmail_helper.rb, line 383
def mail_in_label_with_date(string, date_start, date_end, to = nil)
  array_of_mail = []
  @gmail.mailbox(string).emails(after: date_start, before: date_end, to: to).each do |current_mail|
    current_title = current_mail.message.subject
    current_subject = begin
      current_mail.html_part.body.decoded
                  .force_encoding('utf-8').encode('UTF-8')
    rescue StandardError
      Exception
    end
    reply_to = current_mail.reply_to[0] unless current_mail.reply_to.nil?
    array_of_mail << MailMessage.new(current_title,
                                     current_subject,
                                     reply_to, Time.parse(current_mail.date))
  end
  array_of_mail
end
mail_inbox_count() click to toggle source

@return [Integer] count message in inbox

# File lib/onlyoffice_gmail_helper.rb, line 371
def mail_inbox_count
  count = @gmail.inbox.emails.count
  OnlyofficeLoggerHelper.log("#{count} mails in inbox of #{@user}")
  count
end
mailbox() click to toggle source

Select mailbox @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 62
def mailbox
  if @label
    if @label == :inbox
      @gmail.inbox
    else
      @gmail.mailbox(@label)
    end
  else
    @gmail.mailbox('[Gmail]/All Mail')
  end
end
mark_all_unread() click to toggle source

Mark all messages as unread @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 410
def mark_all_unread
  mailbox.emails.each do |current_mail|
    current_mail.mark(:unread)
  end
end
refresh() click to toggle source

Refresh connection data @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 85
def refresh
  logout
  @gmail = Gmail.new(@user, @password)
end
reply_mail(mail_to_reply, reply_body) click to toggle source

Reply to mail @param [String] mail_to_reply @param [String] reply_body to do @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 318
def reply_mail(mail_to_reply, reply_body)
  messages = get_unread_messages
  timer = 0
  message_found = false
  while timer < @timeout_for_mail && message_found == false
    messages.each do |current_unread_mail|
      next unless current_unread_mail == mail_to_reply

      email = @gmail.compose do
        to("#{current_unread_mail.reply_to.mailbox}@#{current_unread_mail.reply_to.host}".to_s)
        subject "Re: #{current_unread_mail.title}"
        body reply_body
      end
      email.deliver!
      delete_messages(current_unread_mail)
      return true
    end
    messages = get_unread_messages
    timer += 1
  end
  false
end
send_mail(email, title, body, attachment = nil) click to toggle source

Send mail @param [String] email to send @param [String] title to send @param [String] body to send @param [String] attachment to send @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 347
def send_mail(email, title, body, attachment = nil)
  email = @gmail.compose do
    to email
    subject title
    body body
    add_file attachment unless attachment.nil?
  end
  email.deliver!
  OnlyofficeLoggerHelper.log("send_mail(#{email}, #{title}, #{body}, #{attachment})")
end
send_mail_test_result(mail, title, array_results) click to toggle source

Send mail test result @param [String] mail to send @param [String] title to send @param [Array<String>] array_results test data @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 421
def send_mail_test_result(mail, title, array_results)
  body = ''
  array_results.each do |current_result|
    current_result[1] = 'OK' if current_result[1].nil?
    body = "#{body}#{current_result[0]}\t#{current_result[1]}\n"
  end

  if mail.is_a?(Array)
    mail.each do |current_mail_user|
      email = @gmail.compose do
        to current_mail_user
        subject title
        body body
      end
      email.deliver!
    end
  else
    email = @gmail.compose do
      to mail
      subject title
      body body
    end
    email.deliver!
  end
end
send_notification(email, test_name, error, mail_title = 'Teamlab Daily Check') click to toggle source

Send notification @param [String] email to send @param [String] test_name - name of test @param [String] error - error to send @param [String] mail_title to send @return [nil]

# File lib/onlyoffice_gmail_helper.rb, line 364
def send_notification(email, test_name, error, mail_title = 'Teamlab Daily Check')
  body = "Fail in #{test_name}\n" \
         "Error text: \n\t #{error}"
  send_mail(email, mail_title, body)
end
wait_until_unread_message(title, timeout = @timeout_for_mail, period = 60) click to toggle source

Wait until unread message exists @param [String] title message to wait @param [Integer] timeout to wait @param [Integer] period sleep between tries @return [MailMessage] found message

# File lib/onlyoffice_gmail_helper.rb, line 95
def wait_until_unread_message(title,
                              timeout = @timeout_for_mail, period = 60)
  counter = 0
  message_found = false
  while counter < timeout && !message_found
    @gmail.inbox.emails.each do |current_mail|
      next unless current_mail.subject.include?(title)

      message = MailMessage.new(current_mail.subject,
                                current_mail.html_part.body)
      return message
    end
    sleep period
    refresh
  end
  raise "Message with title: #{title} not found for #{timeout * 60} seconds"
end

Private Instance Methods

message_found?(given, needed) click to toggle source
# File lib/onlyoffice_gmail_helper.rb, line 498
def message_found?(given, needed)
  given.to_s.upcase.include? needed.to_s.upcase
end