https://github.com/dblock/canonical-emails
Combine email validation and transformations to produce canonical email addresses.
https://github.com/dblock/canonical-emails
Last synced: 9 months ago
JSON representation
Combine email validation and transformations to produce canonical email addresses.
- Host: GitHub
- URL: https://github.com/dblock/canonical-emails
- Owner: dblock
- License: mit
- Created: 2013-06-22T23:05:16.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2014-12-30T22:25:37.000Z (over 11 years ago)
- Last Synced: 2025-08-25T15:20:31.989Z (10 months ago)
- Language: Ruby
- Size: 172 KB
- Stars: 23
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
CanonicalEmails
==============
[](http://badge.fury.io/rb/canonical-emails)
[](https://travis-ci.org/dblock/canonical-emails)
[](https://gemnasium.com/dblock/canonical-emails)
[](https://codeclimate.com/github/dblock/canonical-emails)
Combine email validation and transformations to produce canonical email addresses. For example, parse and transform `Donald Duck ` into `donaldduck@gmail.com`, for `@gmail.com` addresses only. Patches instances of `Mail::Address`.
### Install
Add `canonical-emails` to your Gemfile.
```
gem 'canonical-emails'
```
### Use
``` ruby
class User
include CanonicalEmails::Extensions
attr_accessor :email
canonical_email :email, CanonicalEmails::GMail
end
user = User.new
user.email = "Donald Duck "
user.canonical_email.class # Mail::Address
user.canonical_email.to_s # "Donald Duck "
user.canonical_email.address # "donaldduck@gmail.com"
```
### Transform
#### CanonicalEmails::Downcase
Replaces the address and domain portion of the email by its lowercase equivalent.
``` ruby
email = CanonicalEmails::Downcase.transform("Donald Duck ")
email.to_s # "Donald Duck "
email.address # "donald.duck@example.com"
```
#### CanonicalEmails::GMail
Gmail.com e-mail addresses ignore periods and aren't case-sensitive. The canonical version removes them and changes the address portion to lowercase.
``` ruby
email = CanonicalEmails::GMail.transform("Donald Duck ")
email.to_s # "Donald Duck "
email.local # "donaldduck"
email.address # "donaldduck@gmail.com"
```
### Multiple Transformations
Combine multiple transformations, executed from left to right.
```
canonical_email :email, CanonicalEmails::GMail, CanonicalEmails::...
```
### Custom Transformations
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.
``` ruby
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.name # "kcuD dlanoD"
```
### Contribute
See [CONTRIBUTING](CONTRIBUTING.md).
### Copyright and License
Copyright Daniel Doubrovkine and Contributors, [Artsy](https://artsy.net), 2013-2014
[MIT License](LICENSE.md)