class Normailize::EmailAddress

Public: Class to represent an email address.

Normalizes email addresses according to the rules given by the provider.

Examples

# Compare two Gmail accounts
address1 = Normailize::EmailAddress.new('Jo.Hn+sneaky@gmail.com')
address2 = Normailize::EmailAddress.new('j.o.h.n@googlemail.com')
address1.same_as?(address2) # => true

# Get normalized email address
address = Normailize::EmailAddress.new('Jo.Hn+sneaky@gmail.com')
address.normalized_address # => john@gmail.com

Constants

EMAIL_ADDRESS_REGEX

Private: Simple regex to validate format of an email address

We’re deliberately ignoring a whole range of special and restricted chars for the sake of simplicity. This should match 99.99% of all the email addresses out there. If you allow comments (parentheses enclosed) in the local or domain part of your email addresses, make sure to strip them for normalization purposes. For ‘@’ in the local part to be allowed, split local and domain part at the last occurrence of the @-symbol.

Attributes

address[R]
domain[R]
username[R]

Public Class Methods

new(address) click to toggle source

Public: Class initializer

address - An email address

Raises ArgumentError if email address does not have correct format

# File lib/normailize/email_address.rb, line 35
def initialize(address)
  raise ArgumentError.new("Does not look like a valid email address") unless address =~ EMAIL_ADDRESS_REGEX
  @address = address
  @username, @domain = @address.split('@', 2)
  normalize!
end

Public Instance Methods

lowercase() click to toggle source

Internal: Lowercase characthers in username part

Returns nothing

# File lib/normailize/email_address.rb, line 59
def lowercase
  @username.downcase!
end
normalized_address() click to toggle source

Public: Get normalized email address

Returns normalized address according to the rules specified by the provider.

# File lib/normailize/email_address.rb, line 76
def normalized_address
  "#{@username}@#{@domain}"
end
provider() click to toggle source

Internal: Get provider instance for email address

If provider is known, it returns a specific provider instance, otherwise a generic provider instance is returned

Returns Normailize::Provider

# File lib/normailize/email_address.rb, line 69
def provider
  Provider.factory(@domain)
end
remove_dots() click to toggle source

Internal: Remove all dots from username parts

Returns nothing

# File lib/normailize/email_address.rb, line 45
def remove_dots
  @username.gsub!('.', '')
end
remove_plus_part() click to toggle source

Internal: Removes everything after the first occurrence of a plus sign in the username parts

Returns nothing

# File lib/normailize/email_address.rb, line 52
def remove_plus_part
  @username = @username.split('+', 2).first
end
same_as?(address) click to toggle source

Public: Determine if two email addresses are the same

Performs a comparison of the normalized username and provider

Returns true if same or false if not

# File lib/normailize/email_address.rb, line 85
def same_as?(address)
  (@username == address.username) && self.provider.same_as?(address.provider)
end

Private Instance Methods

normalize!() click to toggle source

Internal: Normalize email address according to rules specified by the provider

Returns nothing

# File lib/normailize/email_address.rb, line 94
def normalize!
  @domain.downcase!
  provider.modifications.each { |m| self.send(m) if self.respond_to?(m) }
end