bodyparts 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.7
data/bodyparts.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bodyparts}
8
- s.version = "0.1.6"
8
+ s.version = "0.1.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Max Ogden"]
12
- s.date = %q{2010-06-14}
12
+ s.date = %q{2010-06-21}
13
13
  s.description = %q{Separates new messages from included reply chains in the body of emails}
14
14
  s.email = %q{max@maxogden.com}
15
15
  s.extra_rdoc_files = [
data/lib/bodyparts.rb CHANGED
@@ -10,37 +10,54 @@ class BodyParts
10
10
  end
11
11
 
12
12
  def self.extract_tmail_attributes(tmail_object)
13
- message_id = tmail_object.message_id
14
- x_mailer = tmail_object['x-mailer'].to_s
13
+ if mail_encoding = tmail_object['content_transfer_encoding']
14
+ content_encoding = mail_encoding.to_s
15
+ else
16
+ content_encoding = "not known"
17
+ end
18
+
15
19
  body = if tmail_object.multipart?
16
20
  tmail_object.parts.first.body
17
21
  else
18
22
  tmail_object.body
19
23
  end
20
- {:message_id => message_id, :x_mailer => x_mailer, :body => body}
24
+
25
+ {:content_encoding => content_encoding, :body => body}
21
26
  end
22
27
 
23
28
  def self.extract_mail_attributes(mail_object)
24
- message_id = mail_object['message_id']
25
- x_mailer = mail_object['x-mailer']
29
+ if mail_encoding = mail_object['content_transfer_encoding']
30
+ content_encoding = mail_encoding.encoding
31
+ else
32
+ content_encoding = "not known"
33
+ end
34
+
26
35
  if mail_object.find_first_mime_type('text/plain')
27
36
  body = mail_object.text_part.body.raw_source
28
37
  else
29
38
  body = mail_object.body.raw_source
30
39
  end
31
- {:message_id => message_id, :x_mailer => x_mailer, :body => body}
40
+
41
+ {:content_encoding => content_encoding, :body => body}
32
42
  end
33
43
 
34
44
  def self.find_reply_in(email)
35
45
  email = Mail::Message.new(email) if email.is_a? String
46
+
36
47
  mail_attributes = case email.class.to_s
37
48
  when "TMail::Mail" then extract_tmail_attributes(email)
38
49
  when "Mail::Message" then extract_mail_attributes(email)
39
50
  else raise "You must pass in either a TMail or Mail object or raw email source text"
40
51
  end
52
+
41
53
  body = mail_attributes[:body]
54
+
55
+ if mail_attributes[:content_encoding] == 'base64'
56
+ body = Base64.decode64 body
57
+ end
42
58
 
43
59
  matches = rules.map {|rule| body.match(rule[:reply_delimiter])}.compact!
60
+
44
61
  unless matches.empty?
45
62
  match = matches.sort_by {|m| m.begin(0)}.first
46
63
  new_message = body[0, match.begin(0)]
@@ -1,7 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "BodyParts" do
4
-
5
4
  [TMail::Mail, Mail].each do |mail_object|
6
5
  it "should accept and parse #{mail_object} objects as input" do
7
6
  generic = FakeMessage.fake_emails[:generic]
@@ -9,7 +8,7 @@ describe "BodyParts" do
9
8
  BodyParts.find_reply_in(message)[:new_message].should == generic[:reply_text]
10
9
  end
11
10
  end
12
-
11
+
13
12
  %w(gmail yahoo hotmail aol_webmail generic).each do |mail_server|
14
13
  it "should strip out the replies from a #{mail_server} message containing forwarded junk" do
15
14
  mail_server = FakeMessage.fake_emails[mail_server.to_sym]
@@ -41,4 +40,12 @@ describe "BodyParts" do
41
40
  message = FakeMessage.load_mail(with_attachment[:filename])
42
41
  BodyParts.find_reply_in(message)[:new_message].should == with_attachment[:reply_text]
43
42
  end
43
+
44
+ [TMail::Mail, Mail].each do |mail_type|
45
+ it "should correctly parse base64 encoded #{mail_type} messages" do
46
+ base64_encoded = FakeMessage.fake_emails[:base64_encoded]
47
+ message = FakeMessage.new_mail(mail_type, base64_encoded[:headers])
48
+ BodyParts.find_reply_in(message)[:new_message].should == base64_encoded[:reply_text]
49
+ end
50
+ end
44
51
  end
data/spec/emails.yml CHANGED
@@ -139,4 +139,12 @@
139
139
  :filename:
140
140
  "email_with_attachment"
141
141
  :reply_text:
142
- HELLO
142
+ HELLO
143
+ :base64_encoded:
144
+ :reply_text:
145
+ INSTAIN REPLY. DO WAY
146
+ :headers:
147
+ content_transfer_encoding:
148
+ base64
149
+ body:
150
+ SU5TVEFJTiBSRVBMWS4gRE8gV0FZ\n
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'spec'
2
2
  require File.expand_path(File.dirname(__FILE__) + '/../lib/bodyparts.rb')
3
3
 
4
-
5
4
  class FakeMessage
6
5
  def self.fake_emails
7
6
  YAML.load_file(File.expand_path(File.dirname(__FILE__) + '/emails.yml'))
@@ -26,7 +25,7 @@ class FakeMessage
26
25
  mail = mail_class.new do
27
26
  if mail_class == Mail
28
27
  text_part do
29
- body headers.delete("body")
28
+ body headers.delete "body"
30
29
  end
31
30
  end
32
31
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 6
9
- version: 0.1.6
8
+ - 7
9
+ version: 0.1.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Max Ogden
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-14 00:00:00 -07:00
17
+ date: 2010-06-21 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency