Skip to content

Commit

Permalink
Technical/Refactor Configuration class (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
bestwebua committed Feb 5, 2021
1 parent d5f7201 commit 995b835
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ detectors:
- Truemail::Log::Serializer::Base#errors
- Truemail::Log::Serializer::ValidatorBase#replace_invalid_chars
- Truemail::Dns::Worker#nameserver_port
- Truemail::Configuration#dns_server_matcher
- Truemail::Configuration#check_dns_settings

ControlParameter:
exclude:
Expand Down Expand Up @@ -77,6 +77,7 @@ detectors:

TooManyConstants:
exclude:
- Truemail::Configuration
- Truemail::RegexConstant

exclude_paths:
Expand Down
55 changes: 24 additions & 31 deletions lib/truemail/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ class Configuration
DEFAULT_CONNECTION_ATTEMPTS = 2
DEFAULT_VALIDATION_TYPE = :smtp
DEFAULT_LOGGER_OPTIONS = { tracking_event: :error, stdout: false, log_absolute_path: nil }.freeze

attr_reader :email_pattern,
:smtp_error_body_pattern,
:verifier_email,
SETTERS = %i[
email_pattern
smtp_error_body_pattern
connection_timeout
response_timeout
connection_attempts
whitelisted_domains
blacklisted_domains
].freeze

attr_reader :verifier_email,
:verifier_domain,
:connection_timeout,
:response_timeout,
:connection_attempts,
:default_validation_type,
:validation_type_by_domain,
:whitelisted_domains,
:blacklisted_domains,
:dns,
:logger
:logger,
*Truemail::Configuration::SETTERS

attr_accessor :whitelist_validation, :not_rfc_mx_lookup_flow, :smtp_fail_fast, :smtp_safe_check

Expand All @@ -31,13 +34,6 @@ def initialize(&block)
tap(&block) if block
end

%i[email_pattern smtp_error_body_pattern].each do |method|
define_method("#{method}=") do |argument|
raise_unless(argument, __method__, argument.is_a?(::Regexp))
instance_variable_set(:"@#{method}", argument)
end
end

def verifier_email=(email)
validate_arguments(email, __method__)
@verifier_email = email.downcase
Expand All @@ -49,13 +45,6 @@ def verifier_domain=(domain)
@verifier_domain = domain.downcase
end

%i[connection_timeout response_timeout connection_attempts].each do |method|
define_method("#{method}=") do |argument|
raise_unless(argument, __method__, argument.is_a?(::Integer) && argument.positive?)
instance_variable_set(:"@#{method}", argument)
end
end

def default_validation_type=(argument)
raise_unless(argument, __method__, argument.is_a?(::Symbol) && Truemail::Validator::VALIDATION_TYPES.include?(argument))
@default_validation_type = argument
Expand All @@ -66,9 +55,17 @@ def validation_type_for=(settings)
validation_type_by_domain.merge!(settings)
end

%i[whitelisted_domains blacklisted_domains dns].each do |method|
def argument_consistent?(argument)
case argument
when ::Array then check_domain_list(argument)
when ::Integer then argument.positive?
when ::Regexp then true
end
end

Truemail::Configuration::SETTERS.each do |method|
define_method("#{method}=") do |argument|
raise_unless(argument, __method__, argument.is_a?(::Array) && check_domain_list(argument))
raise_unless(argument, __method__, argument_consistent?(argument))
instance_variable_set(:"@#{method}", argument)
end
end
Expand Down Expand Up @@ -151,12 +148,8 @@ def validate_validation_type(settings)
end
end

def dns_server_matcher
->(dns_server) { Truemail::RegexConstant::REGEX_DNS_SERVER_ADDRESS_PATTERN.match?(dns_server.to_s) }
end

def check_dns_settings(dns_servers)
dns_servers.all?(&dns_server_matcher)
dns_servers.all? { |dns_server| Truemail::RegexConstant::REGEX_DNS_SERVER_ADDRESS_PATTERN.match?(dns_server.to_s) }
end

def logger_options(current_options)
Expand Down
2 changes: 1 addition & 1 deletion spec/truemail/audit/dns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
end

context 'when a record of verifier domain not refers to currernt host ip address' do
before { dns_mock_server.assign_mocks(verifier_domain => { a: [current_host_ip.next] }) }
before { dns_mock_server.assign_mocks(verifier_domain => { a: [Faker::Internet.ip_v4_address] }) }

include_examples 'addes verifier domain not refer warning to result instance'
end
Expand Down
16 changes: 16 additions & 0 deletions spec/truemail/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@
specify { expect(described_class).to be_const_defined(:DEFAULT_LOGGER_OPTIONS) }
specify { expect(described_class::DEFAULT_LOGGER_OPTIONS).to eq(tracking_event: :error, stdout: false, log_absolute_path: nil) }
end

context 'SETTERS' do
specify { expect(described_class).to be_const_defined(:SETTERS) }

specify do
expect(described_class::SETTERS).to include(
:email_pattern,
:smtp_error_body_pattern,
:connection_timeout,
:response_timeout,
:connection_attempts,
:whitelisted_domains,
:blacklisted_domains
)
end
end
end

describe '.new' do
Expand Down

0 comments on commit 995b835

Please sign in to comment.