config/initializers/url_validator.rb in refinerycms-blog-2.0.5 vs config/initializers/url_validator.rb in refinerycms-blog-2.1.0
- old
+ new
@@ -2,11 +2,11 @@
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
url = value
-
+
# Regex code by 'Arsenic' from http://snippets.dzone.com/posts/show/3654
if url =~ /^
( (https?):\/\/ )?
( [a-z\d]+([\-\.][a-z\d]+)*\.[a-z]{2,6} )
(
@@ -19,22 +19,26 @@
url = "http#{'s' if $7 == '81'}://#{url}" unless $1
else
record.errors[attribute] << 'Not a valid URL'
end
- if options[:verify]
- begin
- url_response = RedirectFollower.new(url).resolve
- url = url_response.url if options[:verify] == [:resolve_redirects]
- rescue RedirectFollower::TooManyRedirects
- record.errors[attribute] << 'URL is redirecting too many times'
- rescue
- record.errors[attribute] << 'could not be resolved'
- end
- end
+ url = resolve_redirects_verify_url(url) if options[:verify]
if options[:update]
value.replace url
+ end
+ end
+
+ def resolve_redirects_verify_url(url)
+ begin
+ url_response = RedirectFollower.new(url).resolve
+ url = url_response.url if options[:verify] == [:resolve_redirects]
+ rescue RedirectFollower::TooManyRedirects
+ record.errors[attribute] << 'URL is redirecting too many times'
+ rescue
+ record.errors[attribute] << 'could not be resolved'
+ ensure
+ url
end
end
end
# Code below written by John Nunemaker