Class: OnlyofficeGmailHelper::Gmail_helper

Inherits:
Object
  • Object
show all
Defined in:
lib/onlyoffice_gmail_helper.rb

Overview

Main class of gem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, password = nil, timeout_for_mail = 10, label = nil) ⇒ Gmail_helper

Returns a new instance of Gmail_helper.



24
25
26
27
28
29
30
31
# File 'lib/onlyoffice_gmail_helper.rb', line 24

def initialize(user = nil, password = nil, timeout_for_mail = 10, label = nil)
  @user = user || EmailAccount..
  @password = password || EmailAccount..password
  @gmail = Gmail.new(user, password)
  @imap = @gmail.instance_variable_get(:@imap)
  @timeout_for_mail = timeout_for_mail
  @label = label
end

Instance Attribute Details

#gmailGmail

Returns gmail object.

Returns:

  • (Gmail)

    gmail object



14
15
16
# File 'lib/onlyoffice_gmail_helper.rb', line 14

def gmail
  @gmail
end

#labelString

Returns default label.

Returns:

  • (String)

    default label



22
23
24
# File 'lib/onlyoffice_gmail_helper.rb', line 22

def label
  @label
end

#passwordString

Returns user password.

Returns:

  • (String)

    user password



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

def password
  @password
end

#timeout_for_mailInteger

Returns default timeout for operation.

Returns:

  • (Integer)

    default timeout for operation



20
21
22
# File 'lib/onlyoffice_gmail_helper.rb', line 20

def timeout_for_mail
  @timeout_for_mail
end

#userString

Returns user name.

Returns:

  • (String)

    user name



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

def user
  @user
end

Instance Method Details

#check_messages_for_message_with_portal_address(message, current_portal_full_name, times: 300) ⇒ Boolean

Check message for message with portal

Parameters:

  • message (String)

    title

  • current_portal_full_name (String)

    name

  • times (Integer) (defaults to: 300)

    to wait

Returns:

  • (Boolean)

    is messag found



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/onlyoffice_gmail_helper.rb', line 87

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

#delete_all_messagesnil

Delete all messsages

Returns:

  • (nil)


124
125
126
127
128
129
# File 'lib/onlyoffice_gmail_helper.rb', line 124

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_messages(message) ⇒ nil

Delete specific message

Parameters:

  • message (String)

    title to delete

Returns:

  • (nil)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/onlyoffice_gmail_helper.rb', line 105

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_message_by_title(portal_address, subject, time = 300, delete = true) ⇒ MailMessage

Get mail body by title

Parameters:

  • portal_address (String)

    to filter

  • subject (String)

    to find

  • time (Integer) (defaults to: 300)

    to wait

  • delete (Boolean) (defaults to: true)

    if needed

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/onlyoffice_gmail_helper.rb', line 62

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
        message_body(current_mail)
      rescue StandardError
        Exception
      end
      current_mail.delete! if delete
      return body
    end
  end
  nil
end

#logoutnil

Perform logout

Returns:

  • (nil)


49
50
51
52
53
54
# File 'lib/onlyoffice_gmail_helper.rb', line 49

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) ⇒ Array<MailMessage>

List all mail in label with date

Parameters:

  • string (String)

    label

  • date_start (Date)

    to find

  • date_end (Date)

    to find

  • to (String) (defaults to: nil)

    whom message send

Returns:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/onlyoffice_gmail_helper.rb', line 154

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

#mailboxnil

Select mailbox

Returns:

  • (nil)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/onlyoffice_gmail_helper.rb', line 35

def mailbox
  if @label
    if @label == :inbox
      @gmail.inbox
    else
      @gmail.mailbox(@label)
    end
  else
    @gmail.mailbox('[Gmail]/All Mail')
  end
end

#send_mail(email, title, body, attachment = nil) ⇒ nil

Send mail

Parameters:

  • email (String)

    to send

  • title (String)

    to send

  • body (String)

    to send

  • attachment (String) (defaults to: nil)

    to send

Returns:

  • (nil)


137
138
139
140
141
142
143
144
145
146
# File 'lib/onlyoffice_gmail_helper.rb', line 137

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