url_validator 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  module UrlValidator
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
4
4
 
data/lib/url_validator.rb CHANGED
@@ -7,14 +7,16 @@ module ActiveModel
7
7
  class UrlValidator < ActiveModel::Validator
8
8
 
9
9
  def validate(record)
10
- schemes = options[:schemes] || %w(http https).join('|')
11
10
  message = options[:message] || "is not a valid URL"
12
- url_regexp = /^((#{schemes}):\/\/){0,1}[a-z0-9]+([a-z0-9\-\.]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
11
+ schemes = options[:schemes] || %w(http https)
12
+ url_regexp = /^((#{schemes.join('|')}):\/\/){0,1}[a-z0-9]+([a-z0-9\-\.]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
13
+ preffered_schema = options[:preffered_schema] || "#{schemes.first}://"
13
14
 
14
15
  options[:attributes].each do |attribute|
15
16
  value = record.send(attribute).to_s
16
17
  next if value.blank? && (options[:allow_blank] || options[:allow_nil])
17
- normalized_value = record.send(attribute.to_s + '_normalized')
18
+ record.send("#{attribute}=", preffered_schema << value) if !value.start_with?(*schemes)
19
+ normalized_value = record.send("#{attribute}_normalized")
18
20
  begin
19
21
  uri = Addressable::URI.parse(value)
20
22
  unless url_regexp =~ normalized_value
@@ -236,5 +236,50 @@ describe "User" do
236
236
  @user.errors[:website].to_s.should == 'is not a valid URL'
237
237
  end
238
238
  end
239
+
240
+ context "automated preffilling prefix - preffill if didnt exists" do
241
+ before do
242
+ @user = User.new
243
+ @user.website = 'website.com'
244
+ @user_with_s = UserWithPrefferedSchema.new
245
+ @user_with_s.website = 'website.com'
246
+ end
247
+
248
+ it "should preffill http:// to url" do
249
+ @user.should be_valid
250
+ @user.website.start_with?('http://').should be_true
251
+ end
252
+
253
+ it "should preffill https:// to url" do
254
+ @user_with_s.should be_valid
255
+ @user_with_s.website.start_with?('https://').should be_true
256
+ end
257
+ end
258
+
259
+ context "automated preffilling prefix - dont preffill if exist" do
260
+ before do
261
+ @user = User.new
262
+ @user.website = 'http://website.com'
263
+ @user_with_s = UserWithPrefferedSchema.new
264
+ @user_with_s.website = 'https://website.com'
265
+ end
266
+
267
+ it "should not preffill http://" do
268
+ @user.should be_valid
269
+ @user.website.should == 'http://website.com'
270
+ @user.website = 'https://website.com'
271
+ @user.valid?
272
+ @user.website.should == 'https://website.com'
273
+ end
274
+
275
+ it "should not preffill https://" do
276
+ @user_with_s.should be_valid
277
+ @user_with_s.website.should == 'https://website.com'
278
+ @user_with_s.website = 'http://website.com'
279
+ @user.valid?
280
+ @user_with_s.website.should == 'http://website.com'
281
+ end
282
+ end
283
+
239
284
  end
240
285
 
@@ -0,0 +1,8 @@
1
+ class UserWithPrefferedSchema
2
+ include ActiveModel::Validations
3
+
4
+ attr_accessor :website, :url
5
+
6
+ validates_url :website, :preffered_schema => 'https://'
7
+ end
8
+
data/spec/spec_helper.rb CHANGED
@@ -22,4 +22,5 @@ autoload :User ,'resources/user'
22
22
  autoload :UserWithBlank ,'resources/user_with_blank'
23
23
  autoload :UserWithAr ,'resources/user_with_ar'
24
24
  autoload :UserWithCustomScheme ,'resources/user_with_custom_scheme'
25
+ autoload :UserWithPrefferedSchema ,'resources/user_with_preffered_schema'
25
26
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_validator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zdenko Nevrala
@@ -112,6 +112,7 @@ files:
112
112
  - spec/resources/user_with_ar.rb
113
113
  - spec/resources/user_with_blank.rb
114
114
  - spec/resources/user_with_custom_scheme.rb
115
+ - spec/resources/user_with_preffered_schema.rb
115
116
  - spec/spec.opts
116
117
  - spec/spec_helper.rb
117
118
  - url_validator.gemspec