xHire-validates_as_email 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ 0.6.0 - Jul 24th 2009
2
+ * Compatibility with Ruby 1.9
3
+ * Support for I18n
4
+
5
+ 0.5.1 - Aug 15th 2008
6
+ * Repackaged original plugin by Ximon Eighteen for GitHub
7
+ * Gem'ified plugin
8
+
9
+ 0.1.4 - May 26th 2006
10
+ * Thijs van der Vossen fixed a plugin load issue in the test suite and
11
+ provided another test to illustrate that test@example is considered
12
+ valid by RFC822, something most people wouldn't realise.
13
+
14
+ 0.1.3 - May 9th 2006
15
+ * Thijs van der Vossen contributed some test cases.
16
+
17
+ 0.1.2 - April 24th 2006
18
+ * Dan Kubb and Tim Fletcher have updated the RFC822 regular expression
19
+ on Tims website so I've put the latest version into this plugin. The
20
+ changes prevent matching of email addresses containing line breaks.
21
+
22
+ 0.1.1 - April 23rd 2006
23
+ * Huge code cleanup & simplification by Dan Kubb.
24
+ * Credit Tim Fletcher with the Ruby version of the RFC822 regular
25
+ expression.
26
+
27
+ 0.1.0 - April 4th 2006
28
+ * First release, advertised on the Rails plugins page:
29
+ http://wiki.rubyonrails.org/rails/pages/Plugins
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Licensing terms follow:
2
+
3
+ This software is placed under the Creative Commons Attribution-Share Alike 3.0 Unported License
4
+
5
+ http://creativecommons.org/licenses/by-sa/3.0/
data/README ADDED
@@ -0,0 +1,48 @@
1
+ ValidatesAsEmail
2
+ ================
3
+
4
+ This gem/plugin is a re-packaged and gem'ified version of the original plugin with credit as follows:
5
+
6
+ Michal Zima <xhire@mujmalysvet.cz>
7
+ Ximon Eighteen <ximon.eighteen@int.greenpeace.org>
8
+ Dan Kubb <dan.kubb@autopilotmarketing.com>
9
+ Thijs van der Vossen <thijs@fngtps.com>
10
+
11
+ This Ruby on Rails plugin implements an ActiveRecord validation helper called
12
+ validates_as_email. The helper acts as if validates_format_of was used with a
13
+ regular expression that defines an RFC822 email address conformance test.
14
+
15
+ The plugin implements the regular expression here:
16
+
17
+ http://tfletcher.com/lib/rfc822.rb
18
+
19
+ Which is an implementation in Ruby of a regular expression published by Cal
20
+ Henderson for PHP here:
21
+
22
+ http://www.iamcal.com/publish/articles/php/parsing_email
23
+
24
+ Installation:
25
+ =============
26
+ gem sources -a http://gems.github.com
27
+
28
+ Install the gem(s):
29
+ sudo gem install xHire-validates_as_email
30
+
31
+ Add to environment.rb initializer block:
32
+ config.gem 'xHire-validates_as_email', :lib => 'validates_as_email', :source => 'http://gems.github.com'
33
+
34
+ Usage:
35
+ ======
36
+ In your model file do something like:
37
+
38
+ class MyClass < ActiveRecord::Base
39
+ validates_as_emaile :email, :message => 'Invalid Email Address', :allow_nil => true
40
+ end
41
+
42
+ Tests:
43
+ ======
44
+ Some tests have been added.
45
+
46
+ License:
47
+ ========
48
+ See the LICENSE file.
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the validates_as_email plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the validates_as_email plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'ValidatesAsEmail'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_as_email'
@@ -0,0 +1,69 @@
1
+ # encoding: utf-8
2
+
3
+ #
4
+ # RFC822 Email Address Regex
5
+ # --------------------------
6
+ #
7
+ # Originally written by Cal Henderson
8
+ # c.f. http://iamcal.com/publish/articles/php/parsing_email/
9
+ #
10
+ # Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
11
+ #
12
+ # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
13
+ # http://creativecommons.org/licenses/by-sa/2.5/
14
+ #
15
+ module RFC822
16
+ module Patterns
17
+ def self.compile(string)
18
+ Regexp.new string, nil, 'n'
19
+ end
20
+
21
+ QTEXT = compile "[^\\x0d\\x22\\x5c\\x80-\\xff]"
22
+ DTEXT = compile "[^\\x0d\\x5b-\\x5d\\x80-\\xff]"
23
+
24
+ ATOM_CORE = compile "[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+"
25
+ ATOM_EDGE = compile "[^\\x00-\\x20\\x22\\x28\\x29\\x2c-\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]"
26
+ ATOM = compile "(?:#{ATOM_EDGE}{1,2}|#{ATOM_EDGE}#{ATOM_CORE}#{ATOM_EDGE})"
27
+
28
+ QPAIR = compile "\\x5c[\\x00-\\x7f]"
29
+ QSTRING = compile "\\x22(?:#{QTEXT}|#{QPAIR})*\\x22"
30
+
31
+ WORD = compile "(?:#{ATOM}|#{QSTRING})"
32
+
33
+ DOMAIN_PT = compile "(?:[a-zA-Z0-9][\-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z0-9]+)"
34
+
35
+ DOMAIN = compile "#{DOMAIN_PT}(?:\\x2e#{DOMAIN_PT})*"
36
+ LOCAL_PT = compile "#{WORD}(?:\\x2e#{WORD})*"
37
+ ADDRESS = compile "#{LOCAL_PT}\\x40#{DOMAIN}"
38
+ end
39
+ EmailAddress = /\A#{Patterns::ADDRESS}\z/
40
+ end
41
+
42
+ # Validation helper for ActiveRecord derived objects that cleanly and simply
43
+ # allows the model to check if the given string is a syntactically valid email
44
+ # address (by using the RFC822 module above).
45
+ #
46
+ # Original code by Ximon Eighteen <ximon.eightee@int.greenpeace.org> which was
47
+ # heavily based on code I can no longer find on the net, my apologies to the
48
+ # author!
49
+ #
50
+ # Huge credit goes to Dan Kubb <dan.kubb@autopilotmarketing.com> for
51
+ # submitting a patch to massively simplify this code and thereby instruct me
52
+ # in the ways of Rails too! I reflowed the patch a little to keep the line
53
+ # length to a maximum of 78 characters, an old habit.
54
+
55
+ module ActiveRecord
56
+ module Validations
57
+ module ClassMethods
58
+ def validates_as_email(*attr_names)
59
+ configuration = {
60
+ :message => (I18n.translate(:'activerecord.errors.messages.invalid_email', :raise => true) rescue 'is an invalid email'),
61
+ :with => RFC822::EmailAddress,
62
+ :allow_nil => false }
63
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
64
+
65
+ validates_format_of attr_names, configuration
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,44 @@
1
+ require 'test/unit'
2
+
3
+ begin
4
+ require File.dirname(__FILE__) + '/../../../../config/boot'
5
+ require 'active_record'
6
+ require 'validates_as_email'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'activerecord'
10
+ require File.dirname(__FILE__) + '/../lib/validates_as_email'
11
+ end
12
+
13
+ class TestRecord < ActiveRecord::Base
14
+ def self.columns; []; end
15
+ attr_accessor :email
16
+ validates_as_email :email
17
+ end
18
+
19
+ class ValidatesAsEmailTest < Test::Unit::TestCase
20
+ def test_illegal_rfc822_email_address
21
+ addresses = [
22
+ 'Max@Job 3:14',
23
+ 'Job@Book of Job',
24
+ 'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
25
+ ]
26
+ addresses.each do |address|
27
+ assert !TestRecord.new(:email => address).valid?, "#{address} should be illegal."
28
+ end
29
+ end
30
+
31
+ def test_legal_rfc822_email_address
32
+ addresses = [
33
+ 'test@example',
34
+ 'test@example.com',
35
+ 'test@example.co.uk',
36
+ '"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
37
+ 'me@[187.223.45.119]',
38
+ 'someone@123.com',
39
+ ]
40
+ addresses.each do |address|
41
+ assert TestRecord.new(:email => address).valid?, "#{address} should be legal."
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xHire-validates_as_email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Michal Zima
8
+ - Ximon Eighteen
9
+ - Dan Kubb
10
+ - Thijs van der Vossen
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+
15
+ date: 2009-07-24 00:00:00 -07:00
16
+ default_executable:
17
+ dependencies: []
18
+
19
+ description: Rails gem/plugin that implements an ActiveRecord validation helper called validates_as_email which validates email address (RFC822)
20
+ email: xhire@mujmalysvet.cz
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files: []
26
+
27
+ files:
28
+ - CHANGELOG
29
+ - LICENSE
30
+ - README
31
+ - Rakefile
32
+ - init.rb
33
+ - lib/validates_as_email.rb
34
+ - test/validates_as_email_test.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/xHire/validates_as_email
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Rails gem/plugin to validate format of email addresses (RFC822)
61
+ test_files:
62
+ - test/validates_as_email_test.rb