Class: EmailDomainChecker::DomainValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/email_domain_checker/domain_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DomainValidator

Returns a new instance of DomainValidator.



10
11
12
13
14
15
16
17
18
# File 'lib/email_domain_checker/domain_validator.rb', line 10

def initialize(options = {})
  @options = {
    check_mx: true,
    check_a: false,
    timeout: 5
  }.merge(options)
  cache = Config.cache_enabled? ? Config.cache_adapter : nil
  @dns_resolver = DnsResolver.new(timeout: @options[:timeout], cache: cache)
end

Instance Attribute Details

#dns_resolverObject (readonly)

Returns the value of attribute dns_resolver.



8
9
10
# File 'lib/email_domain_checker/domain_validator.rb', line 8

def dns_resolver
  @dns_resolver
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/email_domain_checker/domain_validator.rb', line 8

def options
  @options
end

Instance Method Details

#valid?(domain) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/email_domain_checker/domain_validator.rb', line 20

def valid?(domain)
  return false if domain.nil? || domain.empty?

  # Check whitelist first (if configured)
  unless Config.whitelist_domains.nil? || Config.whitelist_domains.empty?
    return whitelisted?(domain)
  end

  # Check blacklist
  return false if blacklisted?(domain)

  # Check custom domain checker
  if Config.domain_checker
    custom_result = Config.domain_checker.call(domain)
    return false unless custom_result
  end

  # Skip DNS checks if test mode is enabled
  return true if Config.test_mode?

  check_domain_records(domain)
end