Sha256: b8fbf2a67110cd754c83d3bc157c3a6628bb09ff5869f1179ba565fb8498be27

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

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 [Time]
    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
      offset = Time.now.strftime('%:z')
      Time.new(date.year, date.month, date.day, hour, minute, 0, offset)
    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

2 entries across 2 versions & 1 rubygems

Version Path
vnstat-ruby-3.0.7 lib/vnstat/parser.rb
vnstat-ruby-3.0.6 lib/vnstat/parser.rb