Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 4 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
View
50 lib/mail_extract/parser.rb
@@ -1,9 +1,11 @@
require 'strscan'
+# require 'pry'
+# require 'pry-debugger'
module MailExtract
class Parser
- attr_reader :body
-
+ attr_reader :body, :results
+
# Initialize a new MailExtract::Parser object
#
# text - Email message body
@@ -19,42 +21,61 @@ def initialize(text, options={})
@last_type = :text
@type = :text
@options = options
-
+ @results = { text: [], quote: [], signature: [] }
+
parse
end
-
+
+ def text
+ @results[:text]
+ end
+ alias :text :body
+
+ def quote
+ @results[:quote]
+ end
+
+ def signature
+ @results[:signature]
+ end
+
private
-
+
# Process email message body
#
def parse
break_after_quote = @options[:only_head] || false
scanner = StringScanner.new(@text)
-
+
# Process until message end
while str = scanner.scan_until(/\n/)
line = parse_line(str)
-
+
if break_after_quote
break if line.quote? && line.subtype == :start
end
end
-
+
# Process the rest (if any)
- if !break_after_quote && @last_type != :quote
+ if !break_after_quote
if (last_line = scanner.rest.to_s).size > 0
parse_line(last_line)
end
end
-
- @body = @lines.join("\n").strip
+
+ @results = @results.inject({}) do |hash, (key, value)|
+ hash[key] = value.join("\n").strip
+ hash
+ end
+
+ @body = @results[:text]
end
-
+
# Process a single line
#
def parse_line(str)
line = MailExtract::Line.new(str)
-
+
if line.quote?
if @last_type == :text ; @type = :quote ; end
elsif line.text?
@@ -66,7 +87,8 @@ def parse_line(str)
end
@last_type = line.type
@lines << line.body.strip if @type == :text
-
+ @results[@type] << line.body.strip
+
line
end
end
View
6 spec/fixtures/quote_reply_with_quotes.txt
@@ -0,0 +1,6 @@
+On Wed, Aug 10, 2011 at 2:17 PM, Dan S. via CoolApplication <
+noreply@cool.app.com> wrote:
+> **
+> Status update on project
+>
+> Blah blah blah
View
14 spec/fixtures/quote_simple_with_quotes.txt
@@ -0,0 +1,14 @@
+On Tue, 2011-03-01 at 18:02 +0530, Abhishek Kona wrote:
+> Hi folks
+>
+> What is the best way to clear a Riak bucket of all key, values after
+> running a test?
+> I am currently using the Java HTTP API.
+>
+> -Abhishek Kona
+>
+>
+> _______________________________________________
+> riak-users mailing list
+> riak-users@lists.basho.com
+> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
View
2 spec/fixtures/quote_text_after_quote.txt
@@ -0,0 +1,2 @@
+On 02/21/2013 04:28 PM, Christian Ikas wrote:
+> Test1
View
1 spec/fixtures/result_text_after_quote.txt
@@ -0,0 +1 @@
+Test7
View
3 spec/fixtures/text_after_quote.txt
@@ -0,0 +1,3 @@
+On 02/21/2013 04:28 PM, Christian Ikas wrote:
+> Test1
+Test7
View
35 spec/parser_spec.rb
@@ -2,27 +2,36 @@
describe 'MailExtract::Parser' do
it 'parses an email' do
- body = parse_fixture('simple.txt')
- body.should == result_fixture('simple.txt')
+ parser = parser_for_fixture('simple.txt')
+ parser.body.should == result_fixture('simple.txt')
+ parser.quote.should == ''
end
-
+
+ it 'parses an email with text after the quote' do
+ parser = parser_for_fixture('text_after_quote.txt')
+ parser.body.should == result_fixture('text_after_quote.txt')
+ parser.quote.should == quote_fixture('text_after_quote.txt')
+ end
+
it 'parses an email with quotes' do
- body = parse_fixture('simple_with_quotes.txt')
- body.should == result_fixture('simple_with_quotes.txt')
+ parser = parser_for_fixture('simple_with_quotes.txt')
+ parser.body.should == result_fixture('simple_with_quotes.txt')
+ parser.quote.should == quote_fixture('simple_with_quotes.txt')
end
-
+
it 'parses a reply email with broken authored line' do
- body = parse_fixture('reply_with_quotes.txt')
- body.should == 'This is a first line of the message'
+ parser = parser_for_fixture('reply_with_quotes.txt')
+ parser.body.should == 'This is a first line of the message'
+ parser.quote.should == quote_fixture('reply_with_quotes.txt')
end
-
+
it 'parses a message send via iphone' do
- body = parse_fixture('iphone.txt')
- body.should == 'This is a shit i sent from my iphone'
+ parser = parser_for_fixture('iphone.txt')
+ parser.body.should == 'This is a shit i sent from my iphone'
end
it 'parses a reply sent via iphone' do
- body = MailExtract.new(fixture('iphone_with_quotes.txt'), :only_head => true).body
- body.should == 'Primary reply content'
+ parser = MailExtract.new(fixture('iphone_with_quotes.txt'), :only_head => true)
+ parser.body.should == 'Primary reply content'
end
end
View
10 spec/spec_helper.rb
@@ -16,6 +16,14 @@ def result_fixture(file)
fixture("result_#{file}")
end
+def quote_fixture(file)
+ fixture("quote_#{file}")
+end
+
def parse_fixture(file)
- MailExtract.new(fixture(file)).body
+ parser_for_fixture(file).body
+end
+
+def parser_for_fixture(file)
+ MailExtract.new(fixture(file))
end

No commit comments for this range

Something went wrong with that request. Please try again.