Class: Vnstat::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/vnstat/document.rb

Overview

A class encapsulating document data.

Direct Known Subclasses

Interface, InterfaceCollection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Document

Initializes the document.

Parameters:

  • data (String)

    The raw XML data.



15
16
17
# File 'lib/vnstat/document.rb', line 15

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataNokogiri::XML::Document

Returns The underlying XML document.

Returns:

  • (Nokogiri::XML::Document)

    The underlying XML document.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vnstat/document.rb', line 10

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
    attr = data.xpath('vnstat').attr('version')
    raise 'Unable to determine version' if attr.nil?

    attr.text
  end

  ##
  # Returns the XML version as specified in the vnstat element.
  #
  # @return [String]
  def xml_version
    attr = data.xpath('vnstat').attr('xmlversion')
    raise 'Unable to determine XML version' if attr.nil?

    attr.text
  end
end

Class Method Details

.load_data(*_args) ⇒ Object

A hook used by open that is intended to be overridden by subclasses.

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/vnstat/document.rb', line 29

def self.load_data(*_args)
  raise NotImplementedError, "Please override #{name}.#{__method__}"
end

.open(*args) ⇒ Document

Returns:



21
22
23
# File 'lib/vnstat/document.rb', line 21

def self.open(*args)
  new(*args, load_data(*args))
end

Instance Method Details

#versionString

Returns the version as specified in the vnstat element.

Returns:

  • (String)


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

def version
  attr = data.xpath('vnstat').attr('version')
  raise 'Unable to determine version' if attr.nil?

  attr.text
end

#xml_versionString

Returns the XML version as specified in the vnstat element.

Returns:

  • (String)


61
62
63
64
65
66
# File 'lib/vnstat/document.rb', line 61

def xml_version
  attr = data.xpath('vnstat').attr('xmlversion')
  raise 'Unable to determine XML version' if attr.nil?

  attr.text
end