Created
June 17, 2013 18:08
-
-
Save johnclaus/5798893 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require 'zlib' | |
# Mandatory: Create a single output file containing the date sent, the sender and the subject for each of the messages | |
class EmailParser | |
def initialize(filepath) | |
@filepath = filepath | |
end | |
def flush_to_file ary | |
CSV.open("output.csv", "a") do |csv| | |
csv << [ary[0], ary[1], ary[2]] | |
end | |
end | |
def parse_msg | |
if @filepath.to_s.strip.length == 0 | |
raise ArgumentError, "Missing argument. Include a filepath to process." | |
end | |
Zlib::GzipReader.open(@filepath) do |file| | |
msg_fields = [] | |
dateFlag, fromFlag, subjectFlag = false | |
file.each_line do |line| | |
case line | |
when /Date: / | |
unless dateFlag == true | |
msg_fields[0] = line | |
dateFlag = true | |
end | |
when /From:/ | |
msg_fields[1] = line unless fromFlag == true | |
fromFlag = true | |
when /Subject:/ | |
msg_fields[2] = line unless subjectFlag == true | |
subjectFlag = true | |
end | |
if msg_fields.count == 3 | |
#puts msg_fields.inspect | |
#flush_to_file msg_fields | |
msg_fields.clear | |
dateFlag, fromFlag, subjectFlag = false | |
end | |
end | |
end | |
end | |
end | |
parser = EmailParser.new(ARGV[0]).parse_msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment