Rails gem to easily parse user name from email address.
Add this line to your application's Gemfile:
gem 'parse_name_from_email'
And then execute:
$ bundle
Or install it yourself as:
$ gem install parse_name_from_email
You don't need to configure anything, but if you want to customize the behaviour, use the following snippet:
ParseNameFromEmail.configure do |config|
# split email address with regexp
config.regexp = /(?=[A-Z])|(?:([0-9]+))|\.|-|\?|!|\+|\;|\_/
## Recognizing plus parts in gmail addresses
#
# DEFAULT: true
#
# if TRUE:
# email address: '[email protected]'
# result name: 'Example (Something 123)'
#
# if FALSE:
# email address: '[email protected]'
# result name: 'Example'
config.friendly_plus_part = true
end
Values in the above snippet are the default values.
# getting email address
ParseNameFromEmail.get_email_name('[email protected]') # => 'john-snow'
ParseNameFromEmail.get_email_name('[email protected]') # => 'john-snow+nickname'
# parsing name from email address
ParseNameFromEmail.parse_name_from('[email protected]') # => 'John Snow'
ParseNameFromEmail.parse_name_from('[email protected]') # => 'John Snow'
ParseNameFromEmail.parse_name_from('[email protected]') # => 'John Snow'
ParseNameFromEmail.parse_name_from('[email protected]') # => 'John Snow'
ParseNameFromEmail.parse_name_from('John Snow <[email protected]>') # => 'John Snow'
# validating RFC format of email
ParseNameFromEmail.valid_rfc_format?('[email protected]') # => false
ParseNameFromEmail.valid_rfc_format?('John Snow <[email protected]>') # => true
# if config.friendly_plus_part = true
ParseNameFromEmail.parse_name_from('[email protected]') # => 'John Snow (Nickname 123)'
# if config.friendly_plus_part = false
ParseNameFromEmail.parse_name_from('[email protected]') # => 'John Snow'
# batches
string_with_emails = 'John Snow <[email protected]>, [email protected]'
ParseNameFromEmail.parse_names_from(string_with_emails) # => ['John Snow', 'Alice']
string_with_emails = '[email protected], [email protected]'
ParseNameFromEmail.parse_names_from(string_with_emails) # => ['Lily (black)', 'Alice']
# advanced parsing
string_with_emails = '[email protected], [email protected]'
ParseNameFromEmail.parse_emails_with_names_from(string_with_emails)
# => {'[email protected]' => 'John Snow', '[email protected]' => 'Lily (black)'}
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/Applifting/parse_name_from_email.
The gem is available as open source under the terms of the MIT License.