Sha256: 8b54acb1ccb7e8d505df05b90f254a44b67e3c4d1dad01cd028a2d0aadac17db

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'resolv'

module RichEmailValidator
  # Validate email address
  class EmailValidator
    class << self
      attr_writer :email_regexp

      def valid?(email)
        new(email).valid?
      end

      def email_regexp
        @email_regexp || default_email_regexp
      end

      private

      def default_email_regexp
        @default_email_regexp ||=
          %r{
            \A[\w!#$%&'*+/=?`{|}~^-]+
            (?:\.[\w!#$%&'*+/=?`{|}~^-]+)*
            @(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\Z
          }ix
      end
    end

    attr_reader :email

    # Validates an email
    # @param email [String]
    def initialize(email)
      @email = email
    end

    # Validates an email
    # @return [Boolean]
    def valid?
      @valid ||= valid_form? && valid_mx_record?
    end

    # Validates an email
    # @return [Boolean]
    def valid_form?
      @valid_form ||= check_form
    end

    # Validates an email
    # @return [Boolean]
    def valid_mx_record?
      @validate_mx_record ||= check_mx_record
    end

    private

    def check_form
      (email =~ (self.class.email_regexp)) == 0
    end

    def check_mx_record
      domain = email.match(/\@(.+)/)[1]
      mx = 0
      Resolv::DNS.open do |dns|
        mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX) +
          dns.getresources(domain, Resolv::DNS::Resource::IN::A)
      end
      mx.size > 0 ? true : false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rich_email_validator-0.0.2 lib/rich_email_validator/email_validator.rb