url_format 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/url_format.rb +6 -0
- data/lib/url_format/url_format_validator.rb +12 -1
- data/lib/url_format/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d5dfa1ccbb5f619c64cb28afa566501f4211ffe
|
4
|
+
data.tar.gz: 91d6d09f0c8c612b824a789b76c6a09bb00f553d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7548a7be7112d4dd292d9c2e06a25961162f2585d53362c70db57e1de8a0c8783ac4445761de62c035a1fd222aa5afc173591b2f5e468b4b4e0f95af368e8bd
|
7
|
+
data.tar.gz: bdb879d6b050ce9e6c6c898e5e321caa0e5a1c99a4bf61a446713f8b6b5006bdadf2aff59bcae7d1c340a027df1b9b3a06c5f707152dd55ba31ea3b2c5669262
|
data/lib/url_format.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
1
|
class UrlFormatValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each(record, attribute, value)
|
3
|
-
|
3
|
+
format_value(value)
|
4
|
+
unless URI.parse(value).kind_of?(URI::HTTP) && value =~ url_regexp
|
4
5
|
record.errors[attribute] << (options[:message] || "is invalid")
|
5
6
|
end
|
6
7
|
rescue URI::InvalidURIError
|
7
8
|
record.errors[attribute] << (options[:message] || "is invalid")
|
8
9
|
end
|
10
|
+
|
11
|
+
def format_value(value)
|
12
|
+
value = "http://#{value}" unless value =~ /\Ahttps?:\/\//
|
13
|
+
end
|
14
|
+
|
15
|
+
# Thanks to Dean Perry and Ryan Bates
|
16
|
+
# https://github.com/deanperry/url_formatter/blob/master/lib/url_formatter.rb
|
17
|
+
def url_regexp
|
18
|
+
/\Ahttps?:\/\/([\A\s:@]+:[\A\s:@]*@)?[-[[:alnum:]]]+(\.[-[[:alnum:]]]+)+\.?(:\d{1,5})?([\/?]\S*)?\z/iux
|
19
|
+
end
|
9
20
|
end
|
data/lib/url_format/version.rb
CHANGED