module Vidibus::Sysinfo::Base

Provides common methods.

Public Instance Methods

call() click to toggle source
# File lib/vidibus/sysinfo/base.rb, line 12
def call
  output, error = perform(command)
  respond(output, error)
end
explain(error) click to toggle source
# File lib/vidibus/sysinfo/base.rb, line 17
def explain(error); end

Private Instance Methods

gigabytes(value, unit = "B") click to toggle source

Converts given amount from unit to gigabytes.

# File lib/vidibus/sysinfo/base.rb, line 63
def gigabytes(value, unit = "B")
  round(megabytes(value, unit) / 1024)
end
megabytes(value, unit = "B") click to toggle source

Converts given amount from unit to megabytes. Treats GB and GiB identically because in our context GB == GiB.

# File lib/vidibus/sysinfo/base.rb, line 51
def megabytes(value, unit = "B")
  value = value.to_f
  case unit
    when 'B'              then value /= 1048576 # bytes
    when 'K', 'KB', 'KiB' then value /= 1024    # kiloytes
    when 'G', 'GB', 'GiB' then value *= 1024    # gigabytes
    when 'T', 'TB', 'TiB' then value *= 1048576 # terabytes
  end
  round(value)
end
open3_reason(error) click to toggle source
# File lib/vidibus/sysinfo/base.rb, line 43
def open3_reason(error)
  if error.match("in `popen3'")
    "the command '#{command}' is not available or this program is not allowed to call it"
  end
end
perform(cmd) click to toggle source
# File lib/vidibus/sysinfo/base.rb, line 21
def perform(cmd)
  response = nil
  Open3.popen3(cmd) do |stdin, stdout, stderr|
    response = [stdout.read, stderr.read]
  end
  response
end
respond(output, error) click to toggle source
# File lib/vidibus/sysinfo/base.rb, line 29
def respond(output, error)
  if error and error != ""
    reason = explain(error) || open3_reason(error) || error
    raise CallError.new("Failed to call #{self}:\n- #{reason}")
  end

  unless output
    raise OutputError.new("Result from call of #{self} is empty")
  end

  parse(output) or
    raise ParseError.new("Result from call of #{self} could not be parsed: #{output.strip}")
end