Class: Geckodriver::Helper

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

Constant Summary collapse

DRIVER_VERSION =
"v0.24.0".freeze
VERSION =
'0.24.0'

Instance Method Summary collapse

Instance Method Details

#binary_pathObject



60
61
62
63
64
65
66
67
68
# File 'lib/geckodriver/helper.rb', line 60

def binary_path
  exe = if platform.include?('win')
          'geckodriver.exe'
        else
          'geckodriver'
        end

  File.join platform_install_dir, exe
end

#download(hit_network = false) ⇒ Object



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

def download hit_network=false
  return if File.exists?(binary_path) && !hit_network
  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
    unpack_archive(filename)
  end

  raise "Could not unarchive #{filename} to get #{binary_path}" unless File.exists? binary_path
  FileUtils.chmod 'ugo+rx', binary_path
end

#download_urlObject



55
56
57
58
# File 'lib/geckodriver/helper.rb', line 55

def download_url
  extension = platform.include?('win') ? 'zip' : 'tar.gz'
  "https://github.com/mozilla/geckodriver/releases/download/#{DRIVER_VERSION}/geckodriver-#{DRIVER_VERSION}-#{platform}.#{extension}"
end

#install_dirObject



76
77
78
79
80
# File 'lib/geckodriver/helper.rb', line 76

def install_dir
  dir = File.expand_path File.join(ENV['HOME'], '.geckodriver-helper')
  FileUtils.mkdir_p dir
  dir
end

#platformObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/geckodriver/helper.rb', line 82

def platform
  @platform ||= begin
    cfg = RbConfig::CONFIG
    case cfg['host_os']
    when /linux/
      append_64_or_32('linux', cfg)
    when /darwin/
      'macos'
    else
      append_64_or_32('win', cfg)
    end
  end
end

#platform_install_dirObject



70
71
72
73
74
# File 'lib/geckodriver/helper.rb', line 70

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

#run(*args) ⇒ Object



15
16
17
18
# File 'lib/geckodriver/helper.rb', line 15

def run *args
  download
  exec binary_path, *args
end

#unpack_archive(file) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/geckodriver/helper.rb', line 39

def unpack_archive(file)
  case file
  when /\.zip$/
    unzip(file)
  when /\.tar\.gz$/
    io = ungzip(file)
    untar(io)
  else
    raise "Don't know how to unpack #{file}"
  end
end

#updateObject



51
52
53
# File 'lib/geckodriver/helper.rb', line 51

def update
  download true
end