Sha256: bcb62d672ba3e668d8d0dea22c6324d060786a545269550535acaf14360cb6fa

Contents?: true

Size: 993 Bytes

Versions: 3

Compression:

Stored size: 993 Bytes

Contents

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: :smtp)
      raise ArgumentError.new(with, :argument) unless VALIDATION_TYPES.include?(with)
      @validation_type, @result = select_validation_type(email, with), Result.new(email: email)
    end

    def run
      Truemail::Validate.const_get(validation_type.capitalize).check(result)
      self
    end

    private

    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-0.1.1.2 lib/truemail/validator.rb
truemail-0.1.1 lib/truemail/validator.rb
truemail-0.1.0 lib/truemail/validator.rb