Class: SSHScan::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_scan/update.rb

Overview

Handle SSHScan updates.

Instance Method Summary collapse

Constructor Details

#initializeUpdate

Returns a new instance of Update.



9
10
11
# File 'lib/ssh_scan/update.rb', line 9

def initialize
  @errors = []
end

Instance Method Details

#errorsObject



57
58
59
# File 'lib/ssh_scan/update.rb', line 57

def errors
  @errors.uniq
end

#gem_exists?(version = SSHScan::VERSION) ⇒ Boolean

Returns true if the given gem version exists.

Parameters:

  • version (String) (defaults to: SSHScan::VERSION)

    version string

Returns:

  • (Boolean)

    true if given gem exists, else false



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

def gem_exists?(version = SSHScan::VERSION)
  uri = URI("https://rubygems.org/gems/ssh_scan/versions/#{version}")

  begin
    res = Net::HTTP.get_response(uri)
  rescue SocketError => e
    @errors << e.message
    return false
  end

  if res.code != "200"
    return false
  else
    return true
  end
end

#newer_gem_available?(version = SSHScan::VERSION) ⇒ Boolean

Tries to check if the next patch, minor or major version is available or not. If so, returns true.

Parameters:

  • version (String) (defaults to: SSHScan::VERSION)

    version string

Returns:

  • (Boolean)

    true if next major/minor version available, else false



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ssh_scan/update.rb', line 66

def newer_gem_available?(version = SSHScan::VERSION)
  if gem_exists?(next_patch_version(version))
    return true
  end

  if gem_exists?(next_minor_version(version))
    return true
  end

  if gem_exists?(next_major_version(version))
    return true
  end

  return false
end

#next_major_version(version = SSHScan::VERSION) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ssh_scan/update.rb', line 29

def next_major_version(version = SSHScan::VERSION)
  major = version.split(".")[0]
  major_num = major.to_i
  major_num += 1

  return [major_num.to_s, "0", "0"].join(".")
end

#next_minor_version(version = SSHScan::VERSION) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ssh_scan/update.rb', line 21

def next_minor_version(version = SSHScan::VERSION)
  major, minor = version.split(".")[0, 2]
  minor_num = minor.to_i
  minor_num += 1

  return [major, minor_num.to_s, "0"].join(".")
end

#next_patch_version(version = SSHScan::VERSION) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ssh_scan/update.rb', line 13

def next_patch_version(version = SSHScan::VERSION)
  major, minor, patch = version.split(".")
  patch_num = patch.to_i
  patch_num += 1

  return [major, minor, patch_num.to_s].join(".")
end