Skip to content

Instantly share code, notes, and snippets.

@wmorgan
Created July 23, 2014 23:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wmorgan/cd5a17616a864fed6108 to your computer and use it in GitHub Desktop.
Save wmorgan/cd5a17616a864fed6108 to your computer and use it in GitHub Desktop.
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