Class: OnlyofficeGmailHelper::Gmail_helper
- Inherits:
-
Object
- Object
- OnlyofficeGmailHelper::Gmail_helper
- Defined in:
- lib/onlyoffice_gmail_helper.rb
Overview
Main class of gem
Instance Attribute Summary collapse
-
#gmail ⇒ Gmail
Gmail object.
-
#label ⇒ String
Default label.
-
#password ⇒ String
User password.
-
#timeout_for_mail ⇒ Integer
Default timeout for operation.
-
#user ⇒ String
User name.
Instance Method Summary collapse
-
#check_messages_for_message_with_portal_address(message, current_portal_full_name, times: 300) ⇒ Boolean
Check message for message with portal.
-
#delete_all_messages ⇒ nil
Delete all messsages.
-
#delete_messages(message) ⇒ nil
Delete specific message.
-
#get_body_message_by_title(portal_address, subject, time = 300, delete = true) ⇒ MailMessage
Get mail body by title.
-
#initialize(user = nil, password = nil, timeout_for_mail = 10, label = nil) ⇒ Gmail_helper
constructor
A new instance of Gmail_helper.
-
#logout ⇒ nil
Perform logout.
-
#mail_in_label_with_date(string, date_start, date_end, to = nil) ⇒ Array<MailMessage>
List all mail in label with date.
-
#mailbox ⇒ nil
Select mailbox.
-
#send_mail(email, title, body, attachment = nil) ⇒ nil
Send mail.
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.default_account.login @password = password || EmailAccount.default_account.password @gmail = Gmail.new(user, password) @imap = @gmail.instance_variable_get(:@imap) @timeout_for_mail = timeout_for_mail @label = label end |
Instance Attribute Details
#gmail ⇒ Gmail
Returns gmail object.
14 15 16 |
# File 'lib/onlyoffice_gmail_helper.rb', line 14 def gmail @gmail end |
#label ⇒ String
Returns default label.
22 23 24 |
# File 'lib/onlyoffice_gmail_helper.rb', line 22 def label @label end |
#password ⇒ String
Returns user password.
18 19 20 |
# File 'lib/onlyoffice_gmail_helper.rb', line 18 def password @password end |
#timeout_for_mail ⇒ Integer
Returns default timeout for operation.
20 21 22 |
# File 'lib/onlyoffice_gmail_helper.rb', line 20 def timeout_for_mail @timeout_for_mail end |
#user ⇒ String
Returns 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/onlyoffice_gmail_helper.rb', line 87 def (, current_portal_full_name, times: 300) times.times do = mailbox.emails(:unread, search: current_portal_full_name.to_s) .each do |current_mail| next unless (current_mail..subject, .title) OnlyofficeLoggerHelper.log('Email successfully found and removed') current_mail.delete! return true end sleep 1 end false end |
#delete_all_messages ⇒ nil
Delete all messsages
124 125 126 127 128 129 |
# File 'lib/onlyoffice_gmail_helper.rb', line 124 def 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
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 () = [] unless .is_a?(Array) .each do || mailbox.emails(:unread).each do |current_mail| if .title == current_mail..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
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 (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 = mailbox.emails(:unread, flags) .each do |current_mail| next unless (current_mail..subject, subject) body = begin (current_mail) rescue StandardError Exception end current_mail.delete! if delete return body end end nil end |
#logout ⇒ nil
Perform logout
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
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..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 |
#mailbox ⇒ nil
Select mailbox
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
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/onlyoffice_gmail_helper.rb', line 137 def send_mail(email, title, body, = nil) email = @gmail.compose do to email subject title body body add_file unless .nil? end email.deliver! OnlyofficeLoggerHelper.log("send_mail(#{email}, #{title}, #{body}, #{})") end |