Module: EmailAddressValidator

Defined in:
lib/email_address_validator.rb,
lib/email_address_validator/version.rb

Defined Under Namespace

Classes: DomainParser, RFC2822Parser

Constant Summary collapse

LIBPATH =

:stopdoc:

::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
PATH =
::File.dirname(LIBPATH) + ::File::SEPARATOR
VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.libpath(*args) ⇒ Object

Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/email_address_validator.rb', line 12

def self.libpath( *args )
  rv =  args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
  if block_given?
    begin
      $LOAD_PATH.unshift LIBPATH
      rv = yield
    ensure
      $LOAD_PATH.shift
    end
  end
  return rv
end

.path(*args) ⇒ Object

Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/email_address_validator.rb', line 29

def self.path( *args )
  rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
  if block_given?
    begin
      $LOAD_PATH.unshift PATH
      rv = yield
    ensure
      $LOAD_PATH.shift
    end
  end
  return rv
end

.require_all_libs_relative_to(fname, dir = nil) ⇒ Object

Utility method used to require all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.



47
48
49
50
51
52
53
# File 'lib/email_address_validator.rb', line 47

def self.require_all_libs_relative_to( fname, dir = nil )
  dir ||= ::File.basename(fname, '.*')
  search_me = ::File.expand_path(
      ::File.join(::File.dirname(fname), dir, '**', '*.rb'))

  Dir.glob(search_me).sort.each {|rb| require rb}
end

.validate(addr, validate_domain = false) ⇒ Object

Shorthand for +EmailAddressParser.validate_2822



69
# File 'lib/email_address_validator.rb', line 69

def self.validate(addr, validate_domain=false); self.validate_2822; end

.validate_2822(addr, validate_domain = false) ⇒ Object

Validates an email address according to RFC 2822 This validates addresses against the full spec, which may not be what you want.



74
75
76
77
78
# File 'lib/email_address_validator.rb', line 74

def self.validate_2822(addr, validate_domain=false)
  parser = RFC2822Parser.new(addr)
  parser.validate_domain = validate_domain
  parser.parse
end

.validate_2822_addr(addr, validate_domain = false) ⇒ Object

Validates addr against the addr_spec portion of RFC 2822. This is what most people actually want out of an email validator You very well may want to set validate_domain to true as well, as RFC2822 doesn’t explicitly require valid domains



62
63
64
65
66
# File 'lib/email_address_validator.rb', line 62

def self.validate_2822_addr(addr, validate_domain=false)
  parser = RFC2822Parser.new(addr, "only_addr_spec")
  parser.validate_domain = validate_domain
  parser.parse
end

.validate_822(addr, validate_domain = false) ⇒ Object

Validates legacy address according to RFC 822, the original email grammar.



82
83
84
85
86
# File 'lib/email_address_validator.rb', line 82

def self.validate_822(addr, validate_domain=false)
  parser = RFC822Parser.new(addr)
  parser.validate_domain = validate_domain
  parser.parse
end

.validate_822_addr(addr, validate_domain = false) ⇒ Object

Validates only the addr_spec portion an address according to RFC 822



89
90
91
92
93
# File 'lib/email_address_validator.rb', line 89

def self.validate_822_addr(addr, validate_domain=false)
  parser = RFC822Parser.new(addr, "only_addr_spec")
  parser.validate_domain = validate_domain
  parser.parse
end

.validate_addr(addr, validate_domain = false) ⇒ Object

Shorthand for +EmailAddressParser.validate_2822_addr



56
# File 'lib/email_address_validator.rb', line 56

def self.validate_addr(addr, validate_domain=false); self.validate_2822; end

.validate_domain(domain) ⇒ Object

Validates a domain name



96
97
98
99
# File 'lib/email_address_validator.rb', line 96

def self.validate_domain(domain)
  parser = DomainParser.new(addr)
  parser.parse
end