url_validator 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/url_validator.rb +19 -7
- data/lib/url_validator/version.rb +1 -1
- data/spec/models/user_spec.rb +10 -16
- data/spec/spec.opts +0 -1
- metadata +3 -2
data/lib/url_validator.rb
CHANGED
@@ -9,17 +9,29 @@ module ActiveModel
|
|
9
9
|
def validate(record)
|
10
10
|
message = options[:message] || "is not a valid URL"
|
11
11
|
schemes = options[:schemes] || %w(http https)
|
12
|
-
url_regexp = /^((#{schemes.join('|')}):\/\/){0,1}[a-z0-9]+([a-z0-9\-\.]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$/ix
|
13
12
|
preffered_schema = options[:preffered_schema] || "#{schemes.first}://"
|
14
13
|
options[:attributes].each do |attribute|
|
15
|
-
value = record.send(attribute).to_s
|
16
|
-
next if value.blank? && (options[:allow_blank] || options[:allow_nil])
|
17
|
-
record.send("#{attribute}=", preffered_schema + value) if !value.start_with?(*schemes)
|
18
|
-
normalized_value = record.send("#{attribute}_normalized")
|
19
14
|
begin
|
15
|
+
value = record.send(attribute).to_s
|
16
|
+
value.gsub!(/\s+/, "") if value.present?
|
17
|
+
next if value.blank? && (options[:allow_blank] || options[:allow_nil])
|
18
|
+
|
20
19
|
uri = Addressable::URI.parse(value)
|
21
|
-
|
22
|
-
|
20
|
+
|
21
|
+
if uri.scheme.blank?
|
22
|
+
record.send("#{attribute}=", preffered_schema + value)
|
23
|
+
value = preffered_schema + value
|
24
|
+
uri = Addressable::URI.parse(value)
|
25
|
+
end
|
26
|
+
|
27
|
+
unless schemes.include?(uri.scheme)
|
28
|
+
raise Addressable::URI::InvalidURIError
|
29
|
+
end
|
30
|
+
|
31
|
+
normalized_value = record.send("#{attribute}_normalized")
|
32
|
+
|
33
|
+
if value.blank? && (!options[:allow_blank] || !options[:allow_nil])
|
34
|
+
raise Addressable::URI::InvalidURIError
|
23
35
|
end
|
24
36
|
rescue Addressable::URI::InvalidURIError
|
25
37
|
record.errors.add(attribute, message, :value => uri.to_s)
|
data/spec/models/user_spec.rb
CHANGED
@@ -32,19 +32,18 @@ describe "User" do
|
|
32
32
|
@user.should_not be_valid
|
33
33
|
end
|
34
34
|
|
35
|
-
it "
|
36
|
-
@user.website =
|
37
|
-
@user.
|
35
|
+
it "should sanitize space in url" do
|
36
|
+
@user.website = "web.asd dot.com"
|
37
|
+
@user.should be_valid
|
38
38
|
end
|
39
39
|
|
40
|
-
it "
|
41
|
-
@user.website =
|
42
|
-
@user.
|
40
|
+
it "should sanitize space in url" do
|
41
|
+
@user.website = "webasd\tdot.com"
|
42
|
+
@user.should be_valid
|
43
43
|
end
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
"http:/www.website.com", "http://website"].each do |url|
|
45
|
+
["htt://www.website.com", "htttp://www.website.com"].each do |url|
|
46
|
+
it "should not accept invalid #{url}" do
|
48
47
|
@user.website = url
|
49
48
|
@user.should_not be_valid
|
50
49
|
end
|
@@ -146,15 +145,10 @@ describe "User" do
|
|
146
145
|
end
|
147
146
|
|
148
147
|
it "should not allow invalid url: website" do
|
149
|
-
@user.website = "website"
|
148
|
+
@user.website = "ftp://website.com"
|
150
149
|
@user.should_not be_valid
|
151
150
|
@user.errors[:website].should_not be_empty
|
152
151
|
end
|
153
|
-
|
154
|
-
it "should not allow invalid url: website.c" do
|
155
|
-
@user.website = "website.c"
|
156
|
-
@user.should_not be_valid
|
157
|
-
end
|
158
152
|
end
|
159
153
|
|
160
154
|
context "with regular validator and custom scheme ftp://" do
|
@@ -192,7 +186,7 @@ describe "User" do
|
|
192
186
|
end
|
193
187
|
|
194
188
|
it "should not be valid http://www.詹姆斯.c" do
|
195
|
-
@user.website = "www
|
189
|
+
@user.website = "ftp://www.詹姆斯"
|
196
190
|
@user.should_not be_valid
|
197
191
|
@user.errors[:website].should_not be_empty
|
198
192
|
end
|
data/spec/spec.opts
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: url_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -147,3 +147,4 @@ test_files:
|
|
147
147
|
- spec/resources/user_with_preffered_schema.rb
|
148
148
|
- spec/spec.opts
|
149
149
|
- spec/spec_helper.rb
|
150
|
+
has_rdoc:
|