Skip to content

Commit 9b661ea

Browse files
committed
Merge
2 parents 654d651 + 571d1ac commit 9b661ea

8 files changed

Lines changed: 112 additions & 2 deletions

lib/email_reply_parser.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def self.read(text)
4141
Email.new.read(text)
4242
end
4343

44+
def self.parse_reply(text)
45+
self.read(text).visible_text
46+
end
47+
4448
### Emails
4549

4650
# An Email instance represents a parsed body String.
@@ -52,6 +56,11 @@ def initialize
5256
@fragments = []
5357
end
5458

59+
60+
def visible_text
61+
fragments.select{|f| !f.hidden?}.map{|f| f.to_s}.join("\n").rstrip
62+
end
63+
5564
# Splits the given text into a list of Fragments. This is roughly done by
5665
# reversing the text and parsing from the bottom to the top. This way we
5766
# can check for 'On <date>, <author> wrote:' lines above quoted blocks.
@@ -105,8 +114,8 @@ def read(text)
105114

106115
private
107116
EMPTY = "".freeze
108-
SIG_REGEX = /(\s--|__|\w-)$/
109-
117+
SIG_REGEX = /(--|__|\w-$)|(^(\w+\s*){1,3} #{"Sent from my".reverse}$)/
118+
110119
### Line-by-Line Parsing
111120

112121
# Scans the given line of text and figures out which fragment it belongs
@@ -240,3 +249,4 @@ def inspect
240249
end
241250
end
242251
end
252+

test/email_reply_parser_test.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,47 @@ def test_does_not_modify_input_string
101101
assert_equal "The Quick Brown Fox Jumps Over The Lazy Dog", original
102102
end
103103

104+
def test_returns_only_the_visible_fragments_as_a_string
105+
reply = email(:email_2_1)
106+
assert_equal reply.fragments.select{|r| !r.hidden?}.map{|r| r.to_s}.join("\n").rstrip, reply.visible_text
107+
end
108+
109+
def test_parse_out_just_top_for_outlook_reply
110+
body = IO.read EMAIL_FIXTURE_PATH.join("email_2_1.txt").to_s
111+
assert_equal "Outlook with a reply", EmailReplyParser.parse_reply(body)
112+
end
113+
114+
def test_parse_out_sent_from_iPhone
115+
body = IO.read EMAIL_FIXTURE_PATH.join("email_iPhone.txt").to_s
116+
assert_equal "Here is another email", EmailReplyParser.parse_reply(body)
117+
end
118+
119+
def test_parse_out_sent_from_BlackBerry
120+
body = IO.read EMAIL_FIXTURE_PATH.join("email_BlackBerry.txt").to_s
121+
assert_equal "Here is another email", EmailReplyParser.parse_reply(body)
122+
end
123+
124+
def test_parse_out_send_from_multiword_mobile_device
125+
body = IO.read EMAIL_FIXTURE_PATH.join("email_multi_word_sent_from_my_mobile_device.txt").to_s
126+
assert_equal "Here is another email", EmailReplyParser.parse_reply(body)
127+
end
128+
129+
def test_do_not_parse_out_send_from_in_regular_sentence
130+
body = IO.read EMAIL_FIXTURE_PATH.join("email_sent_from_my_not_signature.txt").to_s
131+
assert_equal "Here is another email\n\nSent from my desk, is much easier then my mobile phone.", EmailReplyParser.parse_reply(body)
132+
end
133+
134+
def test_retains_bullets
135+
body = IO.read EMAIL_FIXTURE_PATH.join("email_bullets.txt").to_s
136+
assert_equal "test 2 this should list second\n\nand have spaces\n\nand retain this formatting\n\n\n - how about bullets\n - and another",
137+
EmailReplyParser.parse_reply(body)
138+
end
139+
140+
def test_parse_reply
141+
body = IO.read EMAIL_FIXTURE_PATH.join("email_1_2.txt").to_s
142+
assert_equal EmailReplyParser.read(body).visible_text, EmailReplyParser.parse_reply(body)
143+
end
144+
104145
def email(name)
105146
body = IO.read EMAIL_FIXTURE_PATH.join("#{name}.txt").to_s
106147
EmailReplyParser.read body

test/emails/email_2_1.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Outlook with a reply
2+
3+
4+
------------------------------
5+
6+
*From:* Google Apps Sync Team [mailto:mail-noreply@google.com]
7+
*Sent:* Thursday, February 09, 2012 1:36 PM
8+
*To:* jow@xxxx.com
9+
*Subject:* Google Apps Sync was updated!
10+
11+
12+
13+
Dear Google Apps Sync user,
14+
15+
Google Apps Sync for Microsoft Outlook® was recently updated. Your computer
16+
now has the latest version (version 2.5). This release includes bug fixes
17+
to improve product reliability. For more information about these and other
18+
changes, please see the help article here:
19+
20+
http://www.google.com/support/a/bin/answer.py?answer=153463
21+
22+
Sincerely,
23+
24+
The Google Apps Sync Team.
25+

test/emails/email_BlackBerry.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Here is another email
2+
3+
Sent from my BlackBerry

test/emails/email_bullets.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
test 2 this should list second
2+
3+
and have spaces
4+
5+
and retain this formatting
6+
7+
8+
- how about bullets
9+
- and another
10+
11+
12+
On Fri, Feb 24, 2012 at 10:19 AM, <examples@email.goalengine.com> wrote:
13+
14+
> Give us an example of how you applied what they learned to achieve
15+
> something in your organization
16+
17+
18+
19+
20+
--
21+
22+
*Joe Smith | Director, Product Management*

test/emails/email_iPhone.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Here is another email
2+
3+
Sent from my iPhone
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Here is another email
2+
3+
Sent from my Verizon Wireless BlackBerry
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Here is another email
2+
3+
Sent from my desk, is much easier then my mobile phone.

0 commit comments

Comments
 (0)