Module: Vnstat::Parser

Defined in:
lib/vnstat/parser.rb

Overview

A module containing several utility methods responsible for document parsing.

Class Method Summary collapse

Class Method Details

.extract_date_from_xml_element(element) ⇒ Date

Extracts the date from the given XML element.

Parameters:

  • element (Nokogiri::XML::Element)

    The XML element.

Returns:

  • (Date)


26
27
28
29
30
# File 'lib/vnstat/parser.rb', line 26

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

.extract_datetime_from_xml_element(element) ⇒ Time

Extracts the date and time from the given XML element.

Parameters:

  • element (Nokogiri::XML::Element)

    The XML element.

Returns:

  • (Time)


37
38
39
40
41
42
43
# File 'lib/vnstat/parser.rb', line 37

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

.extract_month_from_xml_element(element) ⇒ Array<Integer, Integer>

Extracts the year and month from the given XML element.

Parameters:

  • element (Nokogiri::XML::Element)

    The XML element.

Returns:

  • (Array<Integer, Integer>)

    An Array consisting of year and month.



15
16
17
18
19
# File 'lib/vnstat/parser.rb', line 15

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

.extract_transmitted_bytes_from_xml_element(element) ⇒ Array<Integer, Integer>

Extracts the bytes received and sent from the given XML element.

Parameters:

  • element (Nokogiri::XML::Element)

    The XML element.

Returns:

  • (Array<Integer, Integer>)

    An Array consisting of bytes received and bytes sent.



51
52
53
54
55
# File 'lib/vnstat/parser.rb', line 51

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