Class: Email::Valid

Inherits:
Object
  • Object
show all
Defined in:
lib/email/valid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Valid

Returns a new instance of Valid.



13
14
15
16
17
18
19
20
# File 'lib/email/valid.rb', line 13

def initialize(args)
  @errors = []
  # check mx, default on
  @check_domain = (args[:check_domain].nil? ? true : args[:check_domain])
  
  # check pattern, default on
  @check_rfc822 = (args[:check_rfc822].nil? ? true : args[:check_rfc822])
end

Instance Attribute Details

#check_domainObject

Returns the value of attribute check_domain.



3
4
5
# File 'lib/email/valid.rb', line 3

def check_domain
  @check_domain
end

#check_rfc822Object

Returns the value of attribute check_rfc822.



3
4
5
# File 'lib/email/valid.rb', line 3

def check_rfc822
  @check_rfc822
end

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/email/valid.rb', line 3

def errors
  @errors
end

Class Method Details

.address(email, args = {}) ⇒ Object

class method to check for email address validity



7
8
9
10
# File 'lib/email/valid.rb', line 7

def address(email, args = {})
  obj = self.new(args)
  obj.valid(email)
end

Instance Method Details

#valid(email) ⇒ Object

checks email address for validity



23
24
25
# File 'lib/email/valid.rb', line 23

def valid(email)
  valid_rfc822(email) && valid_domain(email)
end

#valid_domain(email) ⇒ Object

check for valid mx record



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/email/valid.rb', line 39

def valid_domain(email)
  return true unless check_domain == true
  domain = email.match(/\@(.+)/)[1]
  Resolv::DNS.open do |dns|
      @mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
  end
  if @mx.size > 0 
    true
  else
    errors << "Invalid Domain (no mx record)"
    false
  end
end

#valid_rfc822(email) ⇒ Object

checks against the rfc822 pattern



28
29
30
31
32
33
34
35
36
# File 'lib/email/valid.rb', line 28

def valid_rfc822(email)
  return true unless check_rfc822 == true
  if email =~ rfc822
    true
  else
    errors << "Does not conform to rfc822"
    false
  end
end