jcnetdev-validates_as_email_address 1.0.20080704
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +9 -0
- data/LICENSE +2 -0
- data/README +51 -0
- data/Rakefile +79 -0
- data/lib/validates_as_email_address/rfc_822.rb +18 -0
- data/lib/validates_as_email_address.rb +85 -0
- data/rails/init.rb +1 -0
- data/test/app_root/app/models/user.rb +3 -0
- data/test/app_root/db/migrate/001_create_users.rb +11 -0
- data/test/app_root/log/in_memory.log +39 -0
- data/test/test_helper.rb +7 -0
- data/test/unit/validates_as_email_address_test.rb +29 -0
- metadata +71 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
data/README
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= validates_as_email_address
|
2
|
+
|
3
|
+
+validates_as_email_address+ adds support for validating the format/length of
|
4
|
+
email addresses.
|
5
|
+
|
6
|
+
== Resources
|
7
|
+
|
8
|
+
Wiki
|
9
|
+
|
10
|
+
* http://wiki.pluginaweek.org/Validates_as_email_address
|
11
|
+
|
12
|
+
API
|
13
|
+
|
14
|
+
* http://api.pluginaweek.org/validates_as_email_address
|
15
|
+
|
16
|
+
Development
|
17
|
+
|
18
|
+
* http://dev.pluginaweek.org/browser/trunk/validates_as_email_address
|
19
|
+
|
20
|
+
Source
|
21
|
+
|
22
|
+
* http://svn.pluginaweek.org/trunk/validates_as_email_address
|
23
|
+
|
24
|
+
== Description
|
25
|
+
|
26
|
+
Consistently reliable email address validations are difficult to find and hard
|
27
|
+
to choose as there are far too many implementations in various programming
|
28
|
+
languages. This plugins builds on Thijs van der Vossen's validates_as_email
|
29
|
+
by adding advanced validation option support and also validating the length of
|
30
|
+
the email address.
|
31
|
+
|
32
|
+
== Usage
|
33
|
+
|
34
|
+
=== Example
|
35
|
+
|
36
|
+
class Person < ActiveRecord::Base
|
37
|
+
validates_as_email_address :email, :on => :create
|
38
|
+
end
|
39
|
+
|
40
|
+
== Testing
|
41
|
+
|
42
|
+
Before you can run any tests, the following gem must be installed:
|
43
|
+
* plugin_test_helper[http://wiki.pluginaweek.org/Plugin_test_helper]
|
44
|
+
|
45
|
+
== References
|
46
|
+
|
47
|
+
* Cal Henderson - {Parsing Email Adresses in PHP}[http://iamcal.com/publish/articles/php/parsing_email]
|
48
|
+
* Tim Fletcher - {Ruby Translation}[http://tfletcher.com/lib/rfc822.rb]
|
49
|
+
* Dan Kubb[dan.kubb@autopilotmarketing.com]
|
50
|
+
* Ximon Eighteen[ximon.eightee@int.greenpeace.org]
|
51
|
+
* Thijs van der Vossen - validates_as_email[https://svn.greenpeace.org/repositories/rails_plugins/validates_as_email]
|
data/Rakefile
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require 'rake/contrib/sshpublisher'
|
5
|
+
|
6
|
+
PKG_NAME = 'validates_as_email_address'
|
7
|
+
PKG_VERSION = '0.0.2'
|
8
|
+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
9
|
+
RUBY_FORGE_PROJECT = 'pluginaweek'
|
10
|
+
|
11
|
+
desc 'Default: run unit tests.'
|
12
|
+
task :default => :test
|
13
|
+
|
14
|
+
desc 'Test the validates_as_email_address plugin.'
|
15
|
+
Rake::TestTask.new(:test) do |t|
|
16
|
+
t.libs << 'lib'
|
17
|
+
t.pattern = 'test/**/*_test.rb'
|
18
|
+
t.verbose = true
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Generate documentation for the validates_as_email_address plugin.'
|
22
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
23
|
+
rdoc.rdoc_dir = 'rdoc'
|
24
|
+
rdoc.title = 'ValidatesAsEmailAddress'
|
25
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
26
|
+
rdoc.rdoc_files.include('README')
|
27
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
28
|
+
end
|
29
|
+
|
30
|
+
spec = Gem::Specification.new do |s|
|
31
|
+
s.name = PKG_NAME
|
32
|
+
s.version = PKG_VERSION
|
33
|
+
s.platform = Gem::Platform::RUBY
|
34
|
+
s.summary = 'Adds support for validating the format/length of email addresses'
|
35
|
+
|
36
|
+
s.files = FileList['{lib,test}/**/*'].to_a + %w(CHANGELOG init.rb LICENSE Rakefile README)
|
37
|
+
s.require_path = 'lib'
|
38
|
+
s.autorequire = 'validates_as_email_address'
|
39
|
+
s.has_rdoc = true
|
40
|
+
s.test_files = Dir['test/**/*_test.rb']
|
41
|
+
|
42
|
+
s.author = 'Aaron Pfeifer'
|
43
|
+
s.email = 'aaron@pluginaweek.org'
|
44
|
+
s.homepage = 'http://www.pluginaweek.org'
|
45
|
+
end
|
46
|
+
|
47
|
+
Rake::GemPackageTask.new(spec) do |p|
|
48
|
+
p.gem_spec = spec
|
49
|
+
p.need_tar = true
|
50
|
+
p.need_zip = true
|
51
|
+
end
|
52
|
+
|
53
|
+
desc 'Publish the beta gem'
|
54
|
+
task :pgem => [:package] do
|
55
|
+
Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
|
56
|
+
end
|
57
|
+
|
58
|
+
desc 'Publish the API documentation'
|
59
|
+
task :pdoc => [:rdoc] do
|
60
|
+
Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{PKG_NAME}", 'rdoc').upload
|
61
|
+
end
|
62
|
+
|
63
|
+
desc 'Publish the API docs and gem'
|
64
|
+
task :publish => [:pgem, :pdoc, :release]
|
65
|
+
|
66
|
+
desc 'Publish the release files to RubyForge.'
|
67
|
+
task :release => [:gem, :package] do
|
68
|
+
require 'rubyforge'
|
69
|
+
|
70
|
+
ruby_forge = RubyForge.new
|
71
|
+
ruby_forge.login
|
72
|
+
|
73
|
+
%w( gem tgz zip ).each do |ext|
|
74
|
+
file = "pkg/#{PKG_FILE_NAME}.#{ext}"
|
75
|
+
puts "Releasing #{File.basename(file)}..."
|
76
|
+
|
77
|
+
ruby_forge.add_release(RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, file)
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# The standard describing the format of email addresses
|
2
|
+
module RFC822
|
3
|
+
EmailAddress = begin
|
4
|
+
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
5
|
+
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
|
6
|
+
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
|
7
|
+
quoted_pair = '\\x5c[\\x00-\\x7f]'
|
8
|
+
domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
|
9
|
+
quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
|
10
|
+
domain_ref = atom
|
11
|
+
sub_domain = "(?:#{domain_ref}|#{domain_literal})"
|
12
|
+
word = "(?:#{atom}|#{quoted_string})"
|
13
|
+
domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
|
14
|
+
local_part = "#{word}(?:\\x2e#{word})*"
|
15
|
+
addr_spec = "(#{local_part})\\x40(#{domain})"
|
16
|
+
pattern = /\A#{addr_spec}\z/
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'validates_as_email_address/rfc_822'
|
2
|
+
|
3
|
+
module PluginAWeek #:nodoc:
|
4
|
+
# Adds validations for email addresses
|
5
|
+
module ValidatesAsEmailAddress
|
6
|
+
# The options that can be used when validating the format of the email address
|
7
|
+
EMAIL_FORMAT_OPTIONS = [
|
8
|
+
:wrong_format,
|
9
|
+
:allow_nil,
|
10
|
+
:on,
|
11
|
+
:if
|
12
|
+
]
|
13
|
+
|
14
|
+
# The options that can be used when validating the length of the email address
|
15
|
+
EMAIL_LENGTH_OPTIONS = [
|
16
|
+
:minimum,
|
17
|
+
:maximum,
|
18
|
+
:is,
|
19
|
+
:within,
|
20
|
+
:in,
|
21
|
+
:too_long,
|
22
|
+
:too_short,
|
23
|
+
:wrong_length,
|
24
|
+
:allow_nil,
|
25
|
+
:on,
|
26
|
+
:if
|
27
|
+
]
|
28
|
+
|
29
|
+
# Validates whether the value of the specific attribute matches against the
|
30
|
+
# RFC822 specificiation.
|
31
|
+
#
|
32
|
+
# class Person < ActiveRecord::Base
|
33
|
+
# validates_as_email_address :email, :on => :create
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# This will also validate that the email address is within the specification
|
37
|
+
# limits, specifically between 3 and 320 characters in length.
|
38
|
+
#
|
39
|
+
# Configuration options for length:
|
40
|
+
# * +minimum+ - The minimum size of the attribute
|
41
|
+
# * +maximum+ - The maximum size of the attribute
|
42
|
+
# * +is+ - The exact size of the attribute
|
43
|
+
# * +within+ - A range specifying the minimum and maximum size of the attribute
|
44
|
+
# * +in+ - A synonym(or alias) for :within
|
45
|
+
# * +too_long+ - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)")
|
46
|
+
# * +too_short+ - The error message if the attribute goes under the minimum (default is: "is too short (minimum is %d characters)")
|
47
|
+
# * +wrong_length+ - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)")
|
48
|
+
#
|
49
|
+
# Configuration options for format:
|
50
|
+
# * +wrong_format+ - A custom error message (default is: "is an invalid email address")
|
51
|
+
#
|
52
|
+
# Configuration options for both length and format:
|
53
|
+
# * +allow_nil+ - Attribute may be nil; skip validation.
|
54
|
+
# * +on+ - Specifies when this validation is active (default is :save, other options :create, :update)
|
55
|
+
# * +if+ - Specifies a method, proc or string to call to determine if the validation should
|
56
|
+
# occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
|
57
|
+
# method, proc or string should return or evaluate to a true or false value.
|
58
|
+
def validates_as_email_address(*attr_names)
|
59
|
+
configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
|
60
|
+
configuration.assert_valid_keys(EMAIL_FORMAT_OPTIONS | EMAIL_LENGTH_OPTIONS)
|
61
|
+
configuration.reverse_merge!(
|
62
|
+
:wrong_format => ActiveRecord::Errors.default_error_messages[:invalid_email]
|
63
|
+
)
|
64
|
+
|
65
|
+
# Add format validation
|
66
|
+
format_configuration = configuration.reject {|key, value| !EMAIL_FORMAT_OPTIONS.include?(key)}
|
67
|
+
format_configuration[:message] = format_configuration.delete(:wrong_format)
|
68
|
+
format_configuration[:with] = RFC822::EmailAddress
|
69
|
+
validates_format_of attr_names, format_configuration
|
70
|
+
|
71
|
+
# Add length validation
|
72
|
+
length_configuration = configuration.reject {|key, value| !EMAIL_LENGTH_OPTIONS.include?(key)}
|
73
|
+
length_configuration.reverse_merge!(:within => 3..320)
|
74
|
+
validates_length_of attr_names, length_configuration
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
ActiveRecord::Base.class_eval do
|
80
|
+
extend PluginAWeek::ValidatesAsEmailAddress
|
81
|
+
end
|
82
|
+
|
83
|
+
ActiveRecord::Errors.default_error_messages.update(
|
84
|
+
:invalid_email => 'is an invalid email address'
|
85
|
+
)
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_as_email_address'
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Logfile created on Sun Apr 06 00:59:11 -0400 2008 [4;36;1mSQL (0.000430)[0m [0;1mselect sqlite_version(*)[0m
|
2
|
+
[4;35;1mSQL (0.000442)[0m [0mCREATE TABLE "schema_info" (version varchar(255))[0m
|
3
|
+
[4;36;1mSQL (0.000121)[0m [0;1mINSERT INTO "schema_info" (version) VALUES(0)[0m
|
4
|
+
[4;35;1mSQL (0.000224)[0m [0mSELECT version FROM schema_info[0m
|
5
|
+
Migrating to CreateUsers (1)
|
6
|
+
[4;36;1mSQL (0.000223)[0m [0;1m SELECT name
|
7
|
+
FROM sqlite_master
|
8
|
+
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
|
9
|
+
[0m
|
10
|
+
[4;36;1mSQL (0.000428)[0m [0;1mselect sqlite_version(*)[0m
|
11
|
+
[4;35;1mSQL (0.000429)[0m [0mCREATE TABLE "schema_info" (version varchar(255))[0m
|
12
|
+
[4;36;1mSQL (0.000115)[0m [0;1mINSERT INTO "schema_info" (version) VALUES(0)[0m
|
13
|
+
[4;35;1mSQL (0.000221)[0m [0mSELECT version FROM schema_info[0m
|
14
|
+
Migrating to CreateUsers (1)
|
15
|
+
[4;36;1mSQL (0.000359)[0m [0;1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(320) NOT NULL) [0m
|
16
|
+
[4;35;1mSQL (0.000106)[0m [0mUPDATE schema_info SET version = 1[0m
|
17
|
+
[4;36;1mSQL (0.000431)[0m [0;1mselect sqlite_version(*)[0m
|
18
|
+
[4;35;1mSQL (0.000426)[0m [0mCREATE TABLE "schema_info" (version varchar(255))[0m
|
19
|
+
[4;36;1mSQL (0.000114)[0m [0;1mINSERT INTO "schema_info" (version) VALUES(0)[0m
|
20
|
+
[4;35;1mSQL (0.000224)[0m [0mSELECT version FROM schema_info[0m
|
21
|
+
Migrating to CreateUsers (1)
|
22
|
+
[4;36;1mSQL (0.000352)[0m [0;1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(320) NOT NULL) [0m
|
23
|
+
[4;35;1mSQL (0.000105)[0m [0mUPDATE schema_info SET version = 1[0m
|
24
|
+
[4;36;1mSQL (0.000460)[0m [0;1m SELECT name
|
25
|
+
FROM sqlite_master
|
26
|
+
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
|
27
|
+
[0m
|
28
|
+
[4;35;1mSQL (0.000260)[0m [0mselect sqlite_version(*)[0m
|
29
|
+
[4;36;1mSQL (0.000324)[0m [0;1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
30
|
+
[4;35;1mSQL (0.000255)[0m [0mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
31
|
+
[4;36;1mSQL (0.000211)[0m [0;1m SELECT name
|
32
|
+
FROM sqlite_master
|
33
|
+
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
|
34
|
+
[0m
|
35
|
+
[4;35;1mSQL (0.000150)[0m [0mSELECT version FROM schema_migrations[0m
|
36
|
+
Migrating to CreateUsers (1)
|
37
|
+
[4;36;1mSQL (0.000093)[0m [0;1mSELECT version FROM schema_migrations[0m
|
38
|
+
[4;35;1mSQL (0.000361)[0m [0mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(320) NOT NULL) [0m
|
39
|
+
[4;36;1mSQL (0.000151)[0m [0;1mINSERT INTO schema_migrations (version) VALUES ('1')[0m
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class ValidatesAsEmailAddressTest < Test::Unit::TestCase
|
4
|
+
def test_should_require_legal_rfc822_format
|
5
|
+
[
|
6
|
+
'Max@Job 3:14',
|
7
|
+
'Job@Book of Job',
|
8
|
+
'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
|
9
|
+
].each do |address|
|
10
|
+
assert !User.new(:email => address).valid?, "#{address} should be illegal."
|
11
|
+
end
|
12
|
+
|
13
|
+
[
|
14
|
+
'test@example',
|
15
|
+
'test@example.com',
|
16
|
+
'test@example.co.uk',
|
17
|
+
'"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
|
18
|
+
'me@[187.223.45.119]',
|
19
|
+
'someone@123.com',
|
20
|
+
].each do |address|
|
21
|
+
assert User.new(:email => address).valid?, "#{address} should be legal."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_not_allow_email_addresses_longer_than_320_characters
|
26
|
+
assert User.new(:email => 'a' * 314 + '@a.com').valid?
|
27
|
+
assert !User.new(:email => 'a' * 315 + '@a.com').valid?
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jcnetdev-validates_as_email_address
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.20080704
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Pfeifer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-04 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: avanie@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- CHANGELOG
|
25
|
+
- LICENSE
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- lib/validates_as_email_address/rfc_822.rb
|
29
|
+
- lib/validates_as_email_address.rb
|
30
|
+
- LICENSE
|
31
|
+
- rails/init.rb
|
32
|
+
- Rakefile
|
33
|
+
- README
|
34
|
+
- test/app_root/app/models/user.rb
|
35
|
+
- test/app_root/db/migrate/001_create_users.rb
|
36
|
+
- test/app_root/log/in_memory.log
|
37
|
+
- test/test_helper.rb
|
38
|
+
- test/unit/validates_as_email_address_test.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/pager/validates_as_email_address
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --main
|
44
|
+
- README
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.2.0
|
63
|
+
signing_key:
|
64
|
+
specification_version: 2
|
65
|
+
summary: Gemified validates_as_email_address plugin
|
66
|
+
test_files:
|
67
|
+
- test/app_root/app/models/user.rb
|
68
|
+
- test/app_root/db/migrate/001_create_users.rb
|
69
|
+
- test/app_root/log/in_memory.log
|
70
|
+
- test/test_helper.rb
|
71
|
+
- test/unit/validates_as_email_address_test.rb
|