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
|
# File 'lib/fetch_and_process/http.rb', line 17
def http_handler
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.port == 443) do |http|
req = Net::HTTP::Get.new(uri)
req['If-Modified-Since'] = File.mtime(cache_location).rfc2822 if File.exist?(cache_location)
req['User-Agent'] = user_agent
http.request(req) do |response|
case response
when Net::HTTPSuccess
File.open(cache_location, 'w') do |file|
response.read_body do |chunk|
file.write(chunk)
end
end
when Net::HTTPNotModified
logger.debug 'Using cached file'
else
raise response.message
end
end
cache_location
end
rescue StandardError => e
FileUtils.rm cache_location if File.exist?(cache_location)
raise e
end
|