Module: EmailListField::StaticHelpers

Included in:
EmailListField
Defined in:
lib/email_list_field/static_helpers.rb

Constant Summary collapse

EMAIL_EXTRACTION_REGEXP =
/^([^<>]*)<(.+)>$/

Instance Method Summary collapse

Instance Method Details

#parse_email_list_params(emails, options = {}) ⇒ Object

Inputs

> emails: an array of strings, where each string may be either an email or a “Name <email>” string

> options:

:lower_email_address_case [true|false] default=true

Output:

> an array of pairs [name, email], where name might be nil



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/email_list_field/static_helpers.rb', line 12

def parse_email_list_params(emails, options={})
  return [] unless emails

  lower_email_address_case = (!options[:lower_email_address_case].nil? ? options[:lower_email_address_case] : true)

  emails.map do |formatted_email|
    stripped_formatted_email = formatted_email.strip

    if matchdata = stripped_formatted_email.match(EmailListField::StaticHelpers::EMAIL_EXTRACTION_REGEXP)
      parsed_email = [matchdata[1].strip, matchdata[2].strip]
    else
      parsed_email = [nil, stripped_formatted_email]
    end

    if lower_email_address_case
      parsed_email[1].downcase!
    end

    parsed_email
  end
end