Class: Vnstat::InterfaceCollection

Inherits:
Document
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vnstat/interface_collection.rb

Overview

A class encapsulating traffic information for all known network interfaces.

Instance Attribute Summary

Attributes inherited from Document

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

#initialize, open, #version, #xml_version

Constructor Details

This class inherits a constructor from Vnstat::Document

Class Method Details

.load_dataString

Retrieves the raw XML data for all interfaces.

Returns:

  • (String)


13
14
15
# File 'lib/vnstat/interface_collection.rb', line 13

def self.load_data
  Utils.call_executable('--xml')
end

Instance Method Details

#[](id) ⇒ Interface

Returns traffic information for a certain interface.

Parameters:

  • id (String)

    The name of the interface.

Returns:

Raises:

  • (UnknownInterface)

    An error that is raised if the specified interface is not tracked.



50
51
52
53
54
55
# 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) ⇒ Interface?

Creates the traffic database for the given interface.

Parameters:

  • id (String)

    The network interface identifier

Returns:

  • (Interface, nil)

    The interface that has justed been added to tracking.



71
72
73
74
75
76
77
# 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) ⇒ Object

Sets the raw XML data for the Vnstat::InterfaceCollection.

Parameters:

  • data (String)

    A string representing the document.



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

def data=(data)
  super
  each { |interface| interface.data = data }
end

#each {|interface| ... } ⇒ Object

Iterates over each interface.

Yield Parameters:



61
62
63
# File 'lib/vnstat/interface_collection.rb', line 61

def each(&block)
  interfaces_hash.each_value(&block)
end

#idsArray<String>

Returns the names of known interfaces.

Returns:

  • (Array<String>)


39
40
41
# File 'lib/vnstat/interface_collection.rb', line 39

def ids
  interfaces_hash.keys
end

#inspectString

A human readable representation of the Vnstat::InterfaceCollection.

Returns:

  • (String)


93
94
95
# File 'lib/vnstat/interface_collection.rb', line 93

def inspect
  "#<#{self.class.name} ids: #{ids.inspect}>"
end

#rebuildInterfaceCollection

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

Returns:



83
84
85
86
87
# File 'lib/vnstat/interface_collection.rb', line 83

def rebuild
  success = Utils.call_executable_returning_status('--rebuildtotal')
  reload if success
  self
end

#reloadInterfaceCollection

Refreshes data cached in the current instance.

Returns:



30
31
32
33
# File 'lib/vnstat/interface_collection.rb', line 30

def reload
  self.data = self.class.load_data
  self
end