Skip to content

Commit

Permalink
Refactor Truemail::Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bestwebua committed Jun 4, 2019
1 parent d507760 commit 71bf4b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions .reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ detectors:
- Truemail::GenerateEmailHelper#calculate_email_size
- Truemail::Worker#success
- Truemail#raise_unless
- Truemail::Configuration#raise_unless

FeatureEnvy:
exclude:
Expand Down
19 changes: 11 additions & 8 deletions lib/truemail/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize

%i[email_pattern smtp_error_body_pattern].each do |method|
define_method("#{method}=") do |argument|
raise Truemail::ArgumentError.new(argument, __method__) unless argument.is_a?(Regexp)
raise_unless(argument, __method__, argument.is_a?(Regexp))
instance_variable_set(:"@#{method}", argument)
end
end
Expand All @@ -53,7 +53,7 @@ def verifier_domain=(domain)

%i[connection_timeout response_timeout connection_attempts].each do |method|
define_method("#{method}=") do |argument|
raise ArgumentError.new(argument, __method__) unless argument.is_a?(Integer) && argument.positive?
raise_unless(argument, __method__, argument.is_a?(Integer) && argument.positive?)
instance_variable_set(:"@#{method}", argument)
end
end
Expand All @@ -65,7 +65,7 @@ def validation_type_for=(settings)

%i[whitelisted_domains blacklisted_domains].each do |method|
define_method("#{method}=") do |argument|
raise ArgumentError.new(argument, __method__) unless argument.is_a?(Array) && check_domain_list(argument)
raise_unless(argument, __method__, argument.is_a?(Array) && check_domain_list(argument))
instance_variable_set(:"@#{method}", argument)
end
end
Expand All @@ -76,9 +76,13 @@ def complete?

private

def raise_unless(argument_context, argument_name, condition)
raise Truemail::ArgumentError.new(argument_context, argument_name) unless condition
end

def validate_arguments(argument, method)
constant = Truemail::RegexConstant.const_get("regex_#{method[/\A.+_(.+)\=\z/, 1]}_pattern".upcase)
raise Truemail::ArgumentError.new(argument, method) unless constant.match?(argument.to_s)
raise_unless(argument, method, constant.match?(argument.to_s))
end

def default_verifier_domain
Expand All @@ -90,20 +94,19 @@ def domain_matcher
end

def check_domain(domain)
raise Truemail::ArgumentError.new(domain, 'domain') unless domain_matcher.call(domain)
raise_unless(domain, 'domain', domain_matcher.call(domain))
end

def check_domain_list(domains)
domains.all?(&domain_matcher)
end

def check_validation_type(validation_type)
raise Truemail::ArgumentError.new(validation_type, 'validation type') unless
Truemail::Validator::VALIDATION_TYPES.include?(validation_type)
raise_unless(validation_type, 'validation type', Truemail::Validator::VALIDATION_TYPES.include?(validation_type))
end

def validate_validation_type(settings)
raise Truemail::ArgumentError.new(settings, 'hash with settings') unless settings.is_a?(Hash)
raise_unless(settings, 'hash with settings', settings.is_a?(Hash))
settings.each do |domain, validation_type|
check_domain(domain)
check_validation_type(validation_type)
Expand Down

0 comments on commit 71bf4b2

Please sign in to comment.