Class: MailProcessor::MailDir

Inherits:
Base
  • Object
show all
Defined in:
lib/mail_processor/mail_dir.rb

Instance Method Summary collapse

Methods inherited from Base

log, #log

Constructor Details

#initialize(options = {}, &block) ⇒ MailDir

Returns a new instance of MailDir.



9
10
11
12
13
14
15
# File 'lib/mail_processor/mail_dir.rb', line 9

def initialize options = {}, &block
  @attributes = {
    :glob => "#{ENV['HOME']}/MailDir/new/*",
  }
  merge_to_attributes(options)
  instance_eval &block if block_given?
end

Instance Method Details

#glob(v = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/mail_processor/mail_dir.rb', line 18

def glob v = nil
  if v
    @attributes[:glob] = v
  else
    @attributes[:glob]
  end
end

#process(options = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mail_processor/mail_dir.rb', line 27

def process(options = {}, &block)
  a = @attributes.merge(options)

  count = 0
  Dir.glob(a[ :glob ]) do |filename|
    yield(File.read_binary(filename))
    FileUtils::rm(filename)
    count += 1
  end
  log.info "Processed #{count} mails"

  count == 0
end