module WebDriverManager::Support

Public Instance Methods

latest_binary() click to toggle source
# File lib/webdriver_manager/support.rb, line 31
def latest_binary
  driver_binary_list.keys.max
end
provision() click to toggle source
# File lib/webdriver_manager/support.rb, line 3
def provision
  unless driver_url_is_reachable?
    return current_binary.nil? ? nil : driver_binary
  end

  return driver_binary if current_binary == latest_binary

  remove_binary && provision_driver
end
provision_driver(version = nil) click to toggle source
# File lib/webdriver_manager/support.rb, line 13
def provision_driver(version = nil)
  url, filename = driver_filename(version)
  Dir.mkdir(driver_repo) unless File.exist?(driver_repo)

  Dir.chdir(driver_repo) do
    download_driver(filename, url)
    decompress_driver(filename)
  end

  set_driver_permissions
  driver_binary
end
remove_binary() click to toggle source
# File lib/webdriver_manager/support.rb, line 26
def remove_binary
  WebDriverManager.logger.debug("Deleting #{driver_binary}")
  FileUtils.rm_f(driver_binary)
end

Protected Instance Methods

get(url) click to toggle source
# File lib/webdriver_manager/support.rb, line 37
def get(url)
  response = Net::HTTP.get_response(URI(url))

  case response
    when Net::HTTPSuccess then response.body
    when Net::HTTPRedirection
      location = response['location']
      WebDriverManager.logger.debug("Redirected to #{location}")
      get(location)
  end
end

Private Instance Methods

decompress_driver(filename) click to toggle source
# File lib/webdriver_manager/support.rb, line 62
def decompress_driver(filename)
  dcf = decompress_file(filename)
  WebDriverManager.logger.debug("Decompression Complete")

  if dcf
    WebDriverManager.logger.debug("Deleting #{filename}")
    FileUtils.rm_f(filename)
  end

  return if File.exist?(driver_binary)
  raise "Unable to decompress #{filename} to get #{driver_binary}"
end
decompress_file(filename) click to toggle source
# File lib/webdriver_manager/support.rb, line 75
def decompress_file(filename)
  case filename
    when /tar\.gz$/
      WebDriverManager.logger.debug("Decompressing tar")
      untargz_file(filename)
    when /\.zip$/
      WebDriverManager.logger.debug("Decompressing zip")
      unzip_file(filename)
  end
end
download_driver(filename, url) click to toggle source
# File lib/webdriver_manager/support.rb, line 51
def download_driver(filename, url)
  FileUtils.rm_f(filename)

  open(filename, "wb") do |file|
    file.print(get(url))
  end

  raise "Unable to download #{url}" unless File.exist?(filename)
  WebDriverManager.logger.debug("Successfully downloaded #{filename}")
end
driver_binary() click to toggle source
# File lib/webdriver_manager/support.rb, line 144
def driver_binary
  File.join(driver_repo, driver_name)
end
driver_download_url(version) click to toggle source
# File lib/webdriver_manager/support.rb, line 140
def driver_download_url(version)
  driver_binary_list[version || latest_binary]
end
driver_filename(version) click to toggle source
# File lib/webdriver_manager/support.rb, line 117
def driver_filename(version)
  url = driver_download_url(version)
  filename = File.basename(url)
  [url, filename]
end
driver_is_downloaded?() click to toggle source
# File lib/webdriver_manager/support.rb, line 134
def driver_is_downloaded?
  result = File.exist?(driver_binary)
  WebDriverManager.logger.debug("Driver Already Downloaded: #{result}")
  result
end
driver_repo() click to toggle source
# File lib/webdriver_manager/support.rb, line 148
def driver_repo
  File.expand_path(File.join(ENV['HOME'], ".webdrivers")).tap do |dir|
    FileUtils.mkdir_p(dir)
  end
end
driver_url_is_reachable?() click to toggle source
# File lib/webdriver_manager/support.rb, line 123
def driver_url_is_reachable?
  get(driver_base_url)
  WebDriverManager.logger.debug("Driver URL Available: #{driver_base_url}")
  true
rescue StandardError
  WebDriverManager.logger.debug(
    "Driver URL Not Available: #{driver_base_url}"
  )
  false
end
os_platform() click to toggle source
# File lib/webdriver_manager/support.rb, line 154
def os_platform
  cfg = RbConfig::CONFIG
  case cfg['host_os']
    when /linux/
      cfg['host_cpu'] =~ /x86_64|amd64/ ? "linux64" : "linux32"
    when /darwin/
      "mac"
    else
      "win"
  end
end
set_driver_permissions() click to toggle source
# File lib/webdriver_manager/support.rb, line 110
def set_driver_permissions
  FileUtils.chmod("ugo+rx", driver_binary)
  WebDriverManager.logger.debug(
    "Completed download and processing of #{driver_binary}"
  )
end
untargz_file(filename) click to toggle source
# File lib/webdriver_manager/support.rb, line 101
def untargz_file(filename)
  require "rubygems/package"
  tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(filename))
  file = File.open(driver_name, "w+b")
  tar_extract.each { |entry| file << entry.read }
  file.close
  File.basename(file)
end
unzip_file(filename) click to toggle source
# File lib/webdriver_manager/support.rb, line 86
def unzip_file(filename)
  require "zip"
  Zip::File.open("#{Dir.pwd}/#{filename}") do |zip_file|
    zip_file.each do |f|
      @top_path ||= f.name
      f_path = File.join(Dir.pwd, f.name)

      remove_binary

      zip_file.extract(f, f_path)
    end
  end
  @top_path
end