Class: Ifconf::NetworkInterface
- Inherits:
-
Object
- Object
- Ifconf::NetworkInterface
- Defined in:
- lib/ifconf/network_interface.rb
Overview
Represents a single parsed network interface with its flags, addresses, link layer, and statistics.
Constant Summary collapse
- NAME_PATTERN =
/\A[a-zA-Z0-9.:_-]+\z/
Instance Attribute Summary collapse
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#ipv4_config ⇒ Object
readonly
Returns the value of attribute ipv4_config.
-
#ipv6_configs ⇒ Object
readonly
Returns the value of attribute ipv6_configs.
-
#link_layer ⇒ Object
readonly
Returns the value of attribute link_layer.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#statistics ⇒ Object
readonly
Returns the value of attribute statistics.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #all_addresses ⇒ Object
- #encapsulation ⇒ Object
- #error_rate ⇒ Object
- #hash ⇒ Object
- #healthy? ⇒ Boolean
-
#initialize(**attrs) ⇒ NetworkInterface
constructor
A new instance of NetworkInterface.
- #inspect ⇒ Object
- #ipv4_address ⇒ Object
- #ipv6_addresses ⇒ Object
- #loopback? ⇒ Boolean
- #mac_address ⇒ Object
- #mtu ⇒ Object
- #operational? ⇒ Boolean
- #owns_ip?(ip) ⇒ Boolean
- #running? ⇒ Boolean
- #subnet_size ⇒ Object
- #to_s ⇒ Object
- #total_traffic ⇒ Object
- #up? ⇒ Boolean
Constructor Details
#initialize(**attrs) ⇒ NetworkInterface
Returns a new instance of NetworkInterface.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ifconf/network_interface.rb', line 19 def initialize(**attrs) name = attrs.fetch(:name) raise InvalidInterfaceName, name unless name.is_a?(String) && name.match?(NAME_PATTERN) @name = name @flags = attrs.fetch(:flags) @link_layer = attrs.fetch(:link_layer) @ipv4_config = attrs.fetch(:ipv4_config) @ipv6_configs = attrs.fetch(:ipv6_configs).freeze @statistics = attrs.fetch(:statistics) freeze end |
Instance Attribute Details
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
6 7 8 |
# File 'lib/ifconf/network_interface.rb', line 6 def flags @flags end |
#ipv4_config ⇒ Object (readonly)
Returns the value of attribute ipv4_config.
6 7 8 |
# File 'lib/ifconf/network_interface.rb', line 6 def ipv4_config @ipv4_config end |
#ipv6_configs ⇒ Object (readonly)
Returns the value of attribute ipv6_configs.
6 7 8 |
# File 'lib/ifconf/network_interface.rb', line 6 def ipv6_configs @ipv6_configs end |
#link_layer ⇒ Object (readonly)
Returns the value of attribute link_layer.
6 7 8 |
# File 'lib/ifconf/network_interface.rb', line 6 def link_layer @link_layer end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/ifconf/network_interface.rb', line 6 def name @name end |
#statistics ⇒ Object (readonly)
Returns the value of attribute statistics.
6 7 8 |
# File 'lib/ifconf/network_interface.rb', line 6 def statistics @statistics end |
Class Method Details
.build(name:, flags:, link_layer:, statistics:, **options) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/ifconf/network_interface.rb', line 8 def self.build(name:, flags:, link_layer:, statistics:, **) new( name: name, flags: flags, link_layer: link_layer, ipv4_config: [:ipv4_config] || NullIpv4Config.new, ipv6_configs: .fetch(:ipv6_configs, []), statistics: statistics, ) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
107 108 109 |
# File 'lib/ifconf/network_interface.rb', line 107 def ==(other) other.is_a?(NetworkInterface) && @name == other.name end |
#all_addresses ⇒ Object
56 57 58 59 60 61 |
# File 'lib/ifconf/network_interface.rb', line 56 def all_addresses addrs = [] addrs << @ipv4_config.address if @ipv4_config.present? addrs.concat(ipv6_addresses) addrs end |
#encapsulation ⇒ Object
67 68 69 |
# File 'lib/ifconf/network_interface.rb', line 67 def encapsulation @link_layer.encapsulation end |
#error_rate ⇒ Object
79 80 81 |
# File 'lib/ifconf/network_interface.rb', line 79 def error_rate @statistics.error_rate end |
#hash ⇒ Object
113 114 115 |
# File 'lib/ifconf/network_interface.rb', line 113 def hash @name.hash end |
#healthy? ⇒ Boolean
75 76 77 |
# File 'lib/ifconf/network_interface.rb', line 75 def healthy? @statistics.healthy? end |
#inspect ⇒ Object
103 104 105 |
# File 'lib/ifconf/network_interface.rb', line 103 def inspect "#<#{self.class} name=#{@name}>" end |
#ipv4_address ⇒ Object
48 49 50 |
# File 'lib/ifconf/network_interface.rb', line 48 def ipv4_address @ipv4_config.address end |
#ipv6_addresses ⇒ Object
52 53 54 |
# File 'lib/ifconf/network_interface.rb', line 52 def ipv6_addresses @ipv6_configs.map(&:address) end |
#loopback? ⇒ Boolean
44 45 46 |
# File 'lib/ifconf/network_interface.rb', line 44 def loopback? @flags.loopback? end |
#mac_address ⇒ Object
63 64 65 |
# File 'lib/ifconf/network_interface.rb', line 63 def mac_address @link_layer.hardware_addr end |
#mtu ⇒ Object
71 72 73 |
# File 'lib/ifconf/network_interface.rb', line 71 def mtu @flags.mtu end |
#operational? ⇒ Boolean
40 41 42 |
# File 'lib/ifconf/network_interface.rb', line 40 def operational? @flags.operational? end |
#owns_ip?(ip) ⇒ Boolean
87 88 89 90 91 92 |
# File 'lib/ifconf/network_interface.rb', line 87 def owns_ip?(ip) ip_str = ip.to_s return true if @ipv4_config.includes_ip?(ip_str) @ipv6_configs.any? { |c| c.includes_ip?(ip_str) } end |
#running? ⇒ Boolean
36 37 38 |
# File 'lib/ifconf/network_interface.rb', line 36 def running? @flags.running? end |
#subnet_size ⇒ Object
94 95 96 97 |
# File 'lib/ifconf/network_interface.rb', line 94 def subnet_size count = @ipv4_config.host_count count.zero? ? nil : count end |
#to_s ⇒ Object
99 100 101 |
# File 'lib/ifconf/network_interface.rb', line 99 def to_s @name end |
#total_traffic ⇒ Object
83 84 85 |
# File 'lib/ifconf/network_interface.rb', line 83 def total_traffic @statistics.total_bytes end |
#up? ⇒ Boolean
32 33 34 |
# File 'lib/ifconf/network_interface.rb', line 32 def up? @flags.up? end |