class RichEmailValidator::EmailValidator

Validate email address

Attributes

email_regexp[W]
email[R]

Public Class Methods

email_regexp() click to toggle source
# File lib/rich_email_validator/email_validator.rb, line 13
def email_regexp
  @email_regexp || default_email_regexp
end
new(email) click to toggle source

Validates an email @param email [String]

# File lib/rich_email_validator/email_validator.rb, line 33
def initialize(email)
  @email = email
end
valid?(email) click to toggle source
# File lib/rich_email_validator/email_validator.rb, line 9
def valid?(email)
  new(email).valid?
end

Private Class Methods

default_email_regexp() click to toggle source
# File lib/rich_email_validator/email_validator.rb, line 19
def default_email_regexp
  @default_email_regexp ||=
    %r{
      \A[\w!#$%&'*+/=?`{|}~^-]+
      (?:\.[\w!#$%&'*+/=?`{|}~^-]+)*
      @(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\Z
    }ix
end

Public Instance Methods

valid?() click to toggle source

Validates an email @return [Boolean]

# File lib/rich_email_validator/email_validator.rb, line 39
def valid?
  @valid ||= valid_form? && valid_mx_record?
end
valid_form?() click to toggle source

Validates an email @return [Boolean]

# File lib/rich_email_validator/email_validator.rb, line 45
def valid_form?
  @valid_form ||= check_form
end
valid_mx_record?() click to toggle source

Validates an email @return [Boolean]

# File lib/rich_email_validator/email_validator.rb, line 51
def valid_mx_record?
  @validate_mx_record ||= check_mx_record
end

Private Instance Methods

check_form() click to toggle source
# File lib/rich_email_validator/email_validator.rb, line 57
def check_form
  (email =~ (self.class.email_regexp)) == 0
end
check_mx_record() click to toggle source
# File lib/rich_email_validator/email_validator.rb, line 61
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