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.
@@ -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
- unless url_regexp =~ normalized_value
22
- record.errors.add(attribute, message, :value => uri.to_s)
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)
@@ -1,4 +1,4 @@
1
1
  module UrlValidator
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
3
3
  end
4
4
 
@@ -32,19 +32,18 @@ describe "User" do
32
32
  @user.should_not be_valid
33
33
  end
34
34
 
35
- it "www.website.c should not be valid" do
36
- @user.website = 'www.website.c'
37
- @user.should_not be_valid
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 "www.web site.com should not be valid" do
41
- @user.website = 'www.web site.com'
42
- @user.should_not be_valid
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
- it "should not accept invalid URLs" do
46
- ["htt://www.website.com", "htttp://www.website.com",
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.詹姆斯.c"
189
+ @user.website = "ftp://www.詹姆斯"
196
190
  @user.should_not be_valid
197
191
  @user.errors[:website].should_not be_empty
198
192
  end
@@ -1,3 +1,2 @@
1
1
  --colour
2
2
  --format progress
3
-
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.7
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-06-12 00:00:00.000000000 Z
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: