Skip to content

Instantly share code, notes, and snippets.

@akcrono
Last active August 29, 2015 14:16
Show Gist options
  • Save akcrono/376904e565fd873e7b78 to your computer and use it in GitHub Desktop.
Save akcrono/376904e565fd873e7b78 to your computer and use it in GitHub Desktop.
class CustomMailExporter
require 'fileutils'
attr_accessor :service, :target_users, :target_start_date, :target_end_date,
:path, :errors, :filename_counter, :emails_found
# Dates should be in Date or DateTime format. Users should be an array.
def initialize(service, target_users, target_start_date, target_end_date)
@service = service
@target_users = target_users.map(&:downcase)
@target_start_date = target_start_date
@target_end_date = target_end_date
@path = "/mnt/#{service.id}/"
@errors = []
@filename_counter = 0
@emails_found = 0
end
def process
FileUtils.mkdir_p(path) unless File.directory?(path)
service.metadatum_class.find_each(service.id) do |datum|
if target_user?(datum.from) && date_in_range?(datum.date)
write_contents_to_file(datum)
@emails_found += 1
end
end
return true if errors.count == 0
end
def convert_to_email_address(from)
from.split("<").last.split(">").first
end
def target_user?(from)
target_users.include?(convert_to_email_address(from).downcase)
end
def date_in_range?(date)
date > target_start_date && date < target_end_date
end
def write_contents_to_file(datum)
begin
path_and_name = path
if datum.respond_to?(:content_filename) && datum.content_filename.present?
path_and_name += datum.content_filename.gsub(/[.<>:"\/\\|\?\*']/, "")
#use gsub for problem characters in subjects
else
path_and_name += filename_counter.to_s
filename_counter += 1
end
path_and_name += ".eml" unless path_and_name.include? ".eml"
File.open(path_and_name, 'wb') do |f|
datum.content { |chunk| f << chunk }
end
rescue => e
errors << [datum.key, e]
end
end
end
Copy link

ghost commented Feb 26, 2015

Overall, I think it's good. Nice job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment