From f21691efb5de2b6cafa64b07b8e0fb2cb399244d Mon Sep 17 00:00:00 2001 From: Philip Arndt Date: Thu, 29 Nov 2012 08:54:50 +1300 Subject: Prevent post_spec.rb from hanging the entire spec run for 30 seconds while it tries to retrieve external resources. --- config/initializers/url_validator.rb | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'config') diff --git a/config/initializers/url_validator.rb b/config/initializers/url_validator.rb index d956d04..e9dcb89 100644 --- a/config/initializers/url_validator.rb +++ b/config/initializers/url_validator.rb @@ -4,7 +4,7 @@ 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?):\/\/ )? @@ -21,21 +21,25 @@ $/ix 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 -- cgit v1.2.3