Module: BarkestCore::EmailTester

Included in:
ContactMessage, User
Defined in:
lib/barkest_core/concerns/email_tester.rb

Overview

Adds helper methods to the model to allow verifying email addresses.

Constant Summary collapse

VALID_EMAIL_REGEX =

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.

/\A(?<USER>[\w+\-.]+)@(?<DOMAIN>[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+)\z/i

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/barkest_core/concerns/email_tester.rb', line 30

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

.valid_email?(email, check_dns = false) ⇒ Boolean

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.

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'lib/barkest_core/concerns/email_tester.rb', line 20

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