Sha256: 2974f8c79b0442ef5330c19da03e740fc6bb5083c130d798a7e1e37871776d86

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Truemail
  class Validator
    RESULT_ATTRS = %i[success email domain mail_servers errors smtp_debug].freeze
    VALIDATION_TYPES = %i[regex mx smtp].freeze

    Result = Struct.new(*RESULT_ATTRS, keyword_init: true) do
      def initialize(errors: {}, mail_servers: [], **args)
        super
      end
      alias_method :valid?, :success
    end

    attr_reader :validation_type, :result

    def initialize(email, with: Truemail.configuration.default_validation_type)
      raise Truemail::ArgumentError.new(with, :argument) unless Truemail::Validator::VALIDATION_TYPES.include?(with)
      @validation_type = select_validation_type(email, with)
      @result = Truemail::Validator::Result.new(email: email)
    end

    def run
      Truemail::Validate::DomainListMatch.check(result)
      result_not_changed? ? Truemail::Validate.const_get(validation_type.capitalize).check(result) : update_validation_type
      self
    end

    private

    def result_not_changed?
      result.success.nil?
    end

    def update_validation_type
      @validation_type = result.success ? :whitelist : :blacklist
    end

    def select_validation_type(email, current_validation_type)
      domain = email[Truemail::RegexConstant::REGEX_EMAIL_PATTERN, 3]
      Truemail.configuration.validation_type_by_domain[domain] || current_validation_type
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
truemail-1.2.1 lib/truemail/validator.rb
truemail-1.2.0 lib/truemail/validator.rb
truemail-1.1.0 lib/truemail/validator.rb