Created
July 23, 2014 23:36
-
-
Save wmorgan/cd5a17616a864fed6108 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Address | |
def initialize address, display_name, domain | |
@address = address | |
@display_name = display_name | |
@domain = domain | |
end | |
attr_reader :address, :display_name, :domain | |
def self.from_header h # returns an Array of Addresses | |
parse_multi_address h | |
end | |
private | |
def self.parse_multi_address string # splits address lines into multiple emails | |
return [] if string.nil? || string !~ /\S/ | |
emails = string.gsub(/[\t\r\n]+/, " ").split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/) | |
emails.map { |e| parse_address e }.compact | |
end | |
def self.parse_address string # ripped from sup | |
return if string.nil? || string.empty? | |
name, email, domain = case string | |
when /^(["'])(.*?[^\\])\1\s*<(\S+?@(\S+?))>/ | |
a, b, c = $2, $3, $4 | |
a = a.gsub(/\\(["'])/, '\1') | |
[a, b, c] | |
when /(.+?)\s*<(\S+?@(\S+?))>/ | |
[$1, $2, $3] | |
when /<(\S+?@(\S+?))>/ | |
[nil, $1, $2] | |
when /(\S+?@(\S+))/ | |
[nil, $1, $2] | |
else | |
[nil, string, nil] # i guess... | |
end | |
Address.new email, name, domain | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment