Module: Vnstat::Utils

Defined in:
lib/vnstat/utils.rb

Overview

A module containing several utility methods.

Class Method Summary collapse

Class Method Details

.call_executable(*args) ⇒ String .call_executable(*args) {|error_result| ... } ⇒ Object

Calls the vnstat CLI with the given arguments.

Overloads:

  • .call_executable(*args) ⇒ String

    Returns The output of STDOUT or STDERR, depending of the exit status of the called command.

    Returns:

    • (String)

      The output of STDOUT or STDERR, depending of the exit status of the called command.

  • .call_executable(*args) {|error_result| ... } ⇒ Object

    Returns The value the called block returns.

    Yields:

    • (error_result)

      A block yielded when the called command exited unsuccessfully.

    Yield Parameters:

    • error_result (String)

      The STDERR output.

    Returns:

    • (Object)

      The value the called block returns.

Parameters:

  • args (Array)

    The arguments for the system call.



58
59
60
# File 'lib/vnstat/utils.rb', line 58

def call_executable(*args, &block)
  system_call(Vnstat.config.executable_path, *args, &block)
end

.call_executable_returning_status(*args) ⇒ true, false

Calls the vnstat CLI with the given arguments and returning whether the command has been executed successfully.

Parameters:

  • args (Array<String>)

    The arguments for the system call.

Returns:

  • (true, false)

    Indicates whether the command has been executed successfully (true), or not (false).



69
70
71
# File 'lib/vnstat/utils.rb', line 69

def call_executable_returning_status(*args)
  system_call_returning_status(Vnstat.config.executable_path, *args)
end

.system_call(*args) ⇒ String .system_call(*args) {|error_result| ... } ⇒ Object

Initiates a system call with the given arguments.

Overloads:

  • .system_call(*args) ⇒ String

    Returns The output of STDOUT or STDERR, depending of the exit status of the called command.

    Returns:

    • (String)

      The output of STDOUT or STDERR, depending of the exit status of the called command.

  • .system_call(*args) {|error_result| ... } ⇒ Object

    Returns The value the called block returns.

    Yields:

    • (error_result)

      A block yielded when the called command exited unsuccessfully.

    Yield Parameters:

    • error_result (String)

      The STDERR output.

    Returns:

    • (Object)

      The value the called block returns.

Parameters:

  • args (Array)

    The arguments for the system call.



25
26
27
28
29
30
31
# File 'lib/vnstat/utils.rb', line 25

def system_call(*args)
  result = SystemCall.call(*args)
  return result.success_result if result.success?
  return yield(result.error_result) if block_given?

  result.error_result
end

.system_call_returning_status(*args) ⇒ Boolean

Initiates a system call with the given arguments and returning whether the command has been executed successfully.

Parameters:

  • args (Array)

    The arguments for the system call.

Returns:

  • (Boolean)

    Indicates whether the command has been executed successfully (true), or not (false).



40
41
42
# File 'lib/vnstat/utils.rb', line 40

def system_call_returning_status(*args)
  SystemCall.call(*args).success?
end