webget-email_address_pattern_match_validation 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
|
|
1
|
+
# = EmailAddressPatternMatchValidation
|
2
|
+
#
|
3
|
+
# Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
|
4
|
+
# Copyright:: Copyright (c) 2006-2008 Joel Parker Henderson
|
5
|
+
# License:: CreativeCommons License, Non-commercial Share Alike
|
6
|
+
# License:: LGPL, GNU Lesser General Public License
|
7
|
+
#
|
8
|
+
# Email address regex, to validate an email address using RFC 822.
|
9
|
+
#
|
10
|
+
# This is a gem wrapper around http://tfletcher.com/lib/rfc822.rb
|
11
|
+
#
|
12
|
+
# ==Example
|
13
|
+
# if EmailAddressPattern=~'foo@bar.com'
|
14
|
+
# puts "found"
|
15
|
+
# else
|
16
|
+
# puts "not found"
|
17
|
+
# end
|
18
|
+
# => found
|
19
|
+
#
|
20
|
+
# To find an email address in a string, do the pattern match
|
21
|
+
# then use the result, which is the match's string position:
|
22
|
+
#
|
23
|
+
# ==Example of match position
|
24
|
+
# match_position = EmailAddressPattern=~'foo@bar.com'
|
25
|
+
# => 0
|
26
|
+
#
|
27
|
+
# match_position = EmailAddressPattern=~'... foo@bar.com ...'
|
28
|
+
# => 4
|
29
|
+
#
|
30
|
+
# match_position = EmailAddressPattern=~'... hello world ...'
|
31
|
+
# => nil
|
32
|
+
#
|
33
|
+
# To do an exact pattern match, use the EmailAddressExactPattern.
|
34
|
+
# This matches the entire string from start to end; in other words,
|
35
|
+
# the entire string must be one email address.
|
36
|
+
#
|
37
|
+
# ==Example of exact pattern match
|
38
|
+
# if EmailAddressExactPattern=~'foo@bar.com'
|
39
|
+
# puts "found"
|
40
|
+
# else
|
41
|
+
# puts "not found"
|
42
|
+
# end
|
43
|
+
# => found
|
44
|
+
#
|
45
|
+
# if EmailAddressExactPattern=~'... foo@bar.com ...'
|
46
|
+
# puts "found"
|
47
|
+
# else
|
48
|
+
# puts "not found"
|
49
|
+
# end
|
50
|
+
# => not found
|
51
|
+
#
|
52
|
+
# The exact pattern is especialy useful to validate an email address.
|
53
|
+
#
|
54
|
+
# ==Example to validate an email address
|
55
|
+
# def valid?(email_address)
|
56
|
+
# EmailAddressExactPattern=~email_address ? true : false
|
57
|
+
# end
|
58
|
+
##
|
59
|
+
|
60
|
+
module EmailAddressPatternMatchValidation
|
61
|
+
|
62
|
+
EmailAddressQText = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
63
|
+
EmailAddressDText = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
|
64
|
+
EmailAddressAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
|
65
|
+
EmailAddressQuotedPair = '\\x5c[\\x00-\\x7f]'
|
66
|
+
EmailAddressDomainLiteral = "\\x5b(?:#{EmailAddressDText}|#{EmailAddressQuotedPair})*\\x5d"
|
67
|
+
EmailAddressQuotedString = "\\x22(?:#{EmailAddressQText}|#{EmailAddressQuotedPair})*\\x22"
|
68
|
+
EmailAddressDomainRef = EmailAddressAtom
|
69
|
+
EmailAddressSubDomain = "(?:#{EmailAddressDomainRef}|#{EmailAddressDomainLiteral})"
|
70
|
+
EmailAddressWord = "(?:#{EmailAddressAtom}|#{EmailAddressQuotedString})"
|
71
|
+
EmailAddressDomain = "#{EmailAddressSubDomain}(?:\\x2e#{EmailAddressSubDomain})*"
|
72
|
+
EmailAddressLocalPart = "#{EmailAddressWord}(?:\\x2e#{EmailAddressWord})*"
|
73
|
+
EmailAddressSpec = "#{EmailAddressLocalPart}\\x40#{EmailAddressDomain}"
|
74
|
+
|
75
|
+
EmailAddressPattern = /#{EmailAddressSpec}/
|
76
|
+
EmailAddressExactPattern = /\A#{EmailAddressSpec}\z/
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'email_address_pattern_match_validation'
|
3
|
+
|
4
|
+
class EmailAddressPatternMatchValidationTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include EmailAddressPatternMatchValidation
|
7
|
+
|
8
|
+
def test_pattern_success
|
9
|
+
assert_equal(0,EmailAddressPattern=~'foo@bar.com')
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_pattern_failure
|
13
|
+
assert_equal(nil,EmailAddressPattern=~'foo')
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_pattern_chaff_success
|
17
|
+
assert_equal(4,EmailAddressPattern=~'... foo@bar.com ...')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_pattern_chaff_failure
|
21
|
+
assert_equal(nil,EmailAddressPattern=~'... foo ...')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_exact_pattern_success
|
25
|
+
assert_equal(0,EmailAddressExactPattern=~'foo@bar.com')
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_exact_pattern_failure_due_to_left_chaff
|
29
|
+
assert_equal(nil,EmailAddressExactPattern=~'... foo@bar.com')
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_exact_pattern_failure_due_to_right_chaff
|
33
|
+
assert_equal(nil,EmailAddressExactPattern=~'foo@bar.com ...')
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webget-email_address_pattern_match_validation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- WebGet
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: webget@webget.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/email_address_pattern_match_validation.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage: http://webget.com/
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: "0"
|
38
|
+
version:
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.2.0
|
49
|
+
signing_key:
|
50
|
+
specification_version: 2
|
51
|
+
summary: Validate an email address using RFC 822 pattern match regex regular expressions
|
52
|
+
test_files:
|
53
|
+
- test/unit/email_address_pattern_match_validation_test.rb
|