module FetchAndProcess::HTTP

Public Class Methods

included(base) click to toggle source
# File lib/fetch_and_process/http.rb, line 8
def self.included(base)
  base.add_handler('http', :http_handler)
  base.add_handler('https', :http_handler)
end

Public Instance Methods

http_handler() click to toggle source
# 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
  # Ensure there's no empty / partial file to foul up the cache
  FileUtils.rm cache_location if File.exist?(cache_location)
  raise e
end
user_agent() click to toggle source
# File lib/fetch_and_process/http.rb, line 13
def user_agent
  "Ruby/FetchAndProcess-#{FetchAndProcess::VERSION} (Net::HTTP)"
end