class Truemail::Validate::Mx

Constants

ERROR
NULL_MX_RECORD

Public Instance Methods

run() click to toggle source
# File lib/truemail/validate/mx.rb, line 9
def run
  return false unless Truemail::Validate::Regex.check(result)
  return true if success(email_domain_valid? && mx_lookup && domain_not_include_null_mx)
  mail_servers.clear && add_error(Truemail::Validate::Mx::ERROR)
  false
end

Private Instance Methods

a_record(hostname) click to toggle source
# File lib/truemail/validate/mx.rb, line 67
def a_record(hostname)
  Truemail::Dns::Resolver.a_record(hostname, configuration: configuration)
end
domain() click to toggle source
# File lib/truemail/validate/mx.rb, line 58
def domain
  @domain ||= result.punycode_email[Truemail::RegexConstant::REGEX_DOMAIN_FROM_EMAIL, 1]
end
domain_not_include_null_mx() click to toggle source
# File lib/truemail/validate/mx.rb, line 42
def domain_not_include_null_mx
  !mail_servers.include?(Truemail::Validate::Mx::NULL_MX_RECORD)
end
email_domain_valid?() click to toggle source
# File lib/truemail/validate/mx.rb, line 18
def email_domain_valid?
  Truemail::RegexConstant::REGEX_DOMAIN_PATTERN.match?(result.domain)
end
fetch_target_hosts(hosts) click to toggle source
# File lib/truemail/validate/mx.rb, line 33
def fetch_target_hosts(hosts)
  mail_servers.push(*(hosts.uniq - mail_servers))
end
host_extractor_methods() click to toggle source
# File lib/truemail/validate/mx.rb, line 22
def host_extractor_methods
  return %i[hosts_from_mx_records?] if configuration.not_rfc_mx_lookup_flow
  %i[hosts_from_mx_records? hosts_from_cname_records? host_from_a_record?]
end
host_from_a_record?() click to toggle source
# File lib/truemail/validate/mx.rb, line 83
def host_from_a_record?
  fetch_target_hosts([a_record(domain)])
  mail_servers_found?
end
hosts_from_cname_records?() click to toggle source
# File lib/truemail/validate/mx.rb, line 71
def hosts_from_cname_records?
  cname_records = Truemail::Dns::Resolver.cname_records(domain, configuration: configuration)
  return if cname_records.empty?
  cname_records.each do |cname_record|
    host = a_record(cname_record.name.to_s)
    hostname = Truemail::Dns::Resolver.dns_lookup(host, configuration: configuration)
    found_hosts = mx_records(hostname)
    fetch_target_hosts(found_hosts.empty? ? [host] : found_hosts)
  end
  mail_servers_found?
end
hosts_from_mx_records?() click to toggle source
# File lib/truemail/validate/mx.rb, line 62
def hosts_from_mx_records?
  fetch_target_hosts(mx_records(domain))
  mail_servers_found?
end
mail_servers_found?() click to toggle source
# File lib/truemail/validate/mx.rb, line 54
def mail_servers_found?
  !mail_servers.empty?
end
mx_lookup() click to toggle source
# File lib/truemail/validate/mx.rb, line 27
def mx_lookup
  host_extractor_methods.any? do |method|
    Truemail::Wrapper.call(configuration: configuration) { send(method) }
  end
end
mx_records(hostname) click to toggle source
# File lib/truemail/validate/mx.rb, line 46
def mx_records(hostname)
  domain_mx_records = Truemail::Dns::Resolver.mx_records(hostname, configuration: configuration)
  return [Truemail::Validate::Mx::NULL_MX_RECORD] if null_mx?(domain_mx_records)
  domain_mx_records.sort_by(&:preference).flat_map do |mx_record|
    Truemail::Dns::Resolver.a_records(mx_record.exchange.to_s, configuration: configuration)
  end
end
null_mx?(domain_mx_records) click to toggle source
# File lib/truemail/validate/mx.rb, line 37
def null_mx?(domain_mx_records)
  mx_record = domain_mx_records.first
  domain_mx_records.one? && mx_record.preference.zero? && mx_record.exchange.to_s.empty?
end