Class: WifiWand::Platforms::Mac::SystemProfilerWifiDataNavigator

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

Constant Summary collapse

LOCAL_NETWORKS_KEY =
'spairport_airport_local_wireless_networks'
OTHER_LOCAL_NETWORKS_KEY =
'spairport_airport_other_local_wireless_networks'
INTERFACES_KEY =
'spairport_airport_interfaces'
CURRENT_NETWORK_KEY =
'spairport_current_network_information'
SIGNAL_NOISE_KEY =
'spairport_signal_noise'
SECURITY_MODE_KEY =
'spairport_security_mode'
PLACEHOLDER_NETWORK_NAMES =
%w[<hidden> <redacted>].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ SystemProfilerWifiDataNavigator

Returns a new instance of SystemProfilerWifiDataNavigator.



88
89
90
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 88

def initialize(data)
  @data = data
end

Class Method Details

.associated?(interface_data) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 20

def self.associated?(interface_data)
  return false unless interface_data

  current_network = interface_data[CURRENT_NETWORK_KEY]
  return true if current_network.is_a?(Hash) && !current_network.empty?
  return true if !current_network.is_a?(Hash) && current_network && !current_network.to_s.empty?

  false
end

.current_network_name(interface_data, include_placeholder: false) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 30

def self.current_network_name(interface_data, include_placeholder: false)
  current_network = interface_data&.fetch(CURRENT_NETWORK_KEY, nil)
  return nil unless current_network

  network_name = current_network.is_a?(Hash) ? current_network['_name'] : current_network
  return network_name if include_placeholder

  placeholder_network_name?(network_name) ? nil : network_name
end

.current_network_present?(interface_data) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 40

def self.current_network_present?(interface_data)
  !!interface_data&.fetch(CURRENT_NETWORK_KEY, nil)
end

.current_network_signal_dbm(interface_data) ⇒ Object



67
68
69
70
71
72
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 67

def self.current_network_signal_dbm(interface_data)
  current_network = interface_data&.fetch(CURRENT_NETWORK_KEY, nil)
  return nil unless current_network.is_a?(Hash)

  signal_dbm(current_network)
end

.network_array(value) ⇒ Object



84
85
86
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 84

def self.network_array(value)
  value.is_a?(Array) ? value.select { |network| network.is_a?(Hash) } : []
end

.network_list_key(interface_data = nil, associated: nil) ⇒ Object



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

def self.network_list_key(interface_data = nil, associated: nil)
  if associated.nil?
    associated = interface_data ? associated?(interface_data) : false
  end

  associated ? OTHER_LOCAL_NETWORKS_KEY : LOCAL_NETWORKS_KEY
end

.placeholder_network_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 15

def self.placeholder_network_name?(name)
  value = name.to_s.strip
  value.empty? || PLACEHOLDER_NETWORK_NAMES.include?(value.downcase)
end

.signal_strength(network) ⇒ Object



61
62
63
64
65
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 61

def self.signal_strength(network)
  # 'spairport_signal_noise' is a slash-separated "signal/noise" string (e.g. "-65/-95").
  # Take the first component as the signal strength in dBm; default to "0/0" if absent.
  signal_dbm(network) || 0
end

.sorted_network_names(networks) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 52

def self.sorted_network_names(networks)
  network_array(networks)
    .sort_by { |network| -signal_strength(network) }
    .map { |network| network['_name'] }
    .reject { |name| placeholder_network_name?(name) }
    .compact
    .uniq
end

Instance Method Details

#associated?(interface) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 104

def associated?(interface)
  self.class.associated?(interface_data(interface))
end

#current_network_name(interface, include_placeholder: false) ⇒ Object



108
109
110
111
112
113
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 108

def current_network_name(interface, include_placeholder: false)
  self.class.current_network_name(
    interface_data(interface),
    include_placeholder: include_placeholder
  )
end

#current_network_present?(interface) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 115

def current_network_present?(interface)
  self.class.current_network_present?(interface_data(interface))
end

#interface_data(interface) ⇒ Object



100
101
102
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 100

def interface_data(interface)
  interfaces.detect { |candidate| candidate['_name'] == interface }
end

#interfacesObject



92
93
94
95
96
97
98
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 92

def interfaces
  interface_data = system_profiler_wifi_data_entries
    .detect { |entry| entry.key?(INTERFACES_KEY) }
    &.fetch(INTERFACES_KEY, [])

  self.class.network_array(interface_data)
end

#network_hidden?(interface, ssid) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 138

def network_hidden?(interface, ssid)
  wifi_interface_data = interface_data(interface)
  return false unless wifi_interface_data
  return false unless wifi_interface_data[CURRENT_NETWORK_KEY]

  !visible_network_names_for_hidden_check(wifi_interface_data).include?(ssid)
end

#network_security(interface, ssid, associated: nil) ⇒ Object



132
133
134
135
136
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 132

def network_security(interface, ssid, associated: nil)
  network = visible_networks(interface, associated: associated)
    .detect { |candidate| candidate['_name'] == ssid }
  network&.fetch(SECURITY_MODE_KEY, nil)
end

#visible_network_names(interface, associated: nil) ⇒ Object



128
129
130
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 128

def visible_network_names(interface, associated: nil)
  self.class.sorted_network_names(visible_networks(interface, associated: associated))
end

#visible_networks(interface, associated: nil) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_navigator.rb', line 119

def visible_networks(interface, associated: nil)
  wifi_interface_data = interface_data(interface)
  return [] unless wifi_interface_data

  key = self.class.network_list_key(wifi_interface_data, associated: associated)
  networks = wifi_interface_data.fetch(key, [])
  self.class.network_array(networks)
end