ruby_regex 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/README.rdoc +14 -1
- data/lib/ruby_regex.rb +19 -4
- data/test/ruby_regex_test.rb +75 -1
- metadata +3 -3
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -7,8 +7,15 @@ Ruby regular expressions library
|
|
7
7
|
RubyRegex::Url
|
8
8
|
RubyRegex::Domain
|
9
9
|
RubyRegex::Email
|
10
|
+
RubyRegex::Username
|
10
11
|
RubyRegex::ZipCode
|
11
|
-
|
12
|
+
RubyRegex::USSocialSecurity
|
13
|
+
RubyRegex::GeneralPostalCode
|
14
|
+
RubyRegex::ZIPCode
|
15
|
+
RubyRegex::CreditCard
|
16
|
+
RubyRegex::MasterCard
|
17
|
+
RubyRegex::Visa
|
18
|
+
|
12
19
|
== Rails
|
13
20
|
|
14
21
|
# config/environment.rb
|
@@ -18,6 +25,12 @@ Ruby regular expressions library
|
|
18
25
|
# models
|
19
26
|
validates_format_of :email, :with => RubyRegex::Email
|
20
27
|
|
28
|
+
== Contribute
|
29
|
+
|
30
|
+
Of course, contributions will be apreciated.
|
31
|
+
|
32
|
+
If you want to contribute send me a pull request with new regular expressions (and their tests) or send me an email with your desired regular expression to emili@eparreno.com
|
33
|
+
|
21
34
|
---
|
22
35
|
|
23
36
|
RubyRegex is released under the MIT-License and is Copyright (c)2010 Emili Parreño.
|
data/lib/ruby_regex.rb
CHANGED
@@ -6,18 +6,33 @@ module RubyRegex
|
|
6
6
|
# Dni (spanish ID card)
|
7
7
|
Dni = /^\d{8}[A-Za-z]{1}$/
|
8
8
|
|
9
|
-
# URL
|
9
|
+
# URL
|
10
10
|
Url = URL = /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix
|
11
11
|
|
12
|
-
# Domain
|
12
|
+
# Domain
|
13
13
|
Domain = /(^$)|(^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?)?$)/ix
|
14
14
|
|
15
|
-
#
|
15
|
+
# CreditCard
|
16
|
+
# Validates Credit Card numbers, Checks if it contains 16 numbers in groups of 4 separated by -, space or nothing
|
17
|
+
CreditCard = /^(\d{4}-){3}\d{4}$|^(\d{4}\s){3}\d{4}$|^\d{16}$/
|
18
|
+
|
19
|
+
# MasterCard credit card
|
16
20
|
MasterCard = /^5[1-5]\d{14}$/
|
17
21
|
|
18
|
-
# Visa
|
22
|
+
# Visa credit card
|
19
23
|
Visa = /^4\d{15}$/
|
20
24
|
|
25
|
+
# US Social Security
|
26
|
+
USSocialSecurity = /^\d{3}-\d{2}-\d{4}$/
|
27
|
+
|
28
|
+
# General postal code
|
29
|
+
# Validates a 5 digits postal code
|
30
|
+
GeneralPostalCode = /^\d{5}$/
|
31
|
+
|
32
|
+
# US ZIP code
|
33
|
+
# Validates US ZIP Code (basic and extended format)
|
34
|
+
ZIPCode = /^(\d{5}$)|(\d{5}-\d{4}$)/
|
35
|
+
|
21
36
|
#
|
22
37
|
# RFC822 Email Address Regex
|
23
38
|
# --------------------------
|
data/test/ruby_regex_test.rb
CHANGED
@@ -5,6 +5,7 @@ require 'active_support/test_case'
|
|
5
5
|
require 'ruby_regex'
|
6
6
|
|
7
7
|
class RubyRegexTest < ActiveSupport::TestCase
|
8
|
+
#Username
|
8
9
|
def test_valid_usernames
|
9
10
|
usernames = ['test', 'test_test', 'test1', 'test_1']
|
10
11
|
usernames.each do |username|
|
@@ -21,6 +22,7 @@ class RubyRegexTest < ActiveSupport::TestCase
|
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
25
|
+
# DNI
|
24
26
|
def test_valid_dnis
|
25
27
|
dnis = ['40990889J', '99888777h']
|
26
28
|
dnis.each do |dni|
|
@@ -37,6 +39,7 @@ class RubyRegexTest < ActiveSupport::TestCase
|
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
42
|
+
# Email
|
40
43
|
def test_valid_emails
|
41
44
|
emails = ['test@test.com', 'test@test.co.uk', 'test@test.es', 'test@test.info']
|
42
45
|
emails.each do |email|
|
@@ -53,7 +56,8 @@ class RubyRegexTest < ActiveSupport::TestCase
|
|
53
56
|
assert(email !~ RubyRegex::Email, message)
|
54
57
|
end
|
55
58
|
end
|
56
|
-
|
59
|
+
|
60
|
+
# Domains
|
57
61
|
def test_valid_domains
|
58
62
|
domains = [ 'test.com', 'www.test.com', 'test.es', 'www.test.es', 'test.co.uk', 'www.test.co.uk', 'test.info', 'www.test.info', 'test.com.es', 'www.test.com.es']
|
59
63
|
domains.each do |domain|
|
@@ -70,6 +74,7 @@ class RubyRegexTest < ActiveSupport::TestCase
|
|
70
74
|
end
|
71
75
|
end
|
72
76
|
|
77
|
+
# Url
|
73
78
|
def test_valid_url
|
74
79
|
urls = [ 'http://test.com', 'http://www.test.com', 'http://test.es/index', 'http://www.test.es/index.html',
|
75
80
|
'https://test.co.uk', 'http://www.test.co.uk/index.html?id=34&name=username']
|
@@ -86,4 +91,73 @@ class RubyRegexTest < ActiveSupport::TestCase
|
|
86
91
|
assert(url !~ RubyRegex::URL, message)
|
87
92
|
end
|
88
93
|
end
|
94
|
+
|
95
|
+
# CreditCard
|
96
|
+
def test_valid_credit_cards
|
97
|
+
credit_cards = [ '1234123412341234', '1234 1234 1234 1234', '1234-1234-1234-1234']
|
98
|
+
credit_cards.each do |credit_card|
|
99
|
+
message = build_message(message, '<?> do not pass the test', credit_card)
|
100
|
+
assert(credit_card =~ RubyRegex::CreditCard, message)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_invalid_credit_cards
|
105
|
+
credit_cards = [ '1234_1234_1234_1234', '1234', '12341234', '123412341234',
|
106
|
+
'1234 1234 1234 1234', '1234-1234 12341234', '123a-1234-1234-1234']
|
107
|
+
credit_cards.each do |credit_card|
|
108
|
+
message = build_message(message, '<?> do not pass the test', credit_card)
|
109
|
+
assert(credit_card !~ RubyRegex::Domain, message)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# US Social Security
|
114
|
+
def test_valid_usss_numbers
|
115
|
+
usss_numbers = [ '123-12-1234']
|
116
|
+
usss_numbers.each do |usss_number|
|
117
|
+
message = build_message(message, '<?> do not pass the test', usss_number)
|
118
|
+
assert(usss_number =~ RubyRegex::USSocialSecurity, message)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_invalid_usss_numbers
|
123
|
+
usss_numbers = [ '1234_1234_1234_1234', '1234', '123121234', '123_12_1234', '123 12 1234']
|
124
|
+
usss_numbers.each do |usss_number|
|
125
|
+
message = build_message(message, '<?> do not pass the test', usss_number)
|
126
|
+
assert(usss_number !~ RubyRegex::USSocialSecurity, message)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# General Postal Code
|
131
|
+
def test_valid_postal_codes
|
132
|
+
postal_codes = [ '12345']
|
133
|
+
postal_codes.each do |postal_code|
|
134
|
+
message = build_message(message, '<?> do not pass the test', postal_code)
|
135
|
+
assert(postal_code =~ RubyRegex::GeneralPostalCode, message)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_invalid_postal_codes
|
140
|
+
postal_codes = [ '1', '12', '123', '1234', '123456']
|
141
|
+
postal_codes.each do |postal_code|
|
142
|
+
message = build_message(message, '<?> do not pass the test', postal_code)
|
143
|
+
assert(postal_code !~ RubyRegex::GeneralPostalCode, message)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# ZIP Code
|
148
|
+
def test_valid_zip_codes
|
149
|
+
zip_codes = [ '12345', '12345-1234']
|
150
|
+
zip_codes.each do |zip_code|
|
151
|
+
message = build_message(message, '<?> do not pass the test', zip_code)
|
152
|
+
assert(zip_code =~ RubyRegex::ZIPCode, message)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_invalid_zip_codes
|
157
|
+
zip_codes = [ '1', '12', '123', '1234', '123456', '12345_1234', '12345 1234', '1234-1234']
|
158
|
+
zip_codes.each do |zip_code|
|
159
|
+
message = build_message(message, '<?> do not pass the test', zip_code)
|
160
|
+
assert(zip_code !~ RubyRegex::ZIPCode, message)
|
161
|
+
end
|
162
|
+
end
|
89
163
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Emili Parreno
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-06 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|