class MailHandler::Receiving::FolderChecker

folder checking base class

Constants

FILE_SEARCH_CLASSES

filter options which need to be done by searching files

Attributes

archive_folder[RW]

folders in which emails will be searched for and managed

inbox_folder[RW]

folders in which emails will be searched for and managed

Public Class Methods

new(inbox_folder = nil, archive_folder = nil) click to toggle source
Calls superclass method MailHandler::Receiving::Checker::new
# File lib/mailhandler/receiving/folder.rb, line 20
def initialize(inbox_folder = nil, archive_folder = nil)
  super()

  @inbox_folder = inbox_folder
  @archive_folder = archive_folder
end

Public Instance Methods

find(options) click to toggle source

check whether email is received by checking for an email in folder

# File lib/mailhandler/receiving/folder.rb, line 28
def find(options)
  verify_and_set_search_options(options)
  verify_mailbox_folders
  email_files = find_files(search_options)

  unless email_files.empty?
    @found_emails = parse_email_from_files(email_files, search_options[:count])
    move_files(email_files) if search_options[:archive]
  end

  search_result
end
start() click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 41
def start
  verify_mailbox_folders
end

Private Instance Methods

archive_file(file) click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 99
def archive_file(file)
  access_file(file) do
    FileUtils.mv("#{inbox_folder}/#{File.basename(file)}",
                 "#{archive_folder}/#{File.basename(file)}")
  end
end
delete_file(file) click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 106
def delete_file(file)
  access_file(file) { FileUtils.rm_r "#{inbox_folder}/#{File.basename(file)}", force: false }
end
filter_files(files, options) click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 69
def filter_files(files, options)
  options.each do |key, value|
    next if FILE_SEARCH_CLASSES[key].nil?

    filter = FILE_SEARCH_CLASSES[key].new(files, value)
    filter.fast_check = options[:fast_check] unless options[:fast_check].nil?
    files = filter.get
  end

  files
end
find_files(options) click to toggle source

find files by FILE_SEARCH_CLASSES options this will ignore filter criteria options which can't be done on files directly

# File lib/mailhandler/receiving/folder.rb, line 63
def find_files(options)
  file_list = FileList.new
  files = filter_files(file_list.get(search_pattern), options)
  file_list.sort(files)
end
move_files(files) click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 81
def move_files(files)
  files.each { |file| inbox_folder == archive_folder ? delete_file(file) : archive_file(file) }
end
parse_email_from_files(files, count) click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 85
def parse_email_from_files(files, count)
  read_files(files, count).map { |email_string| Mail.read_from_string(email_string) }
end
read_files(files, count) click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 89
def read_files(files, count)
  file_contents = []
  files.first(count).each do |file|
    file_content = access_file(file, nil) { File.read(file) }
    file_contents << file_content unless file_content.nil?
  end

  file_contents
end
search_pattern() click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 57
def search_pattern
  @inbox_folder + '/*.*'
end
verify_mailbox_folders() click to toggle source
# File lib/mailhandler/receiving/folder.rb, line 110
def verify_mailbox_folders
  raise MailHandler::Error, 'Folder variables are not set.' if inbox_folder.nil? || archive_folder.nil?
  raise MailHandler::FileError, 'Mailbox folders do not exist.' unless File.directory?(inbox_folder) &&
                                                                       File.directory?(archive_folder)
end