Skip to content

Instantly share code, notes, and snippets.

@bjbatten
Created October 11, 2013 17:33
Show Gist options
  • Save bjbatten/6938812 to your computer and use it in GitHub Desktop.
Save bjbatten/6938812 to your computer and use it in GitHub Desktop.
class Email
def initialize(filein)
@date = get_date(filein)
@time = get_time(filein)
@to = get_to(filein)
@from = get_from(filein)
@subject = get_subject(filein)
@body = get_body(filein)
puts ""
end
def date_time_help(filein)
splits = filein.split("Date: ")
second_split = splits[1].split("\n")
return second_split[0]
end
def get_date(filein)
line = date_time_help(filein)
line = line.split
temp = ""
for spot in 0...4
temp+= line[spot] + " "
end
temp+= "\n"
return temp
end
def get_time(filein)
line = date_time_help(filein)
line = line.split
temp = ""
line.each {|x| temp+= x.to_s if /\d{2}:\d{2}:\d{2}/.match(x) }
return temp
end
def get_body(filein)
if filein.include? "Content-Type: text/plain;"
splits = filein.split("Content-Type: text/plain;")
second_split = splits[1].split("Content-Type: ")
return second_split[0]
end
return "no plain text, only html encoding"
end
def get_subject(filein)
splits = filein.split("Subject: ")
second_split = splits[1].split("\n")
return second_split[0]
end
def get_to(filein)
splits = filein.split("Delivered-To: ")
second_split = splits[1].split("To: ")
third_split = second_split[1].split("\n")
temp = third_split[0]
if third_split[1].start_with?("Cc: ")
temp+= "\n" + third_split[1]
end
return temp
end
def get_from(filein)
splits = filein.split("From: ")
second_split = splits[1].split("\n")
return second_split[0].to_s
end
def print
puts "to: " + @to
puts "from: " + @from
puts "date: " + @date
puts "time: " + @time
puts "subject: " + @subject
puts "body: " + @body
end
end
if ARGV.empty?
puts "please rerun the file again and\nsupply textfile arguments by name only\nafter the file name\n(program will append the .txt extension)"
puts "Example: ruby thisfile.rb email1 email2 email3\nyou can use as many files as you want."
else
ARGV.each do |text_file|
text_file += ".txt"
if File.exists?(text_file)
else
puts text_file.to_s + " does not exist."
puts "Press enter to move to next email..."
$stdin.gets
next
end
file = File.open( text_file, "r")
text = file.read
puts "Content for " + text_file.to_s
#create a class
temp_email = Email.new(text)
temp_email.print
file.close()
puts "Press enter to move to next email..."
$stdin.gets
end
puts "Out of emails"
puts "Thank you for your consideration"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment