Top Level Namespace

Defined Under Namespace

Modules: Addressable, IMW Classes: Array, Hash, String, Symbol, UUID

Instance Method Summary collapse

Instance Method Details

#is_domain?(domain) ⇒ Boolean

Return true if domain is a valid domain name

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/imw/utils/validate.rb', line 24

def is_domain?(domain)
  raise ArgumentError, "'domain' must be a string" if domain.class != String
  return false if domain.empty?

  return false if domain.size > 255 # max number of characters in a domain
  return false if not domain =~ /^[a-zA-Z0-9.\-]+$/ # allowed characters
  parts = domain.split('.')
  return false if parts.size > 127 # max number of subdomains
  parts.all? {|part| return false if part.size > 63} # max number of characters in a subdomain
  
  return true
end

#is_email?(email) ⇒ Boolean

Return true if email is a valid email address

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/imw/utils/validate.rb', line 4

def is_email?(email)
  raise ArgumentError, "'email' must be a string" if email.class != String
  return false if email.empty?

  parts = email.split('@')
  return false if parts.size != 2
  
  local = parts.first
  return false if not local =~ /[a-zA-Z0-9_~=+-.]*/ # allowed characters
  return false if local[0,1] == '.' # starts with .
  return false if local[-1,1] == '.' # end with .
  return false if local.include?('..') # can't repeat .

  domain = parts.last
  return false if not is_domain?(domain)

  return true
end