diff --git a/.reek.yml b/.reek.yml index 26ba7e8..675aa8a 100644 --- a/.reek.yml +++ b/.reek.yml @@ -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: @@ -77,6 +77,7 @@ detectors: TooManyConstants: exclude: + - Truemail::Configuration - Truemail::RegexConstant exclude_paths: diff --git a/lib/truemail/configuration.rb b/lib/truemail/configuration.rb index ab6b18a..7f25396 100644 --- a/lib/truemail/configuration.rb +++ b/lib/truemail/configuration.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/spec/truemail/audit/dns_spec.rb b/spec/truemail/audit/dns_spec.rb index f55c1af..53a842e 100644 --- a/spec/truemail/audit/dns_spec.rb +++ b/spec/truemail/audit/dns_spec.rb @@ -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 diff --git a/spec/truemail/configuration_spec.rb b/spec/truemail/configuration_spec.rb index 0e6c2e1..c55ee79 100644 --- a/spec/truemail/configuration_spec.rb +++ b/spec/truemail/configuration_spec.rb @@ -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