Class: BetterCap::UpdateChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/update_checker.rb

Overview

bettercap and check if a new one is available.

Class Method Summary collapse

Class Method Details

.checkObject

Check if a new version is available, printing the results in human readable form.



20
21
22
23
24
25
26
27
28
29
# File 'lib/bettercap/update_checker.rb', line 20

def self.check
  ver = self.get_latest_version
  if self.vton( BetterCap::VERSION ) < self.vton( ver )
    Logger.warn "New version '#{ver}' available!"
  else
    Logger.info 'You are running the latest version.'
  end
rescue Exception => e
  Logger.error("Error '#{e.class}' while checking for updates: #{e.message}")
end

.get_latest_versionObject

Fetch the latest program version from rubygems.org API.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bettercap/update_checker.rb', line 41

def self.get_latest_version
  Logger.info 'Checking for updates ...'

  api = URI('https://rubygems.org/api/v1/versions/bettercap/latest.json')
  response = Net::HTTP.get_response(api)

  case response
  when Net::HTTPSuccess
    json = JSON.parse(response.body)
  else
    raise response.message
  end

  return json['version']
end

.vton(v) ⇒ Object

Convert a version string v to a number to be used for comparation.



32
33
34
35
36
37
38
# File 'lib/bettercap/update_checker.rb', line 32

def self.vton v
  vi = 0.0
  v.split('.').reverse.each_with_index do |e,i|
    vi += ( e.to_i * 10**i ) - ( e =~ /[\d+]b/ ? 0.5 : 0 )
  end
  vi
end