Module: Chauffeur::PathExpander

Defined in:
lib/chauffeur/path_expander.rb

Overview

Adds locations of drivers for the current platform to the beginning of the path

Class Method Summary collapse

Class Method Details

.current_chromedriver_platformObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/chauffeur/path_expander.rb', line 19

def self.current_chromedriver_platform
  case RbConfig::CONFIG['host_os']
  when 'mingw32'
    'win32'
  when 'mac', /darwin/
    'mac32'
  else
    RbConfig::CONFIG['host_cpu'].eql?('x86_64') ? 'linux64' : 'linux32'
  end
end

.current_geckodriver_platformObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/chauffeur/path_expander.rb', line 36

def self.current_geckodriver_platform
  case RbConfig::CONFIG['host_os']
  when 'mingw32'
    ENV['architecture'].eql?('64') ? 'win64' : 'win32'
  when 'mac', /darwin/
    'macos'
  else
    RbConfig::CONFIG['host_cpu'].eql?('x86_64') ? 'linux64' : 'linux32'
  end
end

.current_iedriver_platformObject



51
52
53
# File 'lib/chauffeur/path_expander.rb', line 51

def self.current_iedriver_platform
  ENV['architecture'].eql?('64') ? 'x64' : 'Win32'
end

.format_for_platform(path, platform = nil) ⇒ Object

Takes a file path and puts the appropriate slashes for the operating system.



63
64
65
66
67
68
69
70
71
72
# File 'lib/chauffeur/path_expander.rb', line 63

def self.format_for_platform(path, platform = nil)
  host_os = platform || RbConfig::CONFIG['host_os']
  if host_os.eql?('mingw32')
    path.tr('/', '\\')
  elsif host_os.include?('linux') || host_os.include?('mac') || host_os.include?('darwin')
    path.tr('\\', '/')
  else
    raise UnknownPlatformError, platform
  end
end

.make_driver_executable(file_path) ⇒ Object



90
91
92
93
# File 'lib/chauffeur/path_expander.rb', line 90

def self.make_driver_executable(file_path)
  return if windows?
  File.chmod(0o0777, file_path) if File.exist?(file_path)
end

.path_to_chromedriver(platform = nil) ⇒ Object



13
14
15
16
17
# File 'lib/chauffeur/path_expander.rb', line 13

def self.path_to_chromedriver(platform = nil)
  driver_path = "#{Dir.pwd}/drivers/chromedriver/#{platform || current_chromedriver_platform}"
  make_driver_executable("#{driver_path}/chromedriver")
  driver_path
end

.path_to_edgedriver(platform = nil) ⇒ Object



55
56
57
58
59
# File 'lib/chauffeur/path_expander.rb', line 55

def self.path_to_edgedriver(platform = nil)
  return nil unless windows_10?
  p = platform || windows_build_number
  "#{Dir.pwd}/drivers/microsoft_webdriver/#{p}"
end

.path_to_geckodriver(platform = nil) ⇒ Object



30
31
32
33
34
# File 'lib/chauffeur/path_expander.rb', line 30

def self.path_to_geckodriver(platform = nil)
  driver_path = "#{Dir.pwd}/drivers/geckodriver/#{platform || current_geckodriver_platform}"
  make_driver_executable("#{driver_path}/geckodriver")
  driver_path
end

.path_to_iedriver(platform = nil) ⇒ Object



47
48
49
# File 'lib/chauffeur/path_expander.rb', line 47

def self.path_to_iedriver(platform = nil)
  windows? ? "#{Dir.pwd}/drivers/iedriver/#{platform || current_iedriver_platform}" : nil
end

.paths_to_addObject



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

def self.paths_to_add
  all_paths = [path_to_chromedriver,
               path_to_geckodriver,
               path_to_iedriver,
               path_to_edgedriver].compact
  # paths = all_paths.compact.join(Gem.path_separator) + Gem.path_separator
  all_paths.map { |p| format_for_platform(p) }
end

.windows?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/chauffeur/path_expander.rb', line 74

def self.windows?
  RbConfig::CONFIG['host_os'].eql?('mingw32')
end

.windows_10?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/chauffeur/path_expander.rb', line 78

def self.windows_10?
  windows? && windows_version.eql?('Version 10')
end

.windows_build_numberObject



86
87
88
# File 'lib/chauffeur/path_expander.rb', line 86

def self.windows_build_number
  `ver`.match(/\.(\d+)\]/)[1]
end

.windows_versionObject



82
83
84
# File 'lib/chauffeur/path_expander.rb', line 82

def self.windows_version
  `ver`.match(/(Version \d+)/)[1]
end