Class: MailFetcher::GmailClient

Inherits:
Object
  • Object
show all
Includes:
RetryHelper
Defined in:
lib/mail_fetcher/gmail_client.rb

Constant Summary collapse

HOST =
'imap.gmail.com'
PORT =
993

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RetryHelper

#eventually

Constructor Details

#initialize(account, client_id, client_secret, refresh_token) ⇒ GmailClient

Returns a new instance of GmailClient.



16
17
18
19
20
21
# File 'lib/mail_fetcher/gmail_client.rb', line 16

def initialize(, client_id, client_secret, refresh_token)
  @account = 
  @client_id = client_id
  @client_secret = client_secret
  @refresh_token = refresh_token
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#find(recipient, subject = '', wait = MailFetcher::Client.max_wait_time) ⇒ Object



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

def find(recipient, subject='', wait=MailFetcher::Client.max_wait_time)
  @connection ||= authenticated_connection

  message_id = eventually(:tries => wait, :delay => 1) do
    begin
      @connection.examine('INBOX')
      search_filter = ['TO', recipient, 'SUBJECT', subject]
      results = @connection.search(search_filter)
      logger.debug("Inbox contains #{results.length} messages matching search criteria") if logger
      results.first
    rescue => e
      logger.error("Error while trying trying to find a message in INBOX (#{e.message})") if logger
      nil
    end
  end

  message_id ? GmailMessage.new(@connection, message_id) : nil
end