Combine email validation and transformations to produce canonical email addresses.
MIT License
Combine email validation and transformations to produce canonical email addresses. For example, parse and transform Donald Duck <[email protected]>
into [email protected]
, for @gmail.com
addresses only. Patches instances of Mail::Address
.
Add canonical-emails
to your Gemfile.
gem 'canonical-emails'
class User
include CanonicalEmails::Extensions
attr_accessor :email
canonical_email :email, CanonicalEmails::GMail
end
user = User.new
user.email = "Donald Duck <[email protected]>"
user.canonical_email.class # Mail::Address
user.canonical_email.to_s # "Donald Duck <[email protected]>"
user.canonical_email.address # "[email protected]"
Replaces the address and domain portion of the email by its lowercase equivalent.
email = CanonicalEmails::Downcase.transform("Donald Duck <[email protected]>")
email.to_s # "Donald Duck <[email protected]>"
email.address # "[email protected]"
Gmail.com e-mail addresses ignore periods and aren't case-sensitive. The canonical version removes them and changes the address portion to lowercase.
email = CanonicalEmails::GMail.transform("Donald Duck <[email protected]>")
email.to_s # "Donald Duck <[email protected]>"
email.local # "donaldduck"
email.address # "[email protected]"
Combine multiple transformations, executed from left to right.
canonical_email :email, CanonicalEmails::GMail, CanonicalEmails::...
A transformation is a module that has a single transform
method that returns a Mail::Address
instance. This library patches internal methods of the Mail::Address
class.
module CanonicalEmails
module ReverseName
def self.transform(value)
Mail::Address.new(value).tap do |email|
email.instance_eval do
def name
super.reverse
end
end
end
end
end
end
email = CanonicalEmails::ReverseName.transform("Donald Duck <[email protected]>")
email.name # "kcuD dlanoD"
See CONTRIBUTING.
Copyright Daniel Doubrovkine and Contributors, Artsy, 2013-2014