Skip to content

Instantly share code, notes, and snippets.

@octosteve
Forked from bjbatten/gist:6938812
Last active December 25, 2015 08:09
Show Gist options
  • Save octosteve/6944159 to your computer and use it in GitHub Desktop.
Save octosteve/6944159 to your computer and use it in GitHub Desktop.
require 'date'
class Email
attr_reader :email_text
def initialize(email_text)
@email_text = email_text
end
def date
@date ||= parsed_date.strftime('%a %d %b %Y')
end
def time
@time ||= parsed_date.strftime("%T")
end
def body
unless email_text.include? "Content-Type: text/plain"
return "no plain text, only html encoding"
end
match_from_message(/Content-Type: text\/plain;([\s\S]*)/)
end
def subject
@subject ||= fetch("Subject")
end
def to
@to ||= fetch("To")
end
def from
@from ||= fetch("From")
end
def to_s
"to: #{to}\n" +
"from: #{from}\n" +
"date: #{date}\n" +
"time: #{time}\n" +
"subject: #{subject}\n" +
"body: #{body}\n"
end
private
def fetch(field)
pattern = Regexp.new("\n#{field}: (.*)\n")
match_from_message(pattern)
end
def match_from_message(pattern)
email_text.scan(pattern).flatten.first
end
def parsed_date
@parsed_date ||= DateTime.parse fetch("Date")
end
end
Delivered-To: some_guy@example.com
Received: by 10.76.7.237 with SMTP id m13csp60116oaa;
Fri, 11 Oct 2013 14:42:36 -0700 (PDT)
X-Received: by 10.49.41.3 with SMTP id b3mr8278162qel.51.1381527756566;
Fri, 11 Oct 2013 14:42:36 -0700 (PDT)
Return-Path: <noreply@github.com>
Received: from github-smtp2b-cp1-prd.iad.github.net (github-smtp2-ext3.iad.github.net. [192.30.252.194])
by mx.google.com with ESMTPS id u5si10756913qcj.77.1969.12.31.16.00.00
(version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
Fri, 11 Oct 2013 14:42:36 -0700 (PDT)
Received-SPF: pass (google.com: domain of noreply@github.com designates 192.30.252.194 as permitted sender) client-ip=192.30.252.194;
Authentication-Results: mx.google.com;
spf=pass (google.com: domain of noreply@github.com designates 192.30.252.194 as permitted sender) smtp.mail=noreply@github.com
Date: Fri, 11 Oct 2013 13:53:23 -0700
From: GitHub <noreply@github.com>
To: some_guy@example.com
Message-ID: <52586543d483d_bd31229d4417214d@github-worker7-cp1-prd.iad.github.net.mail>
Subject: Your subject here
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Your message Body
require 'minitest/autorun'
require 'minitest/spec'
require_relative 'email'
describe Email do
before do
file = File.read('email.txt')
@email = Email.new(file)
end
describe "#date" do
it "get's the date" do
@email.date.must_equal "Fri 11 Oct 2013"
end
end
describe "#time" do
it "gets the time" do
@email.time.must_equal "13:53:23"
end
end
describe "#body" do
it "returns a message if no plain text available" do
e = Email.new("Foo")
e.body.must_equal "no plain text, only html encoding"
end
it "returns the body if plain text version available" do
message = <<-eos
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Your message Body
eos
@email.body.must_equal message
end
end
describe "#subject" do
it "Returns the subject" do
@email.subject.must_equal "Your subject here"
end
end
describe "#to" do
it "returns the recipient" do
@email.to.must_equal "some_guy@example.com"
end
end
describe "#from" do
it "returns the sender" do
@email.from.must_equal "GitHub <noreply@github.com>"
end
end
describe "#to_s" do
it "returns a formatted string" do
@email.to_s.must_equal <<-eos
to: some_guy@example.com
from: GitHub <noreply@github.com>
date: Fri 11 Oct 2013
time: 13:53:23
subject: Your subject here
body:
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Your message Body
eos
end
end
end
require_relative 'email'
def help
"Please rerun the file again and\n" +
"supply textfile arguments by name only\n" +
"after the file name\n" +
"(program will append the .txt extension)" +
"Example: ruby thisfile.rb email2 email2 email3\n" +
"you can use as many files as you want."
end
abort help if ARGV.empty?
ARGV.each do |file|
file = "#{file}.txt"
unless File.exists?(file)
puts "#{file} does not exist."
puts "Press enter to move to next email..."
$stdin.gets
next
end
puts "Content for #{file}"
email= Email.new(File.read(file))
puts email
puts "Press enter to move to next email..."
$stdin.gets
end
puts "Out of emails"
puts "Thank you for your consideration"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment