Module: FetchAndProcess::HTTP

Defined in:
lib/fetch_and_process/http.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
# 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

Instance Method Details

#http_handlerObject



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
  # 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_agentObject



13
14
15
# File 'lib/fetch_and_process/http.rb', line 13

def user_agent
  "Ruby/FetchAndProcess-#{FetchAndProcess::VERSION} (Net::HTTP)"
end