Sha256: 702b763f44c4dd7abf03323ee8102d421932938fc6bc19e3f67bf2fc6019e91c

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require_relative 'truemail/core'

module Truemail
  INCOMPLETE_CONFIG = 'verifier_email is required parameter'
  NOT_CONFIGURED = 'use Truemail.configure before or pass custom configuration'
  INVALID_TYPE = 'email should be a String'

  class << self
    def configuration(&block)
      @configuration ||= begin
        return unless block_given?
        configuration = Truemail::Configuration.new(&block)
        raise_unless(configuration.complete?, Truemail::INCOMPLETE_CONFIG)
        configuration
      end
    end

    def configure(&block)
      configuration(&block)
    end

    def reset_configuration!
      @configuration = nil
    end

    def validate(email, custom_configuration: nil, **options)
      check_argument_type(email)
      Truemail::Validator.new(email, configuration: determine_configuration(custom_configuration), **options).run
    end

    def valid?(email, **options)
      check_argument_type(email)
      validate(email, **options).result.valid?
    end

    def host_audit(custom_configuration: nil)
      Truemail::Auditor.new(configuration: determine_configuration(custom_configuration)).run
    end

    private

    def raise_unless(condition, message, error_class = Truemail::ConfigurationError)
      raise error_class, message unless condition
    end

    def check_argument_type(argument)
      raise_unless(argument.is_a?(String), Truemail::INVALID_TYPE, Truemail::TypeError)
    end

    def determine_configuration(custom_configuration)
      current_configuration = custom_configuration || configuration
      raise_unless(current_configuration, Truemail::NOT_CONFIGURED)
      raise_unless(current_configuration.complete?, Truemail::INCOMPLETE_CONFIG)
      current_configuration.dup.freeze
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
truemail-2.2.0 lib/truemail.rb
truemail-2.1.0 lib/truemail.rb
truemail-2.0.2 lib/truemail.rb
truemail-2.0.1 lib/truemail.rb
truemail-2.0.0 lib/truemail.rb
truemail-1.9.2 lib/truemail.rb