Class: BetterCap::UpdateChecker

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

Overview

This class is responsible for fetching the latest version of 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.


24
25
26
27
28
29
30
31
32
33
# File 'lib/bettercap/update_checker.rb', line 24

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.


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bettercap/update_checker.rb', line 45

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.


36
37
38
39
40
41
42
# File 'lib/bettercap/update_checker.rb', line 36

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