Module: Chromedriver::Binary::DownloaderHelper

Included in:
ChromedriverDownloader, VersionResolver
Defined in:
lib/chromedriver/binary/downloader_helper.rb

Instance Method Summary collapse

Instance Method Details

#download_file(url, destination) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chromedriver/binary/downloader_helper.rb', line 13

def download_file(url, destination)
  uri = URI.parse(url)
  logger = Chromedriver::Binary.logger
  http = create_http(uri)

  logger.debug("Saving to: #{destination}")

  exec_get_request(http, uri) do |response|
    handle_unsuccessful_response(response)
    write_response_to_file(response, destination, logger)
  end
rescue StandardError => e
  logger.debug("Error: #{e.message}")
  raise "Error downloading file: #{e.message}"
end

#exec_get_request(http, uri, &block) ⇒ Object



29
30
31
32
33
34
# File 'lib/chromedriver/binary/downloader_helper.rb', line 29

def exec_get_request(http, uri, &block)
  request = Net::HTTP::Get.new(uri)
  Chromedriver::Binary.logger.debug("Sending HTTP GET request...")

  http.request(request, &block)
end

#extract_zip(zip_file, destination) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chromedriver/binary/downloader_helper.rb', line 36

def extract_zip(zip_file, destination)
  Zip::File.open(zip_file) do |zip|
    zip.each do |entry|
      # Extract all files as top-level (ignoring any folder structure)
      destination_file = File.join(destination, File.basename(entry.name))
      entry.extract(destination_file) { true }
    end
  end
rescue StandardError => e
  raise "Error extracting ZIP file: #{e.message}"
end