Sha256: ca0a0f32ed92d9961817043d753f01f74f58c157e5f887258fcbc12b0e51b580

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'resolv'

module BarkestCore

  ##
  # Adds helper methods to the model to allow verifying email addresses.
  module EmailTester

    ##
    # A regex that can be used to verify most email addresses.
    #
    # When used, the match will include a USER and DOMAIN element to represent the broken down email address.
    VALID_EMAIL_REGEX = /\A(?<USER>[\w+\-.]+)@(?<DOMAIN>[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+)\z/i


    ##
    # Validates the supplied email address against the VALID_EMAIL_REGEX.
    #
    # The +check_dns+ option ensures that an MX record can be found for the email address.
    def self.valid_email?(email, check_dns = false)
      match = VALID_EMAIL_REGEX.match(email)
      return false unless match
      if check_dns
        return false if Resolv::DNS.open{ |dns| dns.getresources match['DOMAIN'], Resolv::DNS::Resource::IN::MX }.blank?
      end
      true
    end

    # :nodoc:
    def self.included(base)
      base.class_eval do
        ##
        # Validates the supplied email address against the VALID_EMAIL_REGEX.
        #
        # The +check_dns+ option ensures that an MX record can be found for the email address.
        def self.valid_email?(email, check_dns = false)
          BarkestCore::EmailTester.valid_email? email, check_dns
        end

      end
    end

    protected

    ##
    # Validates the supplied email address against the VALID_EMAIL_REGEX.
    #
    # The +check_dns+ option ensures that an MX record can be found for the email address.
    def valid_email?(email, check_dns = false)
      BarkestCore::EmailTester.valid_email? email, check_dns
    end


  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
barkest_core-1.5.4.0 lib/barkest_core/concerns/email_tester.rb
barkest_core-1.5.3.0 lib/barkest_core/concerns/email_tester.rb