valid_email 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.swp
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in email_validator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 hallelujah [Ramihajamalala Hery]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Purpose
2
+
3
+ It validates email for application use (registering a new account for example)
4
+
5
+ # Usage
6
+
7
+ In your Gemfile :
8
+
9
+ gem 'valid_email'
10
+
11
+
12
+ In your code :
13
+
14
+ require 'valid_email'
15
+ class Person
16
+ include ActiveModel::Validations
17
+ attr_accessor :name, :email
18
+
19
+ validates :name, :presence => true, :length => { :maximum => 100 }
20
+ validates :email, :presence => true, :email => true
21
+ end
22
+
23
+
24
+ p = Person.new
25
+ p.name = "hallelujah"
26
+ p.email = "john@doe.com"
27
+ p.valid? # => true
28
+
29
+ p.email = "john@doe"
30
+ p.valid? # => false
31
+
32
+ p.email = "John Does <john@doe.com>"
33
+ p.valid? # => false
34
+
35
+
36
+
37
+ # Note on Patches/Pull Requests
38
+
39
+ * Fork the project.
40
+
41
+ * Make your feature addition or bug fix.
42
+
43
+ * Add tests for it. This is important so I don’t break it in a future version unintentionally.
44
+
45
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
46
+
47
+ * Send me a pull request. Bonus points for topic branches.
48
+
49
+ # Copyright
50
+
51
+ Copyright &copy; 2011 Ramihajamalala Hery. See LICENSE for details
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+
9
+ task :default => [:spec]
10
+ task :build => [:spec]
@@ -0,0 +1,2 @@
1
+ ValidEmailVersion = "0.0.1"
2
+
@@ -0,0 +1,23 @@
1
+ require 'active_model'
2
+ require 'active_model/validations'
3
+ require 'mail'
4
+ class EmailValidator < ActiveModel::EachValidator
5
+ def validate_each(record,attribute,value)
6
+ begin
7
+ m = Mail::Address.new(value)
8
+ # We must check that value contains a domain and that value is an email address
9
+ r = m.domain && m.address == value
10
+ t = m.__send__(:tree)
11
+ # We need to dig into treetop
12
+ # A valid domain must have dot_atom_text elements size > 1
13
+ # user@localhost is excluded
14
+ # treetop must respond to domain
15
+ # We exclude valid email values like <user@localhost.com>
16
+ # Hence we use m.__send__(tree).domain
17
+ r &&= (t.domain.dot_atom_text.elements.size > 1)
18
+ rescue Exception => e
19
+ r = false
20
+ end
21
+ record.errors[attribute] << (options[:message] || "is invalid") unless r
22
+ end
23
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmailValidator do
4
+ person_class = Class.new do
5
+ include ActiveModel::Validations
6
+ attr_accessor :email
7
+ validates :email, :email => true
8
+ end
9
+
10
+
11
+
12
+ describe "validating email" do
13
+ subject { person_class.new }
14
+
15
+ it "should fail when email empty" do
16
+ subject.valid?.should be_false
17
+ subject.errors[:email].should == [ "is invalid" ]
18
+ end
19
+
20
+ it "should fail when email is not valid" do
21
+ subject.email = 'joh@doe'
22
+ subject.valid?.should be_false
23
+ subject.errors[:email].should == [ "is invalid" ]
24
+ end
25
+
26
+ it "should fail when email is valid with information" do
27
+ subject.email = '"John Doe" <john@doe.com>'
28
+ subject.valid?.should be_false
29
+ subject.errors[:email].should == [ "is invalid" ]
30
+ end
31
+
32
+ it "should pass when email is simple email address" do
33
+ subject.email = 'john@doe.com'
34
+ subject.valid?.should be_true
35
+ subject.errors[:email].should be_empty
36
+ end
37
+
38
+ it "should fail when email is simple email address not stripped" do
39
+ subject.email = 'john@doe.com '
40
+ subject.valid?.should be_false
41
+ subject.errors[:email].should == [ "is invalid" ]
42
+ end
43
+
44
+
45
+ it "should fail when passing multiple simple email addresses" do
46
+ subject.email = 'john@doe.com, maria@doe.com'
47
+ subject.valid?.should be_false
48
+ subject.errors[:email].should == [ "is invalid" ]
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.expand_path('../../lib',__FILE__)
2
+ require 'valid_email'
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "valid_email/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "valid_email"
7
+ s.version = ValidEmailVersion
8
+ s.authors = ["Ramihajamalala Hery"]
9
+ s.email = ["hery@rails-royce.org"]
10
+ s.homepage = "http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp"
11
+ s.summary = %q{ActiveModel Validation for email}
12
+ s.description = %q{ActiveModel Validation for email}
13
+
14
+ s.rubyforge_project = "valid_email"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "rake"
24
+ s.add_runtime_dependency "mail"
25
+ s.add_runtime_dependency "activemodel"
26
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: valid_email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ramihajamalala Hery
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &18108960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *18108960
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &18108020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *18108020
36
+ - !ruby/object:Gem::Dependency
37
+ name: mail
38
+ requirement: &18107240 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *18107240
47
+ - !ruby/object:Gem::Dependency
48
+ name: activemodel
49
+ requirement: &18106280 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *18106280
58
+ description: ActiveModel Validation for email
59
+ email:
60
+ - hery@rails-royce.org
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - lib/valid_email.rb
71
+ - lib/valid_email/version.rb
72
+ - spec/email_validator_spec.rb
73
+ - spec/spec_helper.rb
74
+ - valid_email.gemspec
75
+ homepage: http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ segments:
88
+ - 0
89
+ hash: -211291560077668896
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ segments:
97
+ - 0
98
+ hash: -211291560077668896
99
+ requirements: []
100
+ rubyforge_project: valid_email
101
+ rubygems_version: 1.8.6
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: ActiveModel Validation for email
105
+ test_files:
106
+ - spec/email_validator_spec.rb
107
+ - spec/spec_helper.rb