Class: Normailize::EmailAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/normailize/email_address.rb

Overview

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('[email protected]')
address2 = Normailize::EmailAddress.new('[email protected]')
address1.same_as?(address2) # => true

# Get normalized email address
address = Normailize::EmailAddress.new('[email protected]')
address.normalized_address # => [email protected]

Constant Summary collapse

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.

/\A([a-z0-9_\-][a-z0-9_\-\+\.]{,62})?[a-z0-9_\-]@(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)+[a-z]{2,}\z/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ EmailAddress

Public: Class initializer

address - An email address

Raises ArgumentError if email address does not have correct format

Raises:

  • (ArgumentError)


35
36
37
38
39
40
# 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

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



18
19
20
# File 'lib/normailize/email_address.rb', line 18

def address
  @address
end

#domainObject (readonly)

Returns the value of attribute domain.



18
19
20
# File 'lib/normailize/email_address.rb', line 18

def domain
  @domain
end

#usernameObject (readonly)

Returns the value of attribute username.



18
19
20
# File 'lib/normailize/email_address.rb', line 18

def username
  @username
end

Instance Method Details

#lowercaseObject

Internal: Lowercase characthers in username part

Returns nothing



59
60
61
# File 'lib/normailize/email_address.rb', line 59

def lowercase
  @username.downcase!
end

#normalized_addressObject

Public: Get normalized email address

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



76
77
78
# File 'lib/normailize/email_address.rb', line 76

def normalized_address
  "#{@username}@#{@domain}"
end

#providerObject

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



69
70
71
# File 'lib/normailize/email_address.rb', line 69

def provider
  Provider.factory(@domain)
end

#remove_dotsObject

Internal: Remove all dots from username parts

Returns nothing



45
46
47
# File 'lib/normailize/email_address.rb', line 45

def remove_dots
  @username.gsub!('.', '')
end

#remove_plus_partObject

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

Returns nothing



52
53
54
# File 'lib/normailize/email_address.rb', line 52

def remove_plus_part
  @username = @username.split('+', 2).first
end

#same_as?(address) ⇒ Boolean

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

Returns:

  • (Boolean)


85
86
87
# File 'lib/normailize/email_address.rb', line 85

def same_as?(address)
  (@username == address.username) && self.provider.same_as?(address.provider)
end