attachments 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/attachments/extract.rb +32 -9
- data/test/test_attachments.rb +87 -5
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
data/lib/attachments/extract.rb
CHANGED
@@ -30,19 +30,34 @@ module Attachments
|
|
30
30
|
reset
|
31
31
|
end
|
32
32
|
|
33
|
-
def parse
|
34
|
-
|
33
|
+
def parse filename_or_hash
|
34
|
+
hash = case
|
35
|
+
when filename_or_hash.is_a?(String) then
|
36
|
+
{ :filename => filename_or_hash }
|
37
|
+
when filename_or_hash.is_a?(Hash) then
|
38
|
+
filename_or_hash
|
39
|
+
else
|
40
|
+
{}
|
41
|
+
end
|
42
|
+
|
43
|
+
content = case
|
44
|
+
when hash[:filename] then
|
45
|
+
read_file(hash[:filename])
|
46
|
+
when hash[:content] then
|
47
|
+
hash[:content]
|
48
|
+
when hash[:stream] then
|
49
|
+
hash[:stream].read()
|
50
|
+
else
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
|
54
|
+
parse_data content if content
|
35
55
|
end
|
36
56
|
|
37
57
|
def parse_file filename
|
38
58
|
@last_parsed = filename
|
39
|
-
|
40
|
-
|
41
|
-
file = File.new(filename, "rb")
|
42
|
-
raw_mail_data = file.read()
|
43
|
-
file.close()
|
44
|
-
|
45
|
-
parse_data raw_mail_data
|
59
|
+
content = read_file filename
|
60
|
+
parse_data content
|
46
61
|
end
|
47
62
|
|
48
63
|
def parse_data raw_mail_data
|
@@ -112,6 +127,14 @@ module Attachments
|
|
112
127
|
@files = []
|
113
128
|
end
|
114
129
|
|
130
|
+
def read_file filename
|
131
|
+
# Load the email as binary to avoid encoding exceptions
|
132
|
+
file = File.new(filename, "rb")
|
133
|
+
content = file.read()
|
134
|
+
file.close()
|
135
|
+
content
|
136
|
+
end
|
137
|
+
|
115
138
|
def parse_part mail
|
116
139
|
# Filter parts with a type that is not of interest
|
117
140
|
if mail.content_type
|
data/test/test_attachments.rb
CHANGED
@@ -4,13 +4,22 @@ require 'helper'
|
|
4
4
|
require 'iconv'
|
5
5
|
|
6
6
|
class TestAttachments < Test::Unit::TestCase
|
7
|
+
def compare_extractors a, b
|
8
|
+
(0...a.files.length).each do |i|
|
9
|
+
tmp_a = a.files[i][:tmpfile]
|
10
|
+
tmp_b = b.files[i][:tmpfile]
|
11
|
+
|
12
|
+
isIdentical = FileUtils::compare_file(tmp_a, tmp_b)
|
13
|
+
assert(isIdentical)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
7
17
|
context "Parse test cases without crashes" do
|
8
18
|
setup do
|
9
19
|
@extract = Attachments::Extract.new [ "text/plain", "image/jpeg" ]
|
10
20
|
end
|
11
21
|
|
12
22
|
teardown do
|
13
|
-
@extract.close
|
14
23
|
end
|
15
24
|
|
16
25
|
should "parse test mails without raising exceptions" do
|
@@ -18,6 +27,7 @@ class TestAttachments < Test::Unit::TestCase
|
|
18
27
|
assert_nothing_raised do
|
19
28
|
@extract.parse_file filename
|
20
29
|
end
|
30
|
+
@extract.close
|
21
31
|
end
|
22
32
|
end
|
23
33
|
|
@@ -25,10 +35,9 @@ class TestAttachments < Test::Unit::TestCase
|
|
25
35
|
return unless "".respond_to?(:valid_encoding?)
|
26
36
|
|
27
37
|
Dir.glob("./test/data/mail_*.eml") do |filename|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
38
|
+
@extract.parse_file filename
|
39
|
+
assert @extract.text_body.valid_encoding?
|
40
|
+
@extract.close
|
32
41
|
end
|
33
42
|
end
|
34
43
|
end
|
@@ -126,5 +135,78 @@ class TestAttachments < Test::Unit::TestCase
|
|
126
135
|
end
|
127
136
|
end
|
128
137
|
end
|
138
|
+
|
139
|
+
context "Parse parameters" do
|
140
|
+
setup do
|
141
|
+
@a = Attachments::Extract.new [ "text/plain", "image/jpeg" ]
|
142
|
+
@b = Attachments::Extract.new [ "text/plain", "image/jpeg" ]
|
143
|
+
end
|
144
|
+
|
145
|
+
teardown do
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
should "handle straight filename" do
|
150
|
+
Dir.glob("./test/data/mail_*.eml") do |filename|
|
151
|
+
assert_nothing_raised do
|
152
|
+
@a.parse_file filename
|
153
|
+
@b.parse filename
|
154
|
+
|
155
|
+
compare_extractors(@a, @b)
|
156
|
+
end
|
157
|
+
@a.close
|
158
|
+
@b.close
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
should "handle filename in hash" do
|
164
|
+
Dir.glob("./test/data/mail_*.eml") do |filename|
|
165
|
+
assert_nothing_raised do
|
166
|
+
@a.parse_file filename
|
167
|
+
@b.parse({ :filename => filename })
|
168
|
+
|
169
|
+
compare_extractors(@a, @b)
|
170
|
+
end
|
171
|
+
@a.close
|
172
|
+
@b.close
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
should "handle content in hash" do
|
178
|
+
Dir.glob("./test/data/mail_*.eml") do |filename|
|
179
|
+
assert_nothing_raised do
|
180
|
+
@a.parse_file filename
|
181
|
+
|
182
|
+
f = File.new(filename, "rb")
|
183
|
+
content = f.read()
|
184
|
+
f.close
|
185
|
+
@b.parse({ :content => content })
|
186
|
+
|
187
|
+
compare_extractors(@a, @b)
|
188
|
+
end
|
189
|
+
@a.close
|
190
|
+
@b.close
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
|
195
|
+
should "handle stream in hash" do
|
196
|
+
Dir.glob("./test/data/mail_*.eml") do |filename|
|
197
|
+
assert_nothing_raised do
|
198
|
+
@a.parse_file filename
|
199
|
+
|
200
|
+
f = File.new(filename, "rb")
|
201
|
+
@b.parse({ :stream => f })
|
202
|
+
f.close
|
203
|
+
|
204
|
+
compare_extractors(@a, @b)
|
205
|
+
end
|
206
|
+
@a.close
|
207
|
+
@b.close
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
129
211
|
end
|
130
212
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attachments
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Rune Myrland
|