This is a simple gem for Ruby that simplifies validating email addresses. It conforms with RFC2822.
Here comes a quick code sample. Currently no docs.
require 'rfc822' # To test if whole string is an email... ugly way # (returns 0 on success, nil on failure): "firstname.lastname@example.org" =~ RFC822::EMAIL_REGEXP_WHOLE # or nice one (returns true/false): "email@example.com".is_email? # To test if string contains an email... ugly way # (returns 0 on success, nil on failure): "demo firstname.lastname@example.orgFooBar" =~ RFC822::EMAIL_REGEXP_PART # or nice one (returns true/false): "demo email@example.comFooBar".contains_email? # To scan for emails within a string... ugly way # (returns an array): "something firstname.lastname@example.org xyz".scan(RFC822::EMAIL_REGEXP_PART) # or nice one (also returns an array): "something email@example.com xyz".scan_for_emails
Code was tested with ruby-1.8.7-p334 [ i386 ] and ruby-1.9.2-p180 [ i386 ] under RVM.
The gems are hosted at Rubygems.org. Make sure you're using the latest version of rubygems:
$ gem update --system
Then you can install the gem as follows:
$ gem install rfc822
Add to your Gemfile:
gem "rfc822", "~> 0.1.1"
and then type:
From the GitHub source
The source code is available at github.com/saepia/rfc822. You can either clone the git repository or download a tarball or zip file. Once you have the source, you can unpack it and use from wherever you downloaded.
Fixed description in the gem description
Added compatibility with Ruby 1.9
Added String#is_email?, String#contains_email? and String#scan_for_emails methods