Class: Vnstat::Document
- Inherits:
-
Object
- Object
- Vnstat::Document
- Defined in:
- lib/vnstat/document.rb
Overview
A class encapsulating document data.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Nokogiri::XML::Document
The underlying XML document.
Class Method Summary collapse
-
.load_data(*_args) ⇒ Object
A hook used by Document.open that is intended to be overridden by subclasses.
- .open(*args) ⇒ Document
Instance Method Summary collapse
-
#initialize(data) ⇒ Document
constructor
Initializes the document.
-
#version ⇒ String
Returns the version as specified in the vnstat element.
-
#xml_version ⇒ String
Returns the XML version as specified in the vnstat element.
Constructor Details
#initialize(data) ⇒ Document
Initializes the document.
15 16 17 |
# File 'lib/vnstat/document.rb', line 15 def initialize(data) self.data = data end |
Instance Attribute Details
#data ⇒ Nokogiri::XML::Document
Returns 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.
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
21 22 23 |
# File 'lib/vnstat/document.rb', line 21 def self.open(*args) new(*args, load_data(*args)) end |
Instance Method Details
#version ⇒ String
Returns the version as specified in the vnstat element.
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_version ⇒ String
Returns the XML version as specified in the vnstat element.
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 |