Class: WifiWand::Platforms::Mac::SystemNetworkInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/wifi_wand/platforms/mac/system_network_info.rb

Instance Method Summary collapse

Constructor Details

#initialize(command_runner:, wifi_interface_provider:, out_stream_provider: nil, err_stream_provider: nil, verbosity_provider: nil) ⇒ SystemNetworkInfo

Returns a new instance of SystemNetworkInfo.



12
13
14
15
16
17
18
19
# File 'lib/wifi_wand/platforms/mac/system_network_info.rb', line 12

def initialize(command_runner:, wifi_interface_provider:, out_stream_provider: nil,
  err_stream_provider: nil, verbosity_provider: nil)
  @command_runner = command_runner
  @wifi_interface_provider = wifi_interface_provider
  @out_stream_provider = out_stream_provider
  @err_stream_provider = err_stream_provider
  @verbosity_provider = verbosity_provider
end

Instance Method Details

#default_interface(timeout_in_secs: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wifi_wand/platforms/mac/system_network_info.rb', line 64

def default_interface(timeout_in_secs: nil)
  options = { raise_on_error: false }
  options[:timeout_in_secs] = timeout_in_secs if timeout_in_secs

  output = @command_runner.call(%w[route -n get default], **options).stdout
  return nil if output.empty?

  interface_line = output.split("\n").find { |line| line.include?('interface:') }
  return nil unless interface_line

  default_iface = interface_line.split(':', 2).last.strip
  default_iface.empty? ? nil : default_iface
rescue WifiWand::CommandExecutor::OsCommandError
  nil
end

#detect_macos_version(timeout_in_secs: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wifi_wand/platforms/mac/system_network_info.rb', line 84

def detect_macos_version(timeout_in_secs: nil)
  options = {}
  options[:timeout_in_secs] = timeout_in_secs if timeout_in_secs

  output = @command_runner.call(%w[sw_vers -productVersion], **options).stdout
  Helper::Bundle.normalize_detected_macos_version(output)
rescue WifiWand::CommandExecutor::OsCommandError, WifiWand::CommandTimeoutError,
  WifiWand::CommandNotFoundError, WifiWand::CommandSpawnError => e
  err_stream.puts "Could not detect macOS version: #{e.message}." if verbose?
  nil
end

#ipv4_addresses(iface: nil, timeout_in_secs: nil) ⇒ Object



21
22
23
24
# File 'lib/wifi_wand/platforms/mac/system_network_info.rb', line 21

def ipv4_addresses(iface: nil, timeout_in_secs: nil)
  interface_addresses(iface: iface, timeout_in_secs: timeout_in_secs, line_type: 'inet',
    family: :ipv4)
end

#ipv6_addresses(iface: nil, timeout_in_secs: nil) ⇒ Object



26
27
28
29
# File 'lib/wifi_wand/platforms/mac/system_network_info.rb', line 26

def ipv6_addresses(iface: nil, timeout_in_secs: nil)
  interface_addresses(iface: iface, timeout_in_secs: timeout_in_secs, line_type: 'inet6',
    family: :ipv6)
end

#mac_addressObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/wifi_wand/platforms/mac/system_network_info.rb', line 53

def mac_address
  iface = @wifi_interface_provider.call
  output = @command_runner.call(['ifconfig', iface]).stdout
  ether_line = output.split("\n").find { |line| line.include?('ether') }
  return nil unless ether_line

  tokens = ether_line.split
  ether_index = tokens.index('ether')
  ether_index ? tokens[ether_index + 1] : nil
end

#open_resource(resource_url) ⇒ Object



80
81
82
# File 'lib/wifi_wand/platforms/mac/system_network_info.rb', line 80

def open_resource(resource_url)
  @command_runner.call(['open', resource_url])
end

#wifi_on?(iface: nil, timeout_in_secs: nil) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/wifi_wand/platforms/mac/system_network_info.rb', line 44

def wifi_on?(iface: nil, timeout_in_secs: nil)
  iface ||= @wifi_interface_provider.call
  options = {}
  options[:timeout_in_secs] = timeout_in_secs if timeout_in_secs

  output = @command_runner.call(['networksetup', '-getairportpower', iface], **options).stdout
  output.chomp.match?(/\): On$/)
end