Sha256: 3111979b00f1bb531f726cc3b8d3a1f48718f59bf01ba6fed8a009ad1c84bd3c

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

module Vnstat
  ##
  # A module containing several utility methods responsible for document
  # parsing.
  module Parser
    module_function

    ##
    # Extracts the year and month from the given XML element.
    #
    # @param [Nokogiri::XML::Element] element The XML element.
    # @return [Array<Integer, Integer>] An Array consisting of year and month.
    def extract_month_from_xml_element(element)
      month = element.xpath('date/month').text.to_i
      year = element.xpath('date/year').text.to_i
      [year, month]
    end

    ##
    # Extracts the date from the given XML element.
    #
    # @param [Nokogiri::XML::Element] element The XML element.
    # @return [Date]
    def extract_date_from_xml_element(element)
      day = element.xpath('date/day').text.to_i
      year, month = extract_month_from_xml_element(element)
      Date.new(year, month, day)
    end

    ##
    # Extracts the date and time from the given XML element.
    #
    # @param [Nokogiri::XML::Element] element The XML element.
    # @return [DateTime]
    def extract_datetime_from_xml_element(element)
      date = extract_date_from_xml_element(element)
      hour = element.xpath('time/hour').text.to_i
      minute = element.xpath('time/minute').text.to_i
      DateTime.new(date.year, date.month, date.day, hour, minute)
    end

    ##
    # Extracts the bytes received and sent from the given XML element.
    #
    # @param [Nokogiri::XML::Element] element The XML element.
    # @return [Array<Integer, Integer>] An Array consisting of bytes received
    #   and bytes sent.
    def extract_transmitted_bytes_from_xml_element(element)
      bytes_received = element.xpath('rx').text.to_i * 1024
      bytes_sent = element.xpath('tx').text.to_i * 1024
      [bytes_received, bytes_sent]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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