mailhandler 1.0.9 → 1.0.10
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.rb +6 -1
- data/lib/mailhandler/errors.rb +14 -0
- data/lib/mailhandler/receiver.rb +4 -4
- data/lib/mailhandler/receiving/base.rb +43 -16
- data/lib/mailhandler/receiving/filter.rb +1 -1
- data/lib/mailhandler/receiving/folder.rb +2 -1
- data/lib/mailhandler/receiving/imap.rb +10 -3
- data/lib/mailhandler/receiving/notification/email.rb +2 -1
- data/lib/mailhandler/receiving/notification/email/states.rb +2 -1
- data/lib/mailhandler/sending/api_batch.rb +2 -1
- data/lib/mailhandler/sending/base.rb +11 -1
- data/lib/mailhandler/version.rb +1 -1
- data/mailhandler.gemspec +1 -1
- data/readme.md +5 -0
- data/spec/unit/mailhandler/receiver_spec.rb +3 -3
- data/spec/unit/mailhandler/receiving/base_spec.rb +6 -0
- data/spec/unit/mailhandler/receiving/folder_spec.rb +81 -1
- data/spec/unit/mailhandler/receiving/notification/email_spec.rb +0 -1
- data/spec/unit/mailhandler/sending/sender_api_batch_spec.rb +2 -2
- data/spec/unit/mailhandler_spec.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a290f9049b44faf3abc87592583dfd76063c5442
|
4
|
+
data.tar.gz: 61bdcf1d9d8c1355463d5e7bc0678158e7087864
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9c6e5a41a9f4dbb9ec1631be903839104f52fd10a1aec1d182dd9a320131481b29b79ad22c5368d40b7e422b77699fd3a567e2c59f7002f0b48683234020007
|
7
|
+
data.tar.gz: 6409f13a167cef7deaf8ebb9aac4be757626f5a39d148b77667f26e3da5025852d0f84a7d49078203d7c19e969a62aae525b7b1b810eb449b4ccc5075e23c56f
|
data/lib/mailhandler.rb
CHANGED
@@ -3,6 +3,10 @@ require_relative 'mailhandler/receiver'
|
|
3
3
|
|
4
4
|
require_relative 'mailhandler/receiving/notification/email'
|
5
5
|
require_relative 'mailhandler/receiving/notification/console'
|
6
|
+
require_relative 'mailhandler/errors'
|
7
|
+
|
8
|
+
# Main MailHandler class, that allows you to create sender and receiver objects.
|
9
|
+
# Sender objects for sending emails, receiver objects for receiving emails from certain mailboxes.
|
6
10
|
|
7
11
|
module MailHandler
|
8
12
|
|
@@ -72,6 +76,7 @@ module MailHandler
|
|
72
76
|
|
73
77
|
private
|
74
78
|
|
79
|
+
# method for adding custom notifications, in case email delivery is delayed.
|
75
80
|
def add_receiving_notifications(receiver, notifications)
|
76
81
|
|
77
82
|
if (notifications - NOTIFICATION_TYPES.keys).empty?
|
@@ -84,7 +89,7 @@ module MailHandler
|
|
84
89
|
|
85
90
|
def verify_type(type, types)
|
86
91
|
|
87
|
-
raise
|
92
|
+
raise MailHandler::TypeError, "Unknown type - #{type}, possible options: #{types.keys}" unless types.keys.include? type
|
88
93
|
|
89
94
|
end
|
90
95
|
|
data/lib/mailhandler/receiver.rb
CHANGED
@@ -10,7 +10,7 @@ module MailHandler
|
|
10
10
|
|
11
11
|
attr_accessor :checker,
|
12
12
|
:search,
|
13
|
-
:
|
13
|
+
:max_search_duration
|
14
14
|
|
15
15
|
module DEFAULTS
|
16
16
|
|
@@ -32,7 +32,7 @@ module MailHandler
|
|
32
32
|
def initialize(checker)
|
33
33
|
|
34
34
|
@checker = checker
|
35
|
-
@
|
35
|
+
@max_search_duration = DEFAULTS::MAX_SEARCH_DURATION
|
36
36
|
|
37
37
|
end
|
38
38
|
|
@@ -62,7 +62,7 @@ module MailHandler
|
|
62
62
|
@search = Search.new
|
63
63
|
@search.options = options
|
64
64
|
@search.started_at = Time.now
|
65
|
-
@search.max_duration = @
|
65
|
+
@search.max_duration = @max_search_duration
|
66
66
|
|
67
67
|
end
|
68
68
|
|
@@ -78,7 +78,7 @@ module MailHandler
|
|
78
78
|
|
79
79
|
def search_time_expired?
|
80
80
|
|
81
|
-
(Time.now - search.started_at) > @
|
81
|
+
(Time.now - search.started_at) > @max_search_duration
|
82
82
|
|
83
83
|
end
|
84
84
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require_relative '../errors'
|
2
|
+
|
1
3
|
module MailHandler
|
2
4
|
|
3
5
|
module Receiving
|
@@ -22,21 +24,16 @@ module MailHandler
|
|
22
24
|
|
23
25
|
]
|
24
26
|
|
25
|
-
DEFAULT_SEARCH_OPTIONS = [
|
26
|
-
|
27
|
-
:archive
|
28
|
-
]
|
29
|
-
|
30
27
|
def initialize
|
31
28
|
|
32
|
-
|
29
|
+
set_base_search_options
|
33
30
|
reset_found_emails
|
34
31
|
|
35
32
|
end
|
36
33
|
|
37
34
|
def find(options)
|
38
35
|
|
39
|
-
raise
|
36
|
+
raise MailHandler::Error, 'Method not implemented'
|
40
37
|
|
41
38
|
end
|
42
39
|
|
@@ -54,31 +51,61 @@ module MailHandler
|
|
54
51
|
|
55
52
|
protected
|
56
53
|
|
57
|
-
def
|
54
|
+
def verify_and_set_search_options(options)
|
58
55
|
|
59
|
-
|
60
|
-
|
56
|
+
validate_used_options(options)
|
57
|
+
validate_option_values(options)
|
58
|
+
|
59
|
+
set_base_search_options
|
60
|
+
add_additional_search_options(options)
|
61
|
+
reset_found_emails
|
61
62
|
|
62
63
|
end
|
63
64
|
|
64
|
-
def
|
65
|
+
def validate_option_values(options)
|
65
66
|
|
66
|
-
|
67
|
-
validate_used_options(options)
|
67
|
+
if options[:by_date]
|
68
68
|
|
69
|
-
|
70
|
-
|
69
|
+
raise MailHandler::Error, "Incorrect option options[:by_date]=#{options[:date]}" unless options[:by_date].is_a? Time
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
unless options[:count].nil?
|
74
|
+
|
75
|
+
count = options[:count]
|
76
|
+
raise MailHandler::Error, "Incorrect option options[:count]=#{options[:count]}" if count < 0 or count > 2000
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
if options[:archive]
|
81
|
+
|
82
|
+
raise MailHandler::Error, "Incorrect option options[:archive]=#{options[:archive]}" unless options[:archive] == true or options[:archive] == false
|
83
|
+
|
84
|
+
end
|
71
85
|
|
72
86
|
end
|
73
87
|
|
74
88
|
def validate_used_options(options)
|
75
89
|
|
76
90
|
unless (options.keys - AVAILABLE_SEARCH_OPTIONS).empty?
|
77
|
-
raise
|
91
|
+
raise MailHandler::Error, "#{(options.keys - AVAILABLE_SEARCH_OPTIONS)} - Incorrect search option values, options are #{AVAILABLE_SEARCH_OPTIONS}"
|
78
92
|
end
|
79
93
|
|
80
94
|
end
|
81
95
|
|
96
|
+
def set_base_search_options
|
97
|
+
|
98
|
+
# Default number of email results to return, and whether to archive emails.
|
99
|
+
@search_options = {:count => 50, :archive => false}
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
def add_additional_search_options(options)
|
104
|
+
|
105
|
+
@search_options = @search_options.merge options
|
106
|
+
|
107
|
+
end
|
108
|
+
|
82
109
|
end
|
83
110
|
|
84
111
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require_relative 'base.rb'
|
2
2
|
require_relative 'filter'
|
3
3
|
require 'mail'
|
4
|
+
require_relative '../errors'
|
4
5
|
|
5
6
|
module MailHandler
|
6
7
|
|
@@ -107,7 +108,7 @@ module MailHandler
|
|
107
108
|
# create folders if they don't exist
|
108
109
|
def setup_inbox_folders
|
109
110
|
|
110
|
-
raise
|
111
|
+
raise MailHandler::Error, 'Folder variables are not set' if inbox_folder.nil? or archive_folder.nil?
|
111
112
|
|
112
113
|
Dir::mkdir inbox_folder unless File.directory? inbox_folder
|
113
114
|
Dir::mkdir archive_folder unless File.directory? archive_folder
|
@@ -1,6 +1,8 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'mail'
|
2
4
|
require_relative 'base.rb'
|
3
|
-
|
5
|
+
require_relative '../errors'
|
4
6
|
|
5
7
|
module MailHandler
|
6
8
|
|
@@ -18,6 +20,7 @@ module MailHandler
|
|
18
20
|
AVAILABLE_SEARCH_OPTIONS = [
|
19
21
|
|
20
22
|
:by_subject,
|
23
|
+
:by_content,
|
21
24
|
:count,
|
22
25
|
:archive,
|
23
26
|
:by_recipient
|
@@ -111,7 +114,11 @@ module MailHandler
|
|
111
114
|
|
112
115
|
when :by_subject
|
113
116
|
|
114
|
-
keys << 'SUBJECT' << options[:by_subject]
|
117
|
+
keys << 'SUBJECT' << options[:by_subject].to_s
|
118
|
+
|
119
|
+
when :by_content
|
120
|
+
|
121
|
+
keys << 'BODY' << options[:by_content].to_s
|
115
122
|
|
116
123
|
else
|
117
124
|
|
@@ -128,7 +135,7 @@ module MailHandler
|
|
128
135
|
def validate_options(options)
|
129
136
|
|
130
137
|
unless (options.keys - AVAILABLE_SEARCH_OPTIONS).empty?
|
131
|
-
raise
|
138
|
+
raise MailHandler::Error, "#{(options.keys - AVAILABLE_SEARCH_OPTIONS)} - Not supported search option values for imap, options are #{AVAILABLE_SEARCH_OPTIONS}"
|
132
139
|
end
|
133
140
|
|
134
141
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require_relative 'email/content'
|
2
2
|
require_relative 'email/states'
|
3
|
+
require_relative '../../errors'
|
3
4
|
|
4
5
|
module MailHandler
|
5
6
|
|
@@ -58,7 +59,7 @@ module MailHandler
|
|
58
59
|
|
59
60
|
def verify_email_type(type)
|
60
61
|
|
61
|
-
raise
|
62
|
+
raise MailHandler::TypeError, "Incorrect type: #{type}, allowed types: #{EMAIL_TYPES}" unless EMAIL_TYPES.include? type
|
62
63
|
|
63
64
|
end
|
64
65
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require_relative '../email'
|
2
|
+
require_relative '../../../errors'
|
2
3
|
|
3
4
|
module MailHandler
|
4
5
|
|
@@ -25,7 +26,7 @@ module MailHandler
|
|
25
26
|
|
26
27
|
def notify(search)
|
27
28
|
|
28
|
-
raise
|
29
|
+
raise MailHandler::Error, 'notify(search) interface has to be implemented'
|
29
30
|
|
30
31
|
end
|
31
32
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require_relative 'api.rb'
|
2
|
+
require_relative '../errors'
|
2
3
|
|
3
4
|
module MailHandler
|
4
5
|
|
@@ -18,7 +19,7 @@ module MailHandler
|
|
18
19
|
|
19
20
|
def verify_email(emails)
|
20
21
|
|
21
|
-
raise
|
22
|
+
raise MailHandler::TypeError, "Invalid type error, only Array of Mail::Message object types for sending allowed" unless emails.is_a?(Array) && emails.all? { |e| e.is_a? Mail::Message }
|
22
23
|
|
23
24
|
end
|
24
25
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require_relative '../errors'
|
2
|
+
|
1
3
|
module MailHandler
|
2
4
|
|
3
5
|
module Sending
|
@@ -10,7 +12,15 @@ module MailHandler
|
|
10
12
|
|
11
13
|
def verify_email(email)
|
12
14
|
|
13
|
-
raise
|
15
|
+
raise MailHandler::TypeError, "Invalid type error, only #{allowed_email_type} object type for sending allowed" unless email.is_a? allowed_email_type
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def allowed_email_type
|
22
|
+
|
23
|
+
Mail::Message
|
14
24
|
|
15
25
|
end
|
16
26
|
|
data/lib/mailhandler/version.rb
CHANGED
data/mailhandler.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
|
20
20
|
s.files = `git ls-files`.split($/)
|
21
21
|
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
22
|
-
s.homepage = 'https://github.com/wildbit'
|
22
|
+
s.homepage = 'https://github.com/wildbit/mailhandler'
|
23
23
|
s.require_paths = ["lib"]
|
24
24
|
|
25
25
|
s.post_install_message = %q{
|
data/readme.md
CHANGED
@@ -61,11 +61,16 @@ email_receiver.find_email(:by_subject => subject, :archive => true)
|
|
61
61
|
You can search imap mailbox by following options:
|
62
62
|
|
63
63
|
* `:by_subject` - subject of the email
|
64
|
+
* `:by_content` - search email content by keywords
|
65
|
+
* `:by_recipient` - by email recipient
|
64
66
|
|
65
67
|
You can search local mailbox by following options:
|
66
68
|
|
67
69
|
* `:by_subject` - subject of the email
|
68
70
|
* `:by_recipient` - by email recipient
|
71
|
+
* `:by_content` - search email content by keywords
|
72
|
+
|
73
|
+
Recipient to search by needs to by added in following form: `by_recipient => { :to => 'igor@example.com' }`.
|
69
74
|
|
70
75
|
If you would like email to be archived after its read, use `:archive => true` option (recommended)
|
71
76
|
|
@@ -23,7 +23,7 @@ describe MailHandler::Receiver do
|
|
23
23
|
|
24
24
|
it { is_expected.to respond_to(:checker) }
|
25
25
|
it { is_expected.to respond_to(:search) }
|
26
|
-
it { is_expected.to respond_to(:
|
26
|
+
it { is_expected.to respond_to(:max_search_duration) }
|
27
27
|
|
28
28
|
end
|
29
29
|
|
@@ -31,7 +31,7 @@ describe MailHandler::Receiver do
|
|
31
31
|
|
32
32
|
it { is_expected.to respond_to(:checker=) }
|
33
33
|
it { is_expected.to respond_to(:search=) }
|
34
|
-
it { is_expected.to respond_to(:
|
34
|
+
it { is_expected.to respond_to(:max_search_duration=) }
|
35
35
|
|
36
36
|
end
|
37
37
|
|
@@ -80,7 +80,7 @@ describe MailHandler::Receiver do
|
|
80
80
|
|
81
81
|
it "max duration - #{duration} seconds" do
|
82
82
|
|
83
|
-
receiver.
|
83
|
+
receiver.max_search_duration = duration
|
84
84
|
receiver.find_email(default_search_option)
|
85
85
|
|
86
86
|
expect(receiver.search.duration).to be_within(1).of(duration)
|
@@ -18,13 +18,93 @@ describe MailHandler::Receiving::FolderChecker do
|
|
18
18
|
|
19
19
|
context 'search options' do
|
20
20
|
|
21
|
-
it '
|
21
|
+
it 'by multiple search options' do
|
22
|
+
|
23
|
+
time = Time.now
|
24
|
+
checker.find({:by_subject => 'test', :by_content => 'test', :by_date => time, :by_recipient => 'igor@example.com'})
|
25
|
+
expect(checker.search_options).to eq(
|
26
|
+
{:count=>50,
|
27
|
+
:archive=>false,
|
28
|
+
:by_subject => 'test',
|
29
|
+
:by_content => 'test',
|
30
|
+
:by_date => time,
|
31
|
+
:by_recipient => 'igor@example.com'})
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'by_subject' do
|
22
36
|
|
23
37
|
checker.find({:by_subject => 'test'})
|
24
38
|
expect(checker.search_options).to eq({:count=>50, :archive=>false, :by_subject=>'test'})
|
25
39
|
|
26
40
|
end
|
27
41
|
|
42
|
+
it 'by_content' do
|
43
|
+
|
44
|
+
checker.find({:by_content => 'test'})
|
45
|
+
expect(checker.search_options).to eq({:count=>50, :archive=>false, :by_content => 'test'})
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'archive' do
|
50
|
+
|
51
|
+
it 'invalid' do
|
52
|
+
|
53
|
+
expect { checker.find({:archive => 'test'}) }.to raise_error MailHandler::Error
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'by_date' do
|
60
|
+
|
61
|
+
it 'valid' do
|
62
|
+
|
63
|
+
time = Time.now
|
64
|
+
checker.find({:by_date => time})
|
65
|
+
expect(checker.search_options).to eq({:count=>50, :archive=>false, :by_date => time })
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'invalid' do
|
70
|
+
|
71
|
+
time = Time.now.to_s
|
72
|
+
expect { checker.find({:by_date => time}) }.to raise_error MailHandler::Error
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'by_recipient' do
|
79
|
+
|
80
|
+
checker.find({:by_recipient => 'igor@example.com'})
|
81
|
+
expect(checker.search_options).to eq({:count=>50, :archive=>false, :by_recipient => 'igor@example.com' })
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'count' do
|
86
|
+
|
87
|
+
it 'valid' do
|
88
|
+
|
89
|
+
checker.find({:count => 1})
|
90
|
+
expect(checker.search_options).to eq({:count=>1, :archive=>false})
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'invalid - below limit' do
|
95
|
+
|
96
|
+
expect { checker.find({:count => -1}) }.to raise_error MailHandler::Error
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'invalid - above limit' do
|
101
|
+
|
102
|
+
expect { checker.find({:count => 3000 }) }.to raise_error MailHandler::Error
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
28
108
|
end
|
29
109
|
|
30
110
|
context 'not found' do
|
@@ -32,7 +32,7 @@ describe MailHandler::Sending::PostmarkBatchAPISender do
|
|
32
32
|
|
33
33
|
sender = subject.new(api_token)
|
34
34
|
expect{sender.send('test')}.
|
35
|
-
to raise_error
|
35
|
+
to raise_error MailHandler::TypeError, 'Invalid type error, only Array of Mail::Message object types for sending allowed'
|
36
36
|
|
37
37
|
end
|
38
38
|
|
@@ -40,7 +40,7 @@ describe MailHandler::Sending::PostmarkBatchAPISender do
|
|
40
40
|
|
41
41
|
sender = subject.new(api_token)
|
42
42
|
expect{sender.send([1,2,2])}.
|
43
|
-
to raise_error
|
43
|
+
to raise_error MailHandler::TypeError, 'Invalid type error, only Array of Mail::Message object types for sending allowed'
|
44
44
|
|
45
45
|
end
|
46
46
|
|
@@ -9,7 +9,7 @@ describe MailHandler::Handler do
|
|
9
9
|
it 'create - invalid type' do
|
10
10
|
|
11
11
|
expect { subject.init_receiver(:test) }.
|
12
|
-
to raise_error(
|
12
|
+
to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:folder, :imap]')
|
13
13
|
|
14
14
|
end
|
15
15
|
|
@@ -32,7 +32,7 @@ describe MailHandler::Handler do
|
|
32
32
|
it 'create - invalid type' do
|
33
33
|
|
34
34
|
expect { subject.init_sender(:test) }.
|
35
|
-
to raise_error(
|
35
|
+
to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:postmark_api, :postmark_batch_api, :smtp]')
|
36
36
|
|
37
37
|
end
|
38
38
|
|
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.10
|
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-12-
|
11
|
+
date: 2015-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mail
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- Gemfile.lock
|
60
60
|
- Rakefile
|
61
61
|
- lib/mailhandler.rb
|
62
|
+
- lib/mailhandler/errors.rb
|
62
63
|
- lib/mailhandler/receiver.rb
|
63
64
|
- lib/mailhandler/receiving/base.rb
|
64
65
|
- lib/mailhandler/receiving/filter.rb
|
@@ -90,7 +91,7 @@ files:
|
|
90
91
|
- spec/unit/mailhandler/sending/sender_api_batch_spec.rb
|
91
92
|
- spec/unit/mailhandler/sending/sender_api_spec.rb
|
92
93
|
- spec/unit/mailhandler_spec.rb
|
93
|
-
homepage: https://github.com/wildbit
|
94
|
+
homepage: https://github.com/wildbit/mailhandler
|
94
95
|
licenses:
|
95
96
|
- MIT
|
96
97
|
metadata: {}
|