class EmailGraph::GmailFetcher::Fetcher

Attributes

access_token[RW]
batch_size[RW]
email[RW]

Public Class Methods

new(email: nil, access_token: nil) click to toggle source
# File lib/email_graph/gmail_fetcher.rb, line 11
def initialize(email: nil, access_token: nil)
  @email = email
  @access_token = access_token
  @batch_size = 500
end

Public Instance Methods

count_messages(mailboxes: ['INBOX']) click to toggle source
# File lib/email_graph/gmail_fetcher.rb, line 17
def count_messages(mailboxes: ['INBOX'])
  mailboxes.inject(0){ |r, m| r + imap.status(m, ['MESSAGES'])['MESSAGES'] }
end
each_envelope(mailboxes: ['INBOX'], batch_size: nil) { |e| ... } click to toggle source
# File lib/email_graph/gmail_fetcher.rb, line 28
def each_envelope(mailboxes: ['INBOX'], batch_size: nil)
  mailboxes.each do |mailbox|
    # Needed before fetching
    imap.examine(mailbox)

    batch_size ||= @batch_size
    limit = count_messages(mailboxes: [mailbox])

    (1..limit).each_slice(batch_size) do |range|
      envelope_batch = imap.fetch(range, 'ENVELOPE') || []
      envelope_batch.each{ |e| yield e if block_given? }
      puts "Fetched #{range.last}/#{limit}"
    end
  end
end
each_message(mailboxes: ['INBOX'], batch_size: nil) { |m| ... } click to toggle source
# File lib/email_graph/gmail_fetcher.rb, line 21
def each_message(mailboxes: ['INBOX'], batch_size: nil)
  each_envelope(mailboxes: mailboxes, batch_size: batch_size) do |e|
    m = Message.from_net_imap_envelope(e)
    yield m if block_given?
  end
end
imap() click to toggle source
# File lib/email_graph/gmail_fetcher.rb, line 50
def imap
  @imap ||= imap_connect
end
imap_connect() click to toggle source
# File lib/email_graph/gmail_fetcher.rb, line 44
def imap_connect
  Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false).tap do |imap|
    imap.authenticate('XOAUTH2', email, access_token)
  end
end