Class: Vnstat::Result

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/vnstat/result.rb,
lib/vnstat/result/day.rb,
lib/vnstat/result/hour.rb,
lib/vnstat/result/month.rb,
lib/vnstat/result/minute.rb,
lib/vnstat/result/date_delegation.rb,
lib/vnstat/result/time_comparable.rb

Overview

A class representing a tracking result.

Direct Known Subclasses

Day, Hour, Minute, Month

Defined Under Namespace

Modules: DateDelegation, TimeComparable Classes: Day, Hour, Minute, Month

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes_received, bytes_sent) ⇒ Result

Initializes the Vnstat::Result.

Parameters:

  • bytes_received (Integer)

    The received bytes.

  • bytes_sent (Integer)

    The sent bytes.



28
29
30
31
# File 'lib/vnstat/result.rb', line 28

def initialize(bytes_received, bytes_sent)
  @bytes_received = bytes_received
  @bytes_sent = bytes_sent
end

Instance Attribute Details

#bytes_receivedInteger (readonly)

Returns The received bytes.

Returns:

  • (Integer)

    The received bytes.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/vnstat/result.rb', line 11

class Result
  autoload :DateDelegation, 'vnstat/result/date_delegation'
  autoload :Day, 'vnstat/result/day'
  autoload :Hour, 'vnstat/result/hour'
  autoload :Minute, 'vnstat/result/minute'
  autoload :Month, 'vnstat/result/month'
  autoload :TimeComparable, 'vnstat/result/time_comparable'

  include Comparable

  attr_reader :bytes_received, :bytes_sent

  ##
  # Initializes the {Result}.
  #
  # @param [Integer] bytes_received The received bytes.
  # @param [Integer] bytes_sent The sent bytes.
  def initialize(bytes_received, bytes_sent)
    @bytes_received = bytes_received
    @bytes_sent = bytes_sent
  end

  ##
  # Initializes a {Result} using the the data contained in the given XML
  # element.
  #
  # @param [Nokogiri::XML::Element] element The XML element.
  # @return [Result]
  def self.extract_from_xml_element(element)
    new(*Parser.extract_transmitted_bytes_from_xml_element(element))
  end

  ##
  # The transmitted bytes (both sent and received).
  #
  # @return [Integer]
  def bytes_transmitted
    bytes_received + bytes_sent
  end

  ##
  # @return [Integer, nil]
  def <=>(other)
    return nil unless other.respond_to?(:bytes_transmitted)

    bytes_transmitted <=> other.bytes_transmitted
  end
end

#bytes_sentInteger (readonly)

Returns The sent bytes.

Returns:

  • (Integer)

    The sent bytes.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/vnstat/result.rb', line 11

class Result
  autoload :DateDelegation, 'vnstat/result/date_delegation'
  autoload :Day, 'vnstat/result/day'
  autoload :Hour, 'vnstat/result/hour'
  autoload :Minute, 'vnstat/result/minute'
  autoload :Month, 'vnstat/result/month'
  autoload :TimeComparable, 'vnstat/result/time_comparable'

  include Comparable

  attr_reader :bytes_received, :bytes_sent

  ##
  # Initializes the {Result}.
  #
  # @param [Integer] bytes_received The received bytes.
  # @param [Integer] bytes_sent The sent bytes.
  def initialize(bytes_received, bytes_sent)
    @bytes_received = bytes_received
    @bytes_sent = bytes_sent
  end

  ##
  # Initializes a {Result} using the the data contained in the given XML
  # element.
  #
  # @param [Nokogiri::XML::Element] element The XML element.
  # @return [Result]
  def self.extract_from_xml_element(element)
    new(*Parser.extract_transmitted_bytes_from_xml_element(element))
  end

  ##
  # The transmitted bytes (both sent and received).
  #
  # @return [Integer]
  def bytes_transmitted
    bytes_received + bytes_sent
  end

  ##
  # @return [Integer, nil]
  def <=>(other)
    return nil unless other.respond_to?(:bytes_transmitted)

    bytes_transmitted <=> other.bytes_transmitted
  end
end

Class Method Details

.extract_from_xml_element(element) ⇒ Result

Initializes a Vnstat::Result using the the data contained in the given XML element.

Parameters:

  • element (Nokogiri::XML::Element)

    The XML element.

Returns:



39
40
41
# File 'lib/vnstat/result.rb', line 39

def self.extract_from_xml_element(element)
  new(*Parser.extract_transmitted_bytes_from_xml_element(element))
end

Instance Method Details

#<=>(other) ⇒ Integer?

Returns:

  • (Integer, nil)


53
54
55
56
57
# File 'lib/vnstat/result.rb', line 53

def <=>(other)
  return nil unless other.respond_to?(:bytes_transmitted)

  bytes_transmitted <=> other.bytes_transmitted
end

#bytes_transmittedInteger

The transmitted bytes (both sent and received).

Returns:

  • (Integer)


47
48
49
# File 'lib/vnstat/result.rb', line 47

def bytes_transmitted
  bytes_received + bytes_sent
end