The Net::HTTP 2.6.5 white papers have a concrete example of If-Modified-Since mentioned by fooobar.com/questions/1292123 / ...
uri = URI('http://example.com/cached_response') file = File.stat 'cached_response' req = Net::HTTP::Get.new(uri) req['If-Modified-Since'] = file.mtime.rfc2822 res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req) } open 'cached_response', 'w' do |io| io.write res.body end if res.is_a?(Net::HTTPSuccess)
Here is the complete script that actually runs:
#!/usr/bin/env ruby require 'net/http' require 'time' uri = URI('https://upload.wikimedia.org/wikipedia/commons/thumb/9/95/Illumina_iSeq_100_flow_cell_top.jpg/451px-Illumina_iSeq_100_flow_cell_top.jpg') file_path = 'cached_response' req = Net::HTTP::Get.new(uri) if File.file?(file_path) req['If-Modified-Since'] = File.stat(file_path).mtime.rfc2822 end res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) {|http| http.request(req) } if res.is_a? Net::HTTPSuccess File.open(file_path, 'w') {|io| io.write res.body } end
but TODO updates the file every time, although Wikimedia seems to interpret If-Modified-Since : https://wikitech.wikimedia.org/wiki/MediaWiki_caching
source share