Class: Chromedriver::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/chromedriver/helper.rb,
lib/chromedriver/helper/version.rb,
lib/chromedriver/helper/google_code_parser.rb

Defined Under Namespace

Classes: GoogleCodeParser

Constant Summary collapse

VERSION =
"1.2.0"

Instance Method Summary collapse

Instance Method Details

#base_install_dirObject



90
91
92
93
94
95
96
# File 'lib/chromedriver/helper.rb', line 90

def base_install_dir
  @base_install_dir ||= begin
    dir = File.expand_path File.join(ENV['HOME'], ".chromedriver-helper")
    FileUtils.mkdir_p dir
    dir
  end
end

#binary_pathObject



70
71
72
73
74
75
76
# File 'lib/chromedriver/helper.rb', line 70

def binary_path
  if platform == "win"
    File.join platform_install_dir, "chromedriver.exe"
  else
    File.join platform_install_dir, "chromedriver"
  end
end

#current_versionObject



48
49
50
51
52
# File 'lib/chromedriver/helper.rb', line 48

def current_version
  @current_version ||= if File.exist?(version_path)
                         File.read(version_path).strip
                       end
end

#download(hit_network = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chromedriver/helper.rb', line 19

def download(hit_network = false)
  return if File.exist?(binary_path) && !hit_network

  raise "Version not found for #{download_version}" unless download_url

  filename = File.basename download_url
  Dir.chdir platform_install_dir do
    FileUtils.rm_f filename
    File.open(filename, "wb") do |saved_file|
      URI.parse(download_url).open("rb") do |read_file|
        saved_file.write(read_file.read)
      end
    end

    raise "Could not download #{download_url}" unless File.exists? filename
    Archive::Zip.extract(filename, '.', :overwrite => :all)
  end
  raise "Could not unzip #{filename} to get #{binary_path}" unless File.exists? binary_path
  FileUtils.chmod "ugo+rx", binary_path
  File.open(version_path, 'w') { |file| file.write(download_version) }
end

#download_urlObject



58
59
60
# File 'lib/chromedriver/helper.rb', line 58

def download_url
  @download_url ||= google_code_parser.version_download_url(download_version)
end

#download_versionObject



54
55
56
# File 'lib/chromedriver/helper.rb', line 54

def download_version
  @download_version ||= current_version || google_code_parser.newest_download_version.to_s
end

#google_code_parserObject



62
63
64
# File 'lib/chromedriver/helper.rb', line 62

def google_code_parser
  @google_code_parser ||= GoogleCodeParser.new(platform)
end

#platformObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chromedriver/helper.rb', line 98

def platform
  cfg = RbConfig::CONFIG
  host_cpu = cfg["host_cpu"]
  host_os = cfg["host_os"]

  case host_os
  when /linux/ then
    host_cpu =~ /x86_64|amd64/ ? "linux64" : "linux32"
  when /darwin/ then "mac"
  when /mswin/ then "win"
  when /mingw/ then "win"
  else
    raise("Unsupported host OS '#{host_os}'")
  end
end

#platform_install_dirObject



78
79
80
81
82
# File 'lib/chromedriver/helper.rb', line 78

def platform_install_dir
  dir = File.join version_install_dir, platform
  FileUtils.mkdir_p dir
  dir
end

#run(*args) ⇒ Object



14
15
16
17
# File 'lib/chromedriver/helper.rb', line 14

def run(*args)
  download
  exec binary_path, *args
end

#update(version = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/chromedriver/helper.rb', line 41

def update(version = nil)
  @download_version = version || google_code_parser.newest_download_version.to_s

  hit_network = (current_version != download_version) ? true : false
  download(hit_network)
end

#version_install_dirObject



84
85
86
87
88
# File 'lib/chromedriver/helper.rb', line 84

def version_install_dir
  dir = File.expand_path File.join(base_install_dir, download_version)
  FileUtils.mkdir_p dir
  dir
end

#version_pathObject



66
67
68
# File 'lib/chromedriver/helper.rb', line 66

def version_path
  @version_path ||= File.join(base_install_dir, ".chromedriver-version")
end