lib/truemail/configuration.rb in truemail-2.2.3 vs lib/truemail/configuration.rb in truemail-2.3.0

- old
+ new

@@ -5,40 +5,37 @@ DEFAULT_CONNECTION_TIMEOUT = 2 DEFAULT_RESPONSE_TIMEOUT = 2 DEFAULT_CONNECTION_ATTEMPTS = 2 DEFAULT_VALIDATION_TYPE = :smtp DEFAULT_LOGGER_OPTIONS = { tracking_event: :error, stdout: false, log_absolute_path: nil }.freeze + SETTERS = %i[ + email_pattern + smtp_error_body_pattern + connection_timeout + response_timeout + connection_attempts + whitelisted_domains + blacklisted_domains + ].freeze - attr_reader :email_pattern, - :smtp_error_body_pattern, - :verifier_email, + attr_reader :verifier_email, :verifier_domain, - :connection_timeout, - :response_timeout, - :connection_attempts, :default_validation_type, :validation_type_by_domain, - :whitelisted_domains, - :blacklisted_domains, - :logger + :dns, + :logger, + *Truemail::Configuration::SETTERS attr_accessor :whitelist_validation, :not_rfc_mx_lookup_flow, :smtp_fail_fast, :smtp_safe_check def initialize(&block) instance_initializer.each do |instace_variable, value| instance_variable_set(:"@#{instace_variable}", value) end 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 default_verifier_domain end @@ -46,39 +43,45 @@ def verifier_domain=(domain) validate_arguments(domain, __method__) @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)) + raise_unless(argument, __method__, argument.is_a?(::Symbol) && Truemail::Validator::VALIDATION_TYPES.include?(argument)) @default_validation_type = argument end def validation_type_for=(settings) validate_validation_type(settings) validation_type_by_domain.merge!(settings) end - %i[whitelisted_domains blacklisted_domains].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 + def dns=(argument) + raise_unless(argument, __method__, argument.is_a?(::Array) && check_dns_settings(argument)) + @dns = argument + end + def logger=(options) tracking_event, stdout, log_absolute_path = logger_options(options) valid_event = Truemail::Log::Event::TRACKING_EVENTS.key?(tracking_event) stdout_only = stdout && log_absolute_path.nil? - file_only = log_absolute_path.is_a?(String) + file_only = log_absolute_path.is_a?(::String) both_types = stdout && file_only argument_info = valid_event ? log_absolute_path : tracking_event raise_unless(argument_info, __method__, valid_event && (stdout_only || file_only || both_types)) @logger = Truemail::Logger.new(tracking_event, stdout, log_absolute_path) end @@ -87,11 +90,11 @@ !!verifier_email end private - def instance_initializer + def instance_initializer # rubocop:disable Metrics/MethodLength { email_pattern: Truemail::RegexConstant::REGEX_EMAIL_PATTERN, smtp_error_body_pattern: Truemail::RegexConstant::REGEX_SMTP_ERROR_BODY_PATTERN, connection_timeout: Truemail::Configuration::DEFAULT_CONNECTION_TIMEOUT, response_timeout: Truemail::Configuration::DEFAULT_RESPONSE_TIMEOUT, @@ -99,10 +102,11 @@ default_validation_type: Truemail::Configuration::DEFAULT_VALIDATION_TYPE, validation_type_by_domain: {}, whitelisted_domains: [], whitelist_validation: false, blacklisted_domains: [], + dns: [], not_rfc_mx_lookup_flow: false, smtp_fail_fast: false, smtp_safe_check: false } end @@ -135,14 +139,18 @@ def check_validation_type(validation_type) raise_unless(validation_type, 'validation type', Truemail::Validator::VALIDATION_TYPES.include?(validation_type)) end def validate_validation_type(settings) - raise_unless(settings, 'hash with settings', 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) end + end + + def check_dns_settings(dns_servers) + dns_servers.all? { |dns_server| Truemail::RegexConstant::REGEX_DNS_SERVER_ADDRESS_PATTERN.match?(dns_server.to_s) } end def logger_options(current_options) Truemail::Configuration::DEFAULT_LOGGER_OPTIONS.merge(current_options).values end