Class: Reelagram::Mail::Fetchers::EmailFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/reelagram/mail/fetchers/base_fetcher.rb

Direct Known Subclasses

ShippingFetcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EmailFetcher

Available options:

  • :email - Gmail mailbox email

  • :password - Mailbox password

  • :label - Mailbox label

  • :after - Start date

  • :save_path - Temporary location for pdf files

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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[:label]     || self.class::DEFAULT_LABEL
  @after     = options[:after]
  @save_path = options[:save_path] || "/tmp"

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

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



8
9
10
# File 'lib/reelagram/mail/fetchers/base_fetcher.rb', line 8

def after
  @after
end

#emailObject (readonly)

Returns the value of attribute email.



8
9
10
# File 'lib/reelagram/mail/fetchers/base_fetcher.rb', line 8

def email
  @email
end

#labelObject (readonly)

Returns the value of attribute label.



8
9
10
# File 'lib/reelagram/mail/fetchers/base_fetcher.rb', line 8

def label
  @label
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/reelagram/mail/fetchers/base_fetcher.rb', line 8

def password
  @password
end

#save_pathObject (readonly)

Returns the value of attribute save_path.



8
9
10
# File 'lib/reelagram/mail/fetchers/base_fetcher.rb', line 8

def save_path
  @save_path
end

Instance Method Details

#fetch(include_attachments = false) ⇒ Array<String>

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.

Returns:

  • (Array<String>)

    array with saved pdf files locations



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/reelagram/mail/fetchers/base_fetcher.rb', line 41

def fetch(include_attachments = false)
  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