mailhandler 1.0.6 → 1.0.7
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.
- checksums.yaml +4 -4
- data/lib/mailhandler/receiving/base.rb +37 -4
- data/lib/mailhandler/receiving/imap.rb +29 -14
- data/lib/mailhandler/version.rb +1 -1
- data/spec/data/email1.txt +11 -0
- data/spec/data/email2.txt +11 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/unit/mailhandler/receiving/base_spec.rb +23 -0
- data/spec/unit/mailhandler/receiving/folder_spec.rb +169 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56df386f2849aba7d646aee2587fd5fa9231ad6b
|
4
|
+
data.tar.gz: 9d70e0d18c929d3901dc1fa7dcca275eb61c75bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0b4fe86a6f9c41e5dd709883871c2cd3e542153325e8a767786e44528facc2ff34a4c1cc95ea9788dd429129ae4964c91b9a82e151bed1dc28a23190bd026f5
|
7
|
+
data.tar.gz: 05ec38a2fbc09c23e00b8e7587c6b3737393a7e58dac6aca10558db71275f87060099bac88edfa434b79343ac792c76ef348d3cfd82efc781dc61f476c6b4d3c
|
@@ -22,11 +22,16 @@ module MailHandler
|
|
22
22
|
|
23
23
|
]
|
24
24
|
|
25
|
+
DEFAULT_SEARCH_OPTIONS = [
|
26
|
+
|
27
|
+
:count, :archive
|
28
|
+
]
|
29
|
+
|
25
30
|
def initialize
|
26
31
|
|
27
32
|
# Default number of email results to return, and whether to archive emails.
|
28
33
|
@search_options = {:count => 50, :archive => false}
|
29
|
-
|
34
|
+
reset_found_emails
|
30
35
|
|
31
36
|
end
|
32
37
|
|
@@ -36,19 +41,47 @@ module MailHandler
|
|
36
41
|
|
37
42
|
end
|
38
43
|
|
44
|
+
def search_result
|
45
|
+
|
46
|
+
!found_emails.empty?
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
39
52
|
def verify_and_set_search_options(options)
|
40
53
|
|
54
|
+
validate_used_options(options)
|
55
|
+
validate_mandatory_options(options)
|
56
|
+
|
57
|
+
@search_options = search_options.merge options
|
58
|
+
reset_found_emails
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
def validate_used_options(options)
|
63
|
+
|
41
64
|
unless (options.keys - AVAILABLE_SEARCH_OPTIONS).empty?
|
42
65
|
raise StandardError, "#{(options.keys - AVAILABLE_SEARCH_OPTIONS)} - Incorrect search option values, options are #{AVAILABLE_SEARCH_OPTIONS}"
|
43
66
|
end
|
44
67
|
|
45
|
-
|
68
|
+
end
|
69
|
+
|
70
|
+
def validate_mandatory_options(options)
|
71
|
+
|
72
|
+
search = AVAILABLE_SEARCH_OPTIONS - DEFAULT_SEARCH_OPTIONS
|
73
|
+
|
74
|
+
unless search.any? { |e| options[e] != nil }
|
75
|
+
|
76
|
+
raise StandardError, "At least one of the following search options has to be used: #{search}"
|
77
|
+
|
78
|
+
end
|
46
79
|
|
47
80
|
end
|
48
81
|
|
49
|
-
def
|
82
|
+
def reset_found_emails
|
50
83
|
|
51
|
-
|
84
|
+
@found_emails = []
|
52
85
|
|
53
86
|
end
|
54
87
|
|
@@ -19,7 +19,8 @@ module MailHandler
|
|
19
19
|
|
20
20
|
:by_subject,
|
21
21
|
:count,
|
22
|
-
:archive
|
22
|
+
:archive,
|
23
|
+
:by_recipient
|
23
24
|
|
24
25
|
]
|
25
26
|
|
@@ -34,17 +35,7 @@ module MailHandler
|
|
34
35
|
verify_and_set_search_options(options)
|
35
36
|
validate_options(options)
|
36
37
|
init_retriever
|
37
|
-
|
38
|
-
|
39
|
-
unless emails.empty?
|
40
|
-
|
41
|
-
emails.each do |email|
|
42
|
-
|
43
|
-
@found_emails << email if email.subject.include? search_options[:by_subject]
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
38
|
+
@found_emails = find_emails(search_options)
|
48
39
|
|
49
40
|
search_result
|
50
41
|
|
@@ -94,14 +85,38 @@ module MailHandler
|
|
94
85
|
|
95
86
|
if options[:archive]
|
96
87
|
|
97
|
-
Mail.find_and_delete(:what => :last, :count => search_options[:count], :order => :desc, :keys =>
|
88
|
+
Mail.find_and_delete(:what => :last, :count => search_options[:count], :order => :desc, :keys => imap_filter_keys(options))
|
98
89
|
|
99
90
|
else
|
100
91
|
|
101
|
-
Mail.find(:what => :last, :count => search_options[:count], :order => :desc, :keys =>
|
92
|
+
Mail.find(:what => :last, :count => search_options[:count], :order => :desc, :keys => imap_filter_keys(options))
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
def imap_filter_keys(options)
|
99
|
+
|
100
|
+
keys = []
|
101
|
+
|
102
|
+
options.keys.each do |filter_option|
|
103
|
+
|
104
|
+
case filter_option
|
105
|
+
|
106
|
+
when :by_recipient
|
107
|
+
|
108
|
+
keys << options[:by_recipient].keys.first.to_s.upcase << options[:by_recipient].values.first
|
109
|
+
|
110
|
+
when :by_subject
|
111
|
+
|
112
|
+
keys << 'SUBJECT' << options[:by_subject]
|
113
|
+
|
114
|
+
end
|
102
115
|
|
103
116
|
end
|
104
117
|
|
118
|
+
keys
|
119
|
+
|
105
120
|
end
|
106
121
|
|
107
122
|
def validate_options(options)
|
data/lib/mailhandler/version.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
Date: Tue, 13 Oct 2015 14:12:52 +0200
|
2
|
+
From: igor@example.com
|
3
|
+
To: igor1@example.com
|
4
|
+
Message-ID: <561cf544ef064_c1863ffde50601f859577@Igors-MacBook-Pro.local.mail>
|
5
|
+
Subject: test email 1878271
|
6
|
+
Mime-Version: 1.0
|
7
|
+
Content-Type: text/plain;
|
8
|
+
charset=UTF-8
|
9
|
+
Content-Transfer-Encoding: 7bit
|
10
|
+
|
11
|
+
test body 1878271
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Date: Tue, 13 Oct 2015 14:12:52 +0200
|
2
|
+
From: igor@example.com
|
3
|
+
To: igor2@example.com
|
4
|
+
Message-ID: <561cf544ef064_c1863ffde50601f859577@Igors-MacBook-Pro.local.mail>
|
5
|
+
Subject: test email 1878275
|
6
|
+
Mime-Version: 1.0
|
7
|
+
Content-Type: text/plain;
|
8
|
+
charset=UTF-8
|
9
|
+
Content-Transfer-Encoding: 7bit
|
10
|
+
|
11
|
+
test body 1878275
|
data/spec/spec_helper.rb
CHANGED
@@ -7,6 +7,12 @@ require 'mailhandler'
|
|
7
7
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
8
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
9
|
|
10
|
+
def data_folder
|
11
|
+
|
12
|
+
File.join(File.dirname(__FILE__), '/', 'data')
|
13
|
+
|
14
|
+
end
|
15
|
+
|
10
16
|
RSpec.configure do |c|
|
11
17
|
|
12
18
|
# Use the specified formatter, options are: # :documentation, :progress, :html, :textmate
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MailHandler::Receiving::Checker do
|
4
|
+
|
5
|
+
subject { MailHandler::Receiving::Checker.new }
|
6
|
+
|
7
|
+
it '.create' do
|
8
|
+
|
9
|
+
is_expected.to be_kind_of MailHandler::Receiving::Checker
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'init details' do
|
14
|
+
|
15
|
+
aggregate_failures "receiving details" do
|
16
|
+
expect(subject.search_options).to eq({:count => 50, :archive => false})
|
17
|
+
expect(subject.found_emails).to eq []
|
18
|
+
expect(subject.search_result).to be false
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MailHandler::Receiving::FolderChecker do
|
4
|
+
|
5
|
+
subject { MailHandler::Receiving::FolderChecker.new }
|
6
|
+
|
7
|
+
it '.create' do
|
8
|
+
|
9
|
+
is_expected.to be_kind_of MailHandler::Receiving::Checker
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'search emails' do
|
14
|
+
|
15
|
+
let(:checker) { MailHandler::Receiving::FolderChecker.new(data_folder, data_folder) }
|
16
|
+
|
17
|
+
context '.find email' do
|
18
|
+
|
19
|
+
context 'search options' do
|
20
|
+
|
21
|
+
it 'default' do
|
22
|
+
|
23
|
+
checker.find({:by_subject => 'test'})
|
24
|
+
expect(checker.search_options).to eq({:count=>50, :archive=>false, :by_subject=>'test'})
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'not found' do
|
31
|
+
|
32
|
+
it 'result' do
|
33
|
+
|
34
|
+
expect(checker.find({:by_subject => 'test123'})).to be false
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'by subject' do
|
39
|
+
|
40
|
+
checker.find({:by_subject => 'test123'})
|
41
|
+
expect(checker.found_emails).to be_empty
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'by date' do
|
46
|
+
|
47
|
+
checker.find({:by_date => Time.now + 86400})
|
48
|
+
expect(checker.found_emails).to be_empty
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'by recipient' do
|
53
|
+
|
54
|
+
checker.find({:by_recipient => { to: 'igor@example.com'} })
|
55
|
+
expect(checker.found_emails).to be_empty
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'found' do
|
62
|
+
|
63
|
+
let(:mail1) { Mail.read_from_string(File.read "#{data_folder}/email1.txt")}
|
64
|
+
let(:mail2) { Mail.read_from_string(File.read "#{data_folder}/email2.txt")}
|
65
|
+
|
66
|
+
it 'result' do
|
67
|
+
|
68
|
+
expect(checker.find({:by_subject => mail1.subject})).to be true
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'by subject - single' do
|
73
|
+
|
74
|
+
checker.find({:by_subject => mail1.subject})
|
75
|
+
|
76
|
+
aggregate_failures "found mail details" do
|
77
|
+
expect(checker.found_emails.size).to be 1
|
78
|
+
expect(checker.found_emails.first).to eq mail1
|
79
|
+
expect(checker.found_emails).not_to include mail2
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'by subject - multiple' do
|
85
|
+
|
86
|
+
checker.find({:by_subject => 'test'})
|
87
|
+
|
88
|
+
aggregate_failures "found mail details" do
|
89
|
+
expect(checker.found_emails.size).to be 2
|
90
|
+
expect(checker.found_emails).to include mail1
|
91
|
+
expect(checker.found_emails).to include mail2
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'by count' do
|
97
|
+
|
98
|
+
checker.find({:by_subject => 'test', :count => 1})
|
99
|
+
expect(checker.found_emails.size).to be 1
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'by date' do
|
104
|
+
|
105
|
+
checker.find({:by_date => Time.new(2015,10,12,13,30,0, "+02:00")})
|
106
|
+
expect(checker.found_emails.size).to be 2
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'by recipient' do
|
111
|
+
|
112
|
+
checker.find({:by_recipient => { to: 'igor1@example.com'} })
|
113
|
+
expect(checker.found_emails.size).to be 1
|
114
|
+
|
115
|
+
checker.find({:by_recipient => { to: 'igor2@example.com'} })
|
116
|
+
expect(checker.found_emails.size).to be 1
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'archiving emails' do
|
121
|
+
|
122
|
+
after(:each) {
|
123
|
+
|
124
|
+
FileUtils.rm_r "#{data_folder}/checked", :force => true
|
125
|
+
|
126
|
+
}
|
127
|
+
|
128
|
+
let(:mail) {
|
129
|
+
|
130
|
+
mail = Mail.read_from_string(File.read "#{data_folder}/email2.txt")
|
131
|
+
mail.subject = 'test 872878278'
|
132
|
+
File.open("#{data_folder}/email3.txt", "w") { |file| file.write(mail) }
|
133
|
+
mail
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
let(:checker_archive) { MailHandler::Receiving::FolderChecker.new(data_folder, "#{data_folder}/checked") }
|
138
|
+
|
139
|
+
it 'to same folder - delete' do
|
140
|
+
|
141
|
+
checker.find({:by_subject => mail.subject, :archive => true})
|
142
|
+
expect(checker.found_emails.size).to be 1
|
143
|
+
|
144
|
+
checker.find({:by_subject => mail.subject, :archive => true})
|
145
|
+
expect(checker.found_emails).to be_empty
|
146
|
+
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'to separate folder' do
|
151
|
+
|
152
|
+
checker_archive.find({:by_subject => mail.subject, :archive => true})
|
153
|
+
expect(checker_archive.found_emails.size).to be 1
|
154
|
+
|
155
|
+
checker_archive.find({:by_subject => mail.subject, :archive => true})
|
156
|
+
expect(checker_archive.found_emails).to be_empty
|
157
|
+
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mailhandler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Balos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mail
|
@@ -75,8 +75,12 @@ files:
|
|
75
75
|
- lib/mailhandler/version.rb
|
76
76
|
- mailhandler.gemspec
|
77
77
|
- readme.md
|
78
|
+
- spec/data/email1.txt
|
79
|
+
- spec/data/email2.txt
|
78
80
|
- spec/spec_helper.rb
|
79
81
|
- spec/unit/mailhandler/receiver_spec.rb
|
82
|
+
- spec/unit/mailhandler/receiving/base_spec.rb
|
83
|
+
- spec/unit/mailhandler/receiving/folder_spec.rb
|
80
84
|
- spec/unit/mailhandler/receiving/notification/console_spec.rb
|
81
85
|
- spec/unit/mailhandler/receiving/notification/email/content_spec.rb
|
82
86
|
- spec/unit/mailhandler/receiving/notification/email_spec.rb
|