Class: Normailize::EmailAddress
- Inherits:
-
Object
- Object
- Normailize::EmailAddress
- 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
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#initialize(address) ⇒ EmailAddress
constructor
Public: Class initializer.
-
#lowercase ⇒ Object
Internal: Lowercase characthers in username part.
-
#normalized_address ⇒ Object
Public: Get normalized email address.
-
#provider ⇒ Object
Internal: Get provider instance for email address.
-
#remove_dots ⇒ Object
Internal: Remove all dots from username parts.
-
#remove_plus_part ⇒ Object
Internal: Removes everything after the first occurrence of a plus sign in the username parts.
-
#same_as?(address) ⇒ Boolean
Public: Determine if two email addresses are the same.
Constructor Details
#initialize(address) ⇒ EmailAddress
Public: Class initializer
address - An email address
Raises ArgumentError if email address does not have correct format
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
#address ⇒ Object (readonly)
Returns the value of attribute address.
18 19 20 |
# File 'lib/normailize/email_address.rb', line 18 def address @address end |
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
18 19 20 |
# File 'lib/normailize/email_address.rb', line 18 def domain @domain end |
#username ⇒ Object (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
#lowercase ⇒ Object
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_address ⇒ Object
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 |
#provider ⇒ Object
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_dots ⇒ Object
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_part ⇒ Object
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
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 |