Class: EmailList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/email_list.rb,
lib/email_list/version.rb

Constant Summary collapse

EMAIL_ADDRESS_QTEXT =
Regexp.new '[^\\x0d\\x22\\x5c\\x80-\\xff]', nil, 'n'
EMAIL_ADDRESS_DTEXT =
Regexp.new '[^\\x0d\\x5b-\\x5d\\x80-\\xff]', nil, 'n'
EMAIL_ADDRESS_ATOM =
Regexp.new '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+', nil, 'n'
EMAIL_ADDRESS_QUOTED_PAIR =
Regexp.new '\\x5c[\\x00-\\x7f]', nil, 'n'
EMAIL_ADDRESS_DOMAIN_LITERAL =
Regexp.new "\\x5b(?:#{EMAIL_ADDRESS_DTEXT}|#{EMAIL_ADDRESS_QUOTED_PAIR})*\\x5d", nil, 'n'
EMAIL_ADDRESS_QUOTED_STRING =
Regexp.new "\\x22(?:#{EMAIL_ADDRESS_QTEXT}|#{EMAIL_ADDRESS_QUOTED_PAIR})*\\x22", nil, 'n'
EMAIL_ADDRESS_DOMAIN_REF =
EMAIL_ADDRESS_ATOM
EMAIL_ADDRESS_SUB_DOMAIN =
"(?:#{EMAIL_ADDRESS_DOMAIN_REF}|#{EMAIL_ADDRESS_DOMAIN_LITERAL})"
EMAIL_ADDRESS_WORD =
"(?:#{EMAIL_ADDRESS_ATOM}|#{EMAIL_ADDRESS_QUOTED_STRING})"
EMAIL_ADDRESS_DOMAIN =
"#{EMAIL_ADDRESS_SUB_DOMAIN}(?:\\x2e#{EMAIL_ADDRESS_SUB_DOMAIN})*"
EMAIL_ADDRESS_LOCAL_PART =
"#{EMAIL_ADDRESS_WORD}(?:\\x2e#{EMAIL_ADDRESS_WORD})*"
EMAIL_ADDRESS_SPEC =
"#{EMAIL_ADDRESS_LOCAL_PART}\\x40#{EMAIL_ADDRESS_DOMAIN}"
EMAIL_ADDRESS_INNER_PATTERN =
Regexp.new "#{EMAIL_ADDRESS_SPEC}", nil, 'n'
EMAIL_ADDRESS_EXACT_PATTERN =
Regexp.new "\\A#{EMAIL_ADDRESS_SPEC}\\z", nil, 'n'
VERSION =
"0.0.5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email_list) ⇒ EmailList

Returns a new instance of EmailList.



32
33
34
35
36
37
38
39
40
41
# File 'lib/email_list.rb', line 32

def initialize(email_list)
  case email_list
  when String
    @emails = email_list.split(/,|;\s?/).map &:strip
  when Array
    @emails = email_list
  when NilClass
    @emails = []
  end
end

Instance Attribute Details

#emailsObject (readonly)

Returns the value of attribute emails.



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

def emails
  @emails
end

Class Method Details

.split(emails) ⇒ Object



24
25
26
# File 'lib/email_list.rb', line 24

def self.split(emails)
  new(emails).emails
end

.valid?(emails) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/email_list.rb', line 20

def self.valid?(emails)
  new(emails).valid?
end

Instance Method Details

#+(other) ⇒ Object



66
67
68
# File 'lib/email_list.rb', line 66

def +(other)
  EmailList.new emails + other
end

#==(other) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/email_list.rb', line 55

def ==(other)
  case other
  when String
    emails.sort == EmailList.split(other).sort
  when Array
    emails == other
  when EmailList
    emails.sort == other.emails.sort
  end
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/email_list.rb', line 43

def each(&block)
  @emails.each(&block)
end

#sizeObject



47
48
49
# File 'lib/email_list.rb', line 47

def size
  @emails.size
end

#to_sObject



51
52
53
# File 'lib/email_list.rb', line 51

def to_s
  @emails.join(", ")
end

#valid?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# File 'lib/email_list.rb', line 70

def valid?
  emails.each do |email|
    unless email =~ EMAIL_ADDRESS_EXACT_PATTERN
      return false
    end
  end

  true
end