Class: Webdrivers::Common

Inherits:
Object
  • Object
show all
Defined in:
lib/webdrivers/common.rb

Direct Known Subclasses

Chromedriver, Edgedriver, Geckodriver, IEDriver, PhantomJS

Class Method Summary collapse

Class Method Details

.binary_pathObject



74
75
76
# File 'lib/webdrivers/common.rb', line 74

def binary_path
  File.join platform_install_dir, file_name
end

.decompress_file(filename) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/webdrivers/common.rb', line 35

def decompress_file(filename)
  case filename
  when /tar\.gz$/
    untargz_file(filename)
  when /tar\.bz2$/
    system "tar xjf #{filename}"
    filename.gsub('.tar.bz2', '')
  when /\.zip$/
    unzip_file(filename)
  end
end

.downloadObject

Raises:

  • (StandardError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/webdrivers/common.rb', line 13

def download
  return if File.exists?(binary_path) && !internet_connection?
  raise StandardError, "Unable to Reach #{base_url}" unless internet_connection?
  return if newest_version == current_version

  url = download_url
  filename = File.basename url
  Dir.chdir platform_install_dir do
    FileUtils.rm_f filename
    File.open(filename, "wb") do |saved_file|
      URI.parse(url).open("rb") do |read_file|
        saved_file.write(read_file.read)
      end
    end
    raise "Could not download #{url}" unless File.exists? filename
    dcf = decompress_file(filename)
    extract_file(dcf) if respond_to? :extract_file
  end
  raise "Could not unzip #{filename} to get #{binary_path}" unless File.exists? binary_path
  FileUtils.chmod "ugo+rx", binary_path
end

.download_url(version = nil) ⇒ Object



70
71
72
# File 'lib/webdrivers/common.rb', line 70

def download_url(version = nil)
  downloads[version || newest_version]
end

.install(*args) ⇒ Object



8
9
10
11
# File 'lib/webdrivers/common.rb', line 8

def install *args
  download
  exec binary_path, *args
end

.install_dirObject



82
83
84
# File 'lib/webdrivers/common.rb', line 82

def install_dir
  File.expand_path(File.join(ENV['HOME'], ".webdrivers")).tap { |dir| FileUtils.mkdir_p dir }
end

.internet_connection?Boolean



98
99
100
101
102
# File 'lib/webdrivers/common.rb', line 98

def internet_connection?
  true #if open(base_url)
rescue
  false
end

.platformObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/webdrivers/common.rb', line 86

def 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

.platform_install_dirObject



78
79
80
# File 'lib/webdrivers/common.rb', line 78

def platform_install_dir
  File.join(install_dir, platform).tap { |dir| FileUtils.mkdir_p dir }
end

.untargz_file(filename) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/webdrivers/common.rb', line 47

def untargz_file(filename)
  tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(filename))
  tar_extract.rewind

  ucf = File.open(file_name, "w+")
  tar_extract.each { |entry| ucf << entry.read }
  ucf.close
  File.basename ucf
end

.unzip_file(filename) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/webdrivers/common.rb', line 57

def unzip_file(filename)
  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)
      FileUtils.rm_rf(f_path) if File.exist?(f_path)
      FileUtils.mkdir_p(File.dirname(f_path)) unless File.exist?(File.dirname(f_path))
      zip_file.extract(f, f_path)
    end
  end
  @top_path
end