Class: Mail::AddressList

Inherits:
Object show all
Defined in:
lib/mail/elements/address_list.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ AddressList

Mail::AddressList is the class that parses To, From and other address fields from emails passed into Mail.

AddressList provides a way to query the groups and mailbox lists of the passed in string.

It can supply all addresses in an array, or return each address as an address object.

Mail::AddressList requires a correctly formatted group or mailbox list per RFC2822 or RFC822. It also handles all obsolete versions in those RFCs.

list = '[email protected], My Group: [email protected], Bob <[email protected]>;'
a = AddressList.new(list)
a.addresses    #=> [#<Mail::Address:14943130 Address: |[email protected]...
a.group_names  #=> ["My Group"]


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mail/elements/address_list.rb', line 20

def initialize(string)
  if string.blank?
    @address_nodes = []
    return self
  end
  parser = Mail::AddressListsParser.new
  if tree = parser.parse(string)
    @address_nodes = tree.addresses
  else
    raise Mail::Field::ParseError.new(AddressListsParser, string, parser.failure_reason)
  end
end

Instance Method Details

#address_nodesObject

Returns a list of address syntax trees



56
57
58
# File 'lib/mail/elements/address_list.rb', line 56

def address_nodes # :nodoc:
  @address_nodes
end

#addressesObject

Returns a list of address objects from the parsed line



34
35
36
37
38
# File 'lib/mail/elements/address_list.rb', line 34

def addresses
  @addresses ||= get_addresses.map do |address_tree|
    Mail::Address.new(address_tree)
  end
end

#group_namesObject

Returns the names as an array of strings of all groups



51
52
53
# File 'lib/mail/elements/address_list.rb', line 51

def group_names # :nodoc:
  group_recipients.map { |g| g.group_name.text_value }
end

#group_recipientsObject

Returns a list of all recipient syntax trees that are part of a group



46
47
48
# File 'lib/mail/elements/address_list.rb', line 46

def group_recipients # :nodoc:
  @group_recipients ||= @address_nodes.select { |an| an.respond_to?(:group_name) }
end

#individual_recipientsObject

Returns a list of all recipient syntax trees that are not part of a group



41
42
43
# File 'lib/mail/elements/address_list.rb', line 41

def individual_recipients # :nodoc:
  @individual_recipients ||= @address_nodes - group_recipients
end