Sha256: 4534f1d5c7bd04a3f8679c3334c756d8666860820b917a49b91555a1304cbff7

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

module Vnstat
  class Result
    ##
    # A class representing a tracking result for a specific hour.
    #
    # @attr_reader [Date] date The date the result was captured on.
    # @attr_reader [Integer] hour The hour the result was captured at.
    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)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vnstat-ruby-1.0.2 lib/vnstat/result/hour.rb
vnstat-ruby-1.0.1 lib/vnstat/result/hour.rb
vnstat-ruby-1.0.0 lib/vnstat/result/hour.rb