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.23.0".freeze
VERSION =
'0.23.0'

Instance Method Summary collapse

Instance Method Details

#binary_pathObject


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

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


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

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


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

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


74
75
76
77
78
# File 'lib/geckodriver/helper.rb', line 74

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

#platformObject


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

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


68
69
70
71
72
# File 'lib/geckodriver/helper.rb', line 68

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

#run(*args) ⇒ Object


13
14
15
16
# File 'lib/geckodriver/helper.rb', line 13

def run *args
  download
  exec binary_path, *args
end

#unpack_archive(file) ⇒ Object


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

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


49
50
51
# File 'lib/geckodriver/helper.rb', line 49

def update
  download true
end