Class: DriverDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/chauffeur/driver_downloader.rb

Overview

Downloads and installs browser drivers.

Instance Method Summary collapse

Constructor Details

#initialize(verbose = true) ⇒ DriverDownloader

Returns a new instance of DriverDownloader.



5
6
7
8
9
10
# File 'lib/chauffeur/driver_downloader.rb', line 5

def initialize(verbose = true)
  @verbose = verbose
  config_file_path = "#{Dir.pwd}/drivers/config.yml"
  @data = File.open(config_file_path) { |f| YAML.safe_load(f) }
  @installed_versions = @data['installed_versions']
end

Instance Method Details

#all_driver_versionsObject

Returns all available versions of Chromedriver



43
44
45
# File 'lib/chauffeur/driver_downloader.rb', line 43

def all_driver_versions
  raise 'This class should not be initialized! Please use subclass.'
end

#all_platformsObject

This must be implemented in all child classes!

returns an array of all available platforms for the driver.



29
30
31
# File 'lib/chauffeur/driver_downloader.rb', line 29

def all_platforms
  raise 'This class should not be initialized! Please use subclass.'
end

#browser_nameObject

This must be implemented in all child classes!

Returns a string for the driver name in file structure.



15
16
17
# File 'lib/chauffeur/driver_downloader.rb', line 15

def browser_name
  raise 'This class should not be initialized! Please use subclass.'
end

#download_and_unzip(driver_path, download_url) ⇒ Object

Downloads and installs driver to appropriate path

driver_path: string - path to install driver (must end with '/')
download_url: string - URL of the driver desired


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chauffeur/driver_downloader.rb', line 107

def download_and_unzip(driver_path, download_url)
  FileUtils.mkdir_p(driver_path)
  destination_file_name = "#{driver_path}#{File.basename(download_url)}"
  FileUtils.rm_f destination_file_name
  File.open(destination_file_name, 'wb') do |saved_file|
    saved_file.write(HTTParty.get(download_url, verify: false).parsed_response)
  end
  raise "Could not download #{download_url}" unless File.exist?(destination_file_name)
  extract_and_delete(destination_file_name, driver_path) unless instance_of?(EdgedriverDownloader)
  FileUtils.rm_f "#{destination_file_name}.zip"
end

#driver_download_url(_version, _platform) ⇒ Object

This must be implemented in all child classes!

Returns the url for the desired version of driver version: string - must match exactly the version in the download URL platform: string - must be in all_platforms



52
53
54
# File 'lib/chauffeur/driver_downloader.rb', line 52

def driver_download_url(_version, _platform)
  raise 'This class should not be initialized! Please use subclass.'
end

#driver_urlObject

This must be implemented in all child classes!

returns the string url where all downloads can be located.



22
23
24
# File 'lib/chauffeur/driver_downloader.rb', line 22

def driver_url
  raise 'This class should not be initialized! Please use subclass.'
end

#install_driver(version, platform) ⇒ Object

Downloads and installs driver

version: string   - the exact version number for the download
platform: string  - must match the appropriate platform for
                    the particular driver


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chauffeur/driver_downloader.rb', line 70

def install_driver(version, platform)
  raise unknown_platform_error(platform) unless valid_platform?(platform)
  if @installed_versions[browser_name][platform].eql?(version.to_s)
    puts "Driver version #{version} already installed for #{platform}" if @verbose
    return false
  end
  driver_path = path_for(platform, version)
  download_url = driver_download_url(version, platform)

  puts "installing '#{download_url}' into '#{driver_path}'" if @verbose

  download_and_unzip(driver_path, download_url)
  update_browser_version(platform, version)
end

#install_driver_all_platforms(version) ⇒ Object

Installs the specified version of specified driver for all platforms.



100
101
102
# File 'lib/chauffeur/driver_downloader.rb', line 100

def install_driver_all_platforms(version)
  all_platforms.each { |platform| install_driver(version, platform) }
end

#latest_driver_version(_platform) ⇒ Object

This must be implemented in all child classes!

Returns the most recent version of chromedriver for the desired platform. platform must be one of: linux32, linux64, mac32, win32



38
39
40
# File 'lib/chauffeur/driver_downloader.rb', line 38

def latest_driver_version(_platform)
  raise 'This class should not be initialized! Please use subclass.'
end

#path_for(platform, version) ⇒ Object

Returns the destination folder for the browser driver for the desired platform.

platform: string  - must match the appropriate platform for
                    the particular driver


60
61
62
63
64
# File 'lib/chauffeur/driver_downloader.rb', line 60

def path_for(platform, version)
  raise unknown_platform_error(platform) unless valid_platform?(platform)
  last_part = instance_of?(EdgedriverDownloader) ? version : platform
  "#{Dir.pwd}/drivers/#{browser_name}/#{last_part}/"
end

#upgrade_driver(platform) ⇒ Object

Installs the latest version of chromedriver for the specified platform.

platform: string  - must match the appropriate platform for
                    the particular driver


88
89
90
91
92
# File 'lib/chauffeur/driver_downloader.rb', line 88

def upgrade_driver(platform)
  raise unknown_platform_error(platform) unless valid_platform?(platform)
  latest_version = latest_driver_version(platform)
  install_driver(latest_version, platform)
end

#upgrade_driver_all_platformsObject

Installs the latest version of chromedriver for all platforms.



95
96
97
# File 'lib/chauffeur/driver_downloader.rb', line 95

def upgrade_driver_all_platforms
  all_platforms.each { |platform| upgrade_driver(platform) }
end