10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/rcl/http.rb', line 10
def self.fetch_uri(uri, max_redirects=10)
raise ArgumentError, 'nil uri' if uri.nil?
raise ArgumentError, 'invalid uri class' if uri.class != String
raise ArgumentError, 'empty uri' if uri.empty?
raise ArgumentError, 'nil max_redirects' if max_redirects.nil?
raise ArgumentError, 'max_redirects too deep' if max_redirects == 0
url = URI.parse(uri)
count = -1
found = false
until found
host, port = url.host, url.port if url.host && url.port
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(host, port) { |http| http.request(req) }
res.['location'] \
? url = URI.parse(res.['location']) : found = true
count += 1
raise 'too many redirects' if count >= max_redirects
end
return res, count
end
|