Class: Vnstat::Result::Hour

Inherits:
Vnstat::Result show all
Includes:
DateDelegation, TimeComparable
Defined in:
lib/vnstat/result/hour.rb

Overview

A class representing a tracking result for a specific hour.

Instance Attribute Summary collapse

Attributes inherited from Vnstat::Result

#bytes_received, #bytes_sent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DateDelegation

#day, #month, #year

Methods inherited from Vnstat::Result

#bytes_transmitted

Constructor Details

#initialize(date, hour, bytes_received, bytes_sent) ⇒ Hour

Initializes the Vnstat::Result::Hour.

Parameters:

  • date (Date)

    The date the result was captured on.

  • hour (Integer)

    The hour the result was captured at.

  • bytes_received (Integer)

    The received bytes.

  • bytes_sent (Integer)

    The sent bytes.



25
26
27
28
29
# File 'lib/vnstat/result/hour.rb', line 25

def initialize(date, hour, bytes_received, bytes_sent)
  @date = date
  @hour = hour
  super(bytes_received, bytes_sent)
end

Instance Attribute Details

#dateDate (readonly)

Returns The date the result was captured on.

Returns:

  • (Date)

    The date the result was captured on.



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
59
60
61
62
63
# File 'lib/vnstat/result/hour.rb', line 12

class Hour < Result
  include DateDelegation
  include TimeComparable

  attr_reader :date, :hour

  ##
  # Initializes the {Hour}.
  #
  # @param [Date] date The date the result was captured on.
  # @param [Integer] hour The hour the result was captured at.
  # @param [Integer] bytes_received The received bytes.
  # @param [Integer] bytes_sent The sent bytes.
  def initialize(date, hour, bytes_received, bytes_sent)
    @date = date
    @hour = hour
    super(bytes_received, bytes_sent)
  end

  ##
  # Initializes a {Hour} using the the data contained in the given XML
  # element.
  #
  # @param [Nokogiri::XML::Element] element The XML element.
  # @return [Hour]
  def self.extract_from_xml_element(element)
    date = Parser.extract_date_from_xml_element(element)
    hour = Integer(element.attr('id').to_s)
    new(
      date,
      hour,
      *Parser.extract_transmitted_bytes_from_xml_element(element)
    )
  end

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

    [time, bytes_transmitted] <=> [other.time, other.bytes_transmitted]
  end

  ##
  # The time the result was captured.
  #
  # @return [DateTime]
  def time
    DateTime.new(year, month, day, hour, 0, 0, DateTime.now.offset)
  end
end

#hourInteger (readonly)

Returns The hour the result was captured at.

Returns:

  • (Integer)

    The hour the result was captured at.



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
59
60
61
62
63
# File 'lib/vnstat/result/hour.rb', line 12

class Hour < Result
  include DateDelegation
  include TimeComparable

  attr_reader :date, :hour

  ##
  # Initializes the {Hour}.
  #
  # @param [Date] date The date the result was captured on.
  # @param [Integer] hour The hour the result was captured at.
  # @param [Integer] bytes_received The received bytes.
  # @param [Integer] bytes_sent The sent bytes.
  def initialize(date, hour, bytes_received, bytes_sent)
    @date = date
    @hour = hour
    super(bytes_received, bytes_sent)
  end

  ##
  # Initializes a {Hour} using the the data contained in the given XML
  # element.
  #
  # @param [Nokogiri::XML::Element] element The XML element.
  # @return [Hour]
  def self.extract_from_xml_element(element)
    date = Parser.extract_date_from_xml_element(element)
    hour = Integer(element.attr('id').to_s)
    new(
      date,
      hour,
      *Parser.extract_transmitted_bytes_from_xml_element(element)
    )
  end

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

    [time, bytes_transmitted] <=> [other.time, other.bytes_transmitted]
  end

  ##
  # The time the result was captured.
  #
  # @return [DateTime]
  def time
    DateTime.new(year, month, day, hour, 0, 0, DateTime.now.offset)
  end
end

Class Method Details

.extract_from_xml_element(element) ⇒ Hour

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

Parameters:

  • element (Nokogiri::XML::Element)

    The XML element.

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/vnstat/result/hour.rb', line 37

def self.extract_from_xml_element(element)
  date = Parser.extract_date_from_xml_element(element)
  hour = Integer(element.attr('id').to_s)
  new(
    date,
    hour,
    *Parser.extract_transmitted_bytes_from_xml_element(element)
  )
end

Instance Method Details

#<=>(other) ⇒ Integer?

Returns:

  • (Integer, nil)


49
50
51
52
53
54
# File 'lib/vnstat/result/hour.rb', line 49

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

  [time, bytes_transmitted] <=> [other.time, other.bytes_transmitted]
end

#timeDateTime

The time the result was captured.

Returns:

  • (DateTime)


60
61
62
# File 'lib/vnstat/result/hour.rb', line 60

def time
  DateTime.new(year, month, day, hour, 0, 0, DateTime.now.offset)
end