class Vnstat::InterfaceCollection

A class encapsulating traffic information for all known network interfaces.

Public Class Methods

load_data() click to toggle source

Retrieves the raw XML data for all interfaces.

@return [String]

# File lib/vnstat/interface_collection.rb, line 13
def self.load_data
  Utils.call_executable('--xml')
end

Public Instance Methods

[](id) click to toggle source

Returns traffic information for a certain interface.

@param [String] id The name of the interface. @raise [UnknownInterface] An error that is raised if the

specified interface is not tracked.

@return [Interface]

# File lib/vnstat/interface_collection.rb, line 50
def [](id)
  interfaces_hash.fetch(id.to_s) do
    raise UnknownInterface.new(id.to_s),
          "Unknown interface: #{id}"
  end
end
create(id) click to toggle source

Creates the traffic database for the given interface.

@param [String] id The network interface identifier @return [Interface, nil] The interface that has justed been added to

tracking.
# File lib/vnstat/interface_collection.rb, line 71
def create(id)
  success = Utils.call_executable_returning_status('--create', '-i', id)
  return nil unless success

  reload
  self[id]
end
data=(data) click to toggle source

Sets the raw XML data for the {InterfaceCollection}.

@param [String] data A string representing the document.

Calls superclass method Vnstat::Document#data=
# File lib/vnstat/interface_collection.rb, line 21
def data=(data)
  super
  each { |interface| interface.data = data }
end
each(&block) click to toggle source

Iterates over each interface.

@yieldparam [Interface] interface

# File lib/vnstat/interface_collection.rb, line 61
def each(&block)
  interfaces_hash.each_value(&block)
end
ids() click to toggle source

Returns the names of known interfaces.

@return [Array<String>]

# File lib/vnstat/interface_collection.rb, line 39
def ids
  interfaces_hash.keys
end
inspect() click to toggle source

A human readable representation of the {InterfaceCollection}.

@return [String]

# File lib/vnstat/interface_collection.rb, line 93
def inspect
  "#<#{self.class.name} ids: #{ids.inspect}>"
end
rebuild() click to toggle source

Reset the total traffic counters and recount those using recorded months.

@return [InterfaceCollection]

# File lib/vnstat/interface_collection.rb, line 83
def rebuild
  success = Utils.call_executable_returning_status('--rebuildtotal')
  reload if success
  self
end
reload() click to toggle source

Refreshes data cached in the current instance.

@return [InterfaceCollection]

# File lib/vnstat/interface_collection.rb, line 30
def reload
  self.data = self.class.load_data
  self
end

Private Instance Methods

interfaces_hash() click to toggle source
# File lib/vnstat/interface_collection.rb, line 99
def interfaces_hash
  @interfaces_hash ||= begin
    id_attr = xml_version == '2' ? :name : :id
    elements = data.xpath('//interface')
    elements.each_with_object({}) do |node, hash|
      id = node[id_attr]
      hash[id] = Interface.new(id, data)
    end
  end
end