class Reelagram::Mail::Fetchers::EmailFetcher

Attributes

after[R]
email[R]
label[R]
password[R]
save_path[R]

Public Class Methods

new(options = {}) click to toggle source

Available options:

  • :email - Gmail mailbox email

  • :password - Mailbox password

  • :label - Mailbox label

  • :after - Start date

  • :save_path - Temporary location for pdf files

# File lib/reelagram/mail/fetchers/base_fetcher.rb, line 18
def initialize(options = {})
  raise NotConfiguredError, "Configuration is required" unless Reelagram::Mail.configured?

  @email     = Mail.configuration.email
  @password  = Mail.configuration.password
  @label     = options.fetch(:label, self.class::DEFAULT_LABEL)
  @after     = options.fetch(:after, nil)
  @save_path = options.fetch(:save_path, "/tmp")
  @test_mode = options.fetch(:test, Mail.configuration.test)

  unless File.exists?(@save_path)
    FileUtils.mkdir_p(@save_path)
  end
end

Public Instance Methods

fetch(include_attachments = false) click to toggle source

Fetch invoices and yied a saved pdf file for further processing

Fetcher establishes connection to the google mail server with provided credentials and performs an iteration over emails with label defined by SEARCH_LABEL constant. Emails without any attachments are ignored. If attachment file already exists locally it will be overwritten.

@return [Array<String>] array with saved pdf files locations

# File lib/reelagram/mail/fetchers/base_fetcher.rb, line 42
def fetch(include_attachments = false)
  connection.peek = true if @test_mode

  results = emails.map do |message|
    STDOUT.puts "Processing email: #{message_title(message)}"

    {
      from:         message.from,
      subject:      message.subject,
      body:         message.body.decoded,
      attachments:  get_attachments(message, include_attachments) || []
    }
  end
  # Close connection
  connection.logout

  # Return all fetched files
  results
end

Private Instance Methods

connection() click to toggle source
# File lib/reelagram/mail/fetchers/base_fetcher.rb, line 64
def connection
  @connection ||= Gmail.new(@email, @password)
end
emails() click to toggle source
# File lib/reelagram/mail/fetchers/base_fetcher.rb, line 72
def emails
  if @after
    mailbox.emails(after: @after)
  else
    mailbox.emails(:unread)
  end
end
get_attachments(message, include_attachments) { |path| ... } click to toggle source
# File lib/reelagram/mail/fetchers/base_fetcher.rb, line 80
def get_attachments(message, include_attachments)
  return unless include_attachments

  if message.attachments.size == 0
    STDOUT.puts "No attachments found, skipped."
    return
  end

  atts = []

  message.attachments.each do |attachment|
    path = File.join(@save_path, attachment.filename)

    if File.exists?(path)
      STDOUT.puts "File #{path} already exists, overwriting..."
      File.unlink(path)
    end

    if save_attachment(attachment, path)
      atts << path
      yield path if block_given?
    end
  end

  atts
end
mailbox() click to toggle source
# File lib/reelagram/mail/fetchers/base_fetcher.rb, line 68
def mailbox
  connection.mailbox(@label)
end
message_title(message) click to toggle source
# File lib/reelagram/mail/fetchers/base_fetcher.rb, line 115
def message_title(message)
  "#{message.subject} (from #{message.from.first})"
end
save_attachment(attachment, path) click to toggle source
# File lib/reelagram/mail/fetchers/base_fetcher.rb, line 107
def save_attachment(attachment, path)
  file = File.new("#{@save_path}/#{attachment.filename}", "w+")
  file << attachment.decoded
  file.close

  true
end