Sha256: 0df304c3d226fa0164d67330c0327f3accf7d911ebe2ad0fb2e505a3964a89d7

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require 'nokogiri'

module Vnstat
  # A class encapsulating document data.
  #
  # @!attribute [r] data
  #   @return [Nokogiri::XML::Document] The underlying XML document.
  class Document
    ##
    # Initializes the document.
    #
    # @param [String] data The raw XML data.
    def initialize(data)
      self.data = data
    end

    ##
    # @return [Document]
    def self.open(*args)
      new(*args, load_data(*args))
    end

    ##
    # A hook used by {.open} that is intended to be overridden by subclasses.
    #
    # @raise [NotImplementedError]
    def self.load_data(*_args)
      raise NotImplementedError, "Please override #{name}.#{__method__}"
    end

    attr_reader :data

    ##
    # Sets the raw XML data for the {Document}.
    #
    # @param [String] data A string representing the document.
    # @raise [ArgumentError] Raised if the specified data was nil.
    def data=(data)
      raise ArgumentError, 'No document data specified' if data.nil?

      @data = Nokogiri::XML.parse(data.to_s)
    end

    ##
    # Returns the version as specified in the vnstat element.
    #
    # @return [String]
    def version
      data.xpath('vnstat').attr('version').text
    end

    ##
    # Returns the XML version as specified in the vnstat element.
    #
    # @return [String]
    def xml_version
      data.xpath('vnstat').attr('xmlversion').text
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vnstat-ruby-3.0.5 lib/vnstat/document.rb
vnstat-ruby-3.0.4 lib/vnstat/document.rb
vnstat-ruby-3.0.2 lib/vnstat/document.rb
vnstat-ruby-3.0.0 lib/vnstat/document.rb