Sha256: 213bc9bc0f7488a4f505c5c253d42bd53d3eedf9352a347091994a347bbad171

Contents?: true

Size: 739 Bytes

Versions: 3

Compression:

Stored size: 739 Bytes

Contents

module Truemail
  module Validate
    class Mx < Truemail::Validate::Base
      require 'resolv'

      ERROR = 'mx records not found'.freeze

      def run
        return false unless Truemail::Validate::Regex.check(result)
        result.domain = result.email[Truemail::RegexConstant::REGEX_EMAIL_PATTERN, 3]
        return true if success(!result.mail_servers.push(*mx_records(result.domain)).empty?)
        add_error(Truemail::Validate::Mx::ERROR)
        false
      end

      private

      def mx_records(domain)
        mx_records = Resolv::DNS.open { |dns| dns.getresources(domain, Resolv::DNS::Resource::IN::MX) }
        mx_records.sort_by(&:preference).map { |mx_record| mx_record.exchange.to_s }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
truemail-0.1.1.2 lib/truemail/validate/mx.rb
truemail-0.1.1 lib/truemail/validate/mx.rb
truemail-0.1.0 lib/truemail/validate/mx.rb