Class: KawaiiEmailAddress::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/kawaii_email_address/validator.rb

Constant Summary collapse

LocalPartChars =
((?A..?Z).to_a + (?a..?z).to_a + (0..9).to_a).join + %q[!#$%&'*-/=?+-^_`{|}~]
LocalPartQuotedStringChars =
%q[()<>[]:;@,.] << ' '
PERIOD =
'.'
BACKSLASH =
'\\'
DOUBLE_QUOTE =
'"'
DOMAIN_LITERAL_RE =
/\A\[(?<ip_addr>.+)\]\z/

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Validator

Returns a new instance of Validator.



22
23
24
# File 'lib/kawaii_email_address/validator.rb', line 22

def initialize(address)
  @domain_part, @local_part = address.reverse.split('@', 2).map(&:reverse)
end

Class Attribute Details

.period_restriction_violate_domainsObject

Returns the value of attribute period_restriction_violate_domains.



17
18
19
# File 'lib/kawaii_email_address/validator.rb', line 17

def period_restriction_violate_domains
  @period_restriction_violate_domains
end

Instance Attribute Details

#domain_partObject (readonly)

Returns the value of attribute domain_part.



14
15
16
# File 'lib/kawaii_email_address/validator.rb', line 14

def domain_part
  @domain_part
end

#local_partObject (readonly)

Returns the value of attribute local_part.



14
15
16
# File 'lib/kawaii_email_address/validator.rb', line 14

def local_part
  @local_part
end

Instance Method Details

#both_part_present?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/kawaii_email_address/validator.rb', line 34

def both_part_present?
  local_part && domain_part
end

#to_sObject



30
31
32
# File 'lib/kawaii_email_address/validator.rb', line 30

def to_s
  [local_part, domain_part].join('@')
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/kawaii_email_address/validator.rb', line 26

def valid?
  both_part_present? && valid_local_part? && valid_domain_part?
end

#valid_domain_part?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/kawaii_email_address/validator.rb', line 78

def valid_domain_part?
  if match = DOMAIN_LITERAL_RE.match(domain_part)
    valid_domain_literal?(match[:ip_addr])
  else
    valid_fqdn_domain_part?
  end
end

#valid_local_part?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kawaii_email_address/validator.rb', line 38

def valid_local_part?
  return false if invalid_period_position?

  in_quoted_pair    = false
  in_quoted_string  = false

  is_valid = local_part.chars.each.with_index.all? do |char, i|
    # accept anything if it's got a backslash before it
    if in_quoted_pair
      in_quoted_pair = false
      next true
    end

    case char
    when BACKSLASH # backslash signifies the start of a quoted pair
      # must be in quoted string per http://www.rfc-editor.org/errata_search.php?rfc=3696
      if (i < local_part.length - 1) && in_quoted_string
        in_quoted_pair = true
        next true
      end

    when DOUBLE_QUOTE # double quote delimits quoted strings
      in_quoted_string = !in_quoted_string
      next true

    when PERIOD
      period_restriction_violate_domain? ||
      in_quoted_string ||
      local_part[i + 1] != PERIOD

    else
      LocalPartChars.include?(char) || \
      (in_quoted_string && LocalPartQuotedStringChars.include?(char))

    end
  end

  is_valid && !in_quoted_string
end