Sha256: 0128296081d5f833491f45adec978f4ad806d1a043bee32f841a6ca6f1953e2a

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'truemail/core'

module Truemail
  INCOMPLETE_CONFIG = 'verifier_email is required parameter'
  NOT_CONFIGURED = 'use Truemail.configure before or pass custom configuration'

  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)
      Truemail::Validator.new(email, configuration: determine_configuration(custom_configuration), **options).run
    end

    def valid?(email, **options)
      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)
      raise Truemail::ConfigurationError, message unless condition
    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

3 entries across 3 versions & 1 rubygems

Version Path
truemail-1.7.1 lib/truemail.rb
truemail-1.7.0 lib/truemail.rb
truemail-1.6.1 lib/truemail.rb