Class: App::Tools

Inherits:
Object
  • Object
show all
Defined in:
lib/core/tools.rb

Constant Summary collapse

REPORT_WIDTH =
188

Class Method Summary collapse

Class Method Details

.ping(ip_or_url, verbose = true) ⇒ Object

Ping a URL or IP and returns the exit status. 0 = success, anything else means it failed.

Returns:

  • Integer



25
26
27
28
29
# File 'lib/core/tools.rb', line 25

def self.ping(ip_or_url, verbose = true)
    App::Terminal::output("Checking if #{App::Terminal::format_highlight(ip_or_url)} is reachable...") if verbose == true
    `ping -t 1 -c 1 #{ip_or_url}`
    $?.exitstatus
end

.this_is_a_macObject

Returns TRUE if Mac, FALSE if Linux (or anything else for that matter)

Returns:

  • boolean



82
83
84
# File 'lib/core/tools.rb', line 82

def self.this_is_a_mac
    return App::Config.param(App::Config::WORKSTATION_OS) == App::Config::MAC
end

.time_passed_since(time_stamp) ⇒ Object

Returns time ago in human readable format.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/core/tools.rb', line 33

def self.time_passed_since(time_stamp)
    unless time_stamp.is_a?(DateTime)
        raise RuntimeError, "Expected DateTime, got: #{time_stamp.class}"
    end
    seconds_ago = seconds_since(time_stamp.strftime('%Y-%m-%dT%H:%M:%S%z'))
    seconds_ago = seconds_ago.to_i
    case seconds_ago
        when 1..119
            return 'A minute ago'
        when 120..2999
            return "#{(seconds_ago / 60).round} minutes ago"
        when 3000..5399
            return 'About an hour ago'
        when 5399..86399
            return "#{((seconds_ago / 60) / 60).round} hours ago"
        when 86400..169999
            return 'A day ago'
        when 170000..2505599
            return "#{(((seconds_ago / 24) / 60) / 60).round} days ago"
        when 2506000..4000000
            return 'A month ago'
        when 4000001..31535999
            return "#{((((seconds_ago / 30.4368) / 24) / 60) / 60).round} months ago"
        when 31536000..47303999
            return 'A year ago'
        when 47304000..9999999999999
            return "#{((((seconds_ago / 365) / 24) / 60) / 60).round} years ago"
        else
            return "Out of range (#{seconds_ago})"
    end
end

.validate_report_width(array) ⇒ Object

Used to make sure all reports are the same width (for consistency).

Returns:

  • void



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/core/tools.rb', line 67

def self.validate_report_width(array)
    unless array.is_a?(Array)
        raise RuntimeError, "Expected Array, got: #{array.class}"
    end
    report_width = 0
    array.each do |pixels|
        report_width = report_width + pixels.to_i
    end
    unless report_width == REPORT_WIDTH
        raise RuntimeError, "Report needs to be #{REPORT_WIDTH} pixels wide, calculated #{report_width} pixels."
    end
end

.verify_internet_accessObject

Check for internet access. Dies if not connected.

Returns:

  • void



11
12
13
14
15
# File 'lib/core/tools.rb', line 11

def self.verify_internet_access
    if ping('www.google.com') != 0
        App::Terminal::error('You are not connected to the internet', 'Please check your connection and try and again.')
    end
end

.verify_vm_is_reachableObject



17
18
19
20
21
# File 'lib/core/tools.rb', line 17

def self.verify_vm_is_reachable
    if ping(App::Config.param(App::Config::VM_IP)) != 0
        App::Terminal::error('Cannot reach VM', ["The remote host #{App::Terminal::format_highlight(App::Config.param(App::Config::VM_IP))} cannot be reached.", 'Please make sure your VM is online and correctly configured.'])
    end
end