9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/url_resolver/resolver.rb', line 9
def resolve(url)
url_to_check = URI.escape(url)
cached_url = cache.get_url(url_to_check)
return cached_url if cached_url
response = RestClient.head(url_to_check)
response.args[:url].tap do |final_url|
cache.set_url(url_to_check, final_url)
end
rescue SocketError,
Errno::ETIMEDOUT,
RestClient::InternalServerError,
RestClient::ServiceUnavailable,
RestClient::BadRequest,
RestClient::GatewayTimeout,
RestClient::RequestTimeout,
RestClient::ResourceNotFound,
RestClient::BadGateway,
RestClient::MethodNotAllowed,
RestClient::Forbidden,
URI::InvalidURIError => e
if e.message == 'getaddrinfo: nodename nor servname provided, or not known'
response = RestClient.head(url_to_check) { |response, request, result, &block| response }
url = response.[:location] if response.code == 302 && response.[:location]
end
cache.set_url(url_to_check, url) if UrlResolver.configuration.cache_failures
url
rescue Exception => e
if e.message =~ /undefined method `request_uri'/
cache.set_url(url_to_check, url) if UrlResolver.configuration.cache_failures
url
else
raise UrlResolverError.new("#{e.class.to_s}: #{url} (#{e.message})")
end
end
|