Skip to content

Commit bc77601

Browse files
committed
cleaning up code
1 parent e7fd977 commit bc77601

19 files changed

Lines changed: 121 additions & 99 deletions

File tree

.rubocop.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require: rubocop-rspec
2+
3+
# increase line length since we are checking test files
4+
Metrics/LineLength:
5+
Max: 120
6+
7+
Metrics/MethodLength:
8+
Max: 15

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ gem 'mail'
55
gem 'postmark'
66
gem 'pry'
77
gem 'rspec'
8+
gem 'rubocop'

lib/mailhandler/receiver.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def find_email(options)
4545
update_search_details
4646
notify_observers(search)
4747
break if received
48+
4849
sleep search_frequency
4950

5051
end

lib/mailhandler/receiving/base.rb

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ def reset_found_emails
3333
@found_emails = []
3434
end
3535

36-
private
37-
3836
AVAILABLE_SEARCH_OPTIONS = %i[
39-
4037
by_subject
4138
by_content
4239
since
@@ -59,36 +56,45 @@ def verify_and_set_search_options(options)
5956
end
6057

6158
def validate_option_values(options)
62-
if options[:since]
63-
64-
raise MailHandler::Error, "Incorrect option options[:since]=#{options[:since]}." unless options[:since].is_a?(Time)
65-
66-
end
67-
68-
unless options[:count].nil?
59+
validate_since_option(options)
60+
validate_count_option(options)
61+
validate_archive_option(options)
62+
validate_recipient_option(options)
63+
end
6964

70-
count = options[:count]
71-
raise MailHandler::Error, "Incorrect option options[:count]=#{options[:count]}." if (count < 0) || (count > 2000)
65+
def validate_recipient_option(options)
66+
return if options[:by_recipient].nil?
7267

73-
end
68+
error_message = "Incorrect option options[:by_recipient]=#{options[:by_recipient]}."
69+
raise MailHandler::Error, error_message unless options[:by_recipient].is_a?(Hash)
70+
end
7471

75-
if options[:archive]
72+
def validate_archive_option(options)
73+
return if options[:archive].nil?
7674

77-
raise MailHandler::Error, "Incorrect option options[:archive]=#{options[:archive]}." unless (options[:archive] == true) || (options[:archive] == false)
75+
error_message = "Incorrect option options[:archive]=#{options[:archive]}."
76+
raise MailHandler::Error, error_message unless [true, false].include?(options[:archive])
77+
end
7878

79-
end
79+
def validate_since_option(options)
80+
return if options[:since].nil?
8081

81-
if options[:by_recipient]
82+
error_message = "Incorrect option options[:since]=#{options[:since]}."
83+
raise MailHandler::Error, error_message unless options[:since].is_a?(Time)
84+
end
8285

83-
raise MailHandler::Error, "Incorrect option options[:by_recipient]=#{options[:by_recipient]}." unless options[:by_recipient].is_a?(Hash)
86+
def validate_count_option(options)
87+
return if options[:count].nil?
8488

85-
end
89+
count = options[:count]
90+
error_message = "Incorrect option options[:count]=#{options[:count]}."
91+
raise MailHandler::Error, error_message if (count < 0) || (count > 2000)
8692
end
8793

8894
def validate_used_options(options)
89-
unless (options.keys - available_search_options).empty?
90-
raise MailHandler::Error, "#{(options.keys - available_search_options)} - Incorrect search option values, options are #{available_search_options}."
91-
end
95+
error_message = "#{(options.keys - available_search_options)} - Incorrect search option values,"\
96+
" options are #{available_search_options}."
97+
raise MailHandler::Error, error_message unless (options.keys - available_search_options).empty?
9298
end
9399

94100
def set_base_search_options

lib/mailhandler/receiving/filelist/base.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
# Base filtering class, which is used for reading list of all files based on passed pattern.
44
# Patterns to be used can be checked here: http://ruby-doc.org/core-1.9.3/Dir.html
5-
65
module MailHandler
6+
# namespace
77
module Receiving
8+
# namespace
89
module FileHandling
910
# if file exists, execute file operation, otherwise return default return value when it doesn't
1011
def access_file(file, default_return = false)
@@ -14,6 +15,7 @@ def access_file(file, default_return = false)
1415
yield
1516
rescue StandardError => e
1617
raise e if File.exist? file
18+
1719
default_return
1820
end
1921

@@ -25,6 +27,7 @@ def access_file(file, default_return = false)
2527
end
2628
end
2729

30+
# base filelist
2831
class FileList
2932
include FileHandling
3033

@@ -37,25 +40,33 @@ def sort(files)
3740
j = 0
3841

3942
while swapped
40-
4143
swapped = false
4244
j += 1
4345

4446
(files.size - j).times do |i|
45-
file1 = access_file(files[i], false) { File.new(files[i]).ctime }
46-
file2 = access_file(files[i + 1], false) { File.new(files[i + 1]).ctime }
47+
next unless swap_files?(files[i], files[i + 1])
4748

48-
next unless file1 && file2 && file1 < file2
4949
tmp = files[i]
5050
files[i] = files[i + 1]
5151
files[i + 1] = tmp
5252
swapped = true
5353
end
54-
5554
end
5655

5756
files
5857
end
58+
59+
private
60+
61+
def swap_files?(current_file, next_file)
62+
file1 = get_file(current_file)
63+
file2 = get_file(next_file)
64+
file1 && file2 && file1 < file2
65+
end
66+
67+
def get_file(file)
68+
access_file(file, false) { File.new(file).ctime }
69+
end
5970
end
6071
end
6172
end

lib/mailhandler/receiving/filelist/filter/base.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
module MailHandler
55
module Receiving
6+
# namespace
67
class FileList
8+
# namespace
79
module Filter
10+
# base filter for files
811
class Base
912
attr_accessor :files, :fast_check
1013

@@ -36,27 +39,29 @@ def ignore_exception
3639
end
3740

3841
module ByDate
39-
42+
# filter files by date
4043
class BaseDate < Base
4144
def initialize(files, date)
4245
super(files)
4346
@date = date
4447
end
4548
end
4649

50+
# since date filter
4751
class Since < BaseDate
4852
private
4953

5054
def meets_expectation?(file)
51-
File.exist?(file)? (File.ctime file) > @date : false
55+
File.exist?(file) ? (File.ctime file) > @date : false
5256
end
5357
end
5458

59+
# before date filter
5560
class Before < Base
5661
private
5762

5863
def meets_expectation?(file)
59-
File.exist?(file)? (File.ctime file) < @date : false
64+
File.exist?(file) ? (File.ctime file) < @date : false
6065
end
6166
end
6267
end

lib/mailhandler/receiving/filelist/filter/email.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def check_content_slow(_file)
3333
end
3434
end
3535

36+
# filter by email content
3637
class ByEmailContent < Email
3738
def initialize(files, content)
3839
super(files)
@@ -60,6 +61,7 @@ def check_content_slow(file)
6061
end
6162
end
6263

64+
# filter by email subject
6365
class ByEmailSubject < ByEmailContent
6466
private
6567

@@ -72,6 +74,7 @@ def check_content_slow(file)
7274
end
7375
end
7476

77+
# filter by email recipient
7578
class ByEmailRecipient < Email
7679
def initialize(files, recipient)
7780
super(files)

lib/mailhandler/receiving/folder.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
module MailHandler
99
module Receiving
10+
# folder checking base class
1011
class FolderChecker < Checker
1112
include FileHandling
1213

@@ -76,7 +77,7 @@ def filter_files(files, options)
7677
end
7778

7879
def move_files(files)
79-
files.each { |file| (inbox_folder == archive_folder) ? delete_file(file) : archive_file(file) }
80+
files.each { |file| inbox_folder == archive_folder ? delete_file(file) : archive_file(file) }
8081
end
8182

8283
def parse_email_from_files(files, count)
@@ -94,7 +95,10 @@ def read_files(files, count)
9495
end
9596

9697
def archive_file(file)
97-
access_file(file) { FileUtils.mv("#{inbox_folder}/#{File.basename(file)}", "#{archive_folder}/#{File.basename(file)}") }
98+
access_file(file) do
99+
FileUtils.mv("#{inbox_folder}/#{File.basename(file)}",
100+
"#{archive_folder}/#{File.basename(file)}")
101+
end
98102
end
99103

100104
def delete_file(file)
@@ -103,7 +107,8 @@ def delete_file(file)
103107

104108
def verify_mailbox_folders
105109
raise MailHandler::Error, 'Folder variables are not set.' if inbox_folder.nil? || archive_folder.nil?
106-
raise MailHandler::FileError, 'Mailbox folders do not exist.' unless File.directory?(inbox_folder) && File.directory?(archive_folder)
110+
raise MailHandler::FileError, 'Mailbox folders do not exist.' unless File.directory?(inbox_folder) &&
111+
File.directory?(archive_folder)
107112
end
108113
end
109114
end

0 commit comments

Comments
 (0)