Module: Accessibility::SystemInfo

Extended by:
SystemInfo
Included in:
SystemInfo
Defined in:
lib/accessibility/system_info.rb

Overview

Interface for collecting some simple information about the system. This information may be useful as diagnostic output when running tests, or if you simply need to find the hostname of the machine so it can be passed to another process to initiate a connection.

This module extends itself, so all methods are available on the module and you will want to use the module as a utility module.

Instance Method Summary collapse

Instance Method Details

#addressesArray<String>

All IP addresses the system has interfaces for

Examples:


Accessibility::SystemInfo.addresses
  # => ["fe80::6aa8:6dff:fe20:822%en1", "192.168.0.17", "fe80::1%lo0", "127.0.0.1", "::1"]

Returns:

  • (Array<String>)


62
63
64
# File 'lib/accessibility/system_info.rb', line 62

def addresses
  NSHost.currentHost.addresses
end

#battery_charge_levelFloat Also known as: battery_level

Returns the charge percentage of the battery (if present)

A special value of -1.0 is returned if you have no battery.

Examples:


battery_charge_level # => 1.0
# unplug AC cord and wait a couple of minutes
battery_charge_level # => 0.99

# if you have no battery
battery_charge_level # => -1.0

Returns:

  • (Float)


203
204
205
# File 'lib/accessibility/system_info.rb', line 203

def battery_charge_level
  Battery.level
end

#battery_life_estimateFixnum

Return an estimate on the number of minutes until the battery is drained

A special value of 0 indicates that the battery is not discharging. You should really only call this after you know that the battery is discharging by calling #battery_state and having :discharging returned.

A special value of -1 is returned when the estimate is in flux and cannot be accurately estimated.

Examples:


# AC cord plugged in
battery_life_estimate # => 0
# unplug AC cord
battery_life_estimate # => -1
# wait a few minutes
battery_life_estimate # => 423

Returns:

  • (Fixnum)


228
229
230
# File 'lib/accessibility/system_info.rb', line 228

def battery_life_estimate
  Battery.time_to_empty
end

#battery_stateSymbol

Return the current state of the battery

Examples:


battery_state # => :charged
# unplug AC cord
battery_state # => :discharging
# plug AC cord back in after several minutes
battery_state # => :charging

# try this method when you have no battery
battery_state # => :not_installed

Returns:



184
185
186
# File 'lib/accessibility/system_info.rb', line 184

def battery_state
  Battery.state
end

#hostnameArray<String>

The first, and likely common, name the system responds to

Examples:


Accessibility::SystemInfo.hostname # => "ferrous.local"

Returns:

  • (Array<String>)


49
50
51
# File 'lib/accessibility/system_info.rb', line 49

def hostname
  hostnames.first
end

#hostnamesArray<String>

All hostnames that the system responds to

Examples:


Accessibility::SystemInfo.hostnames
  # => ["ferrous.local", "localhost"]

Returns:

  • (Array<String>)


37
38
39
# File 'lib/accessibility/system_info.rb', line 37

def hostnames
  NSHost.currentHost.names
end

#ipv4_addressesArray<String>

All IPv4 addresses the system has interfaces for

Examples:


Accessibility::SystemInfo.ipv4_addresses
  # => ["192.168.0.17", "127.0.0.1"]

Returns:

  • (Array<String>)


75
76
77
# File 'lib/accessibility/system_info.rb', line 75

def ipv4_addresses
  addresses.select { |address| address.match /\./ }
end

#ipv6_addressesArray<String>

All IPv6 addresses the system has interfaces for

Examples:


Accessibility::SystemInfo.ipv6_addresses
  # => ["fe80::6aa8:6dff:fe20:822%en1", "fe80::1%lo0", "::1"]

Returns:

  • (Array<String>)


88
89
90
# File 'lib/accessibility/system_info.rb', line 88

def ipv6_addresses
  addresses.select { |address| address.match /:/ }
end

#modelString

System model string

Examples:


Accessibility::SystemInfo.model # => "MacBookPro8,2"

Returns:

  • (String)


100
101
102
# File 'lib/accessibility/system_info.rb', line 100

def model
  @model ||= `sysctl hw.model`.split.last.chomp
end

#nameString

The name the machine uses for Bonjour

Examples:


Accessibility::SystemInfo.name
  # => "ferrous"

Returns:

  • (String)


24
25
26
# File 'lib/accessibility/system_info.rb', line 24

def name
  NSHost.currentHost.localizedName
end

#num_active_processorsFixnum Also known as: number_of_active_processors

Number of CPUs the system current has enabled

Examples:


Accessibility::SystemInfo.num_active_processors # => 8

Returns:

  • (Fixnum)


151
152
153
# File 'lib/accessibility/system_info.rb', line 151

def num_active_processors
  NSProcessInfo.processInfo.activeProcessorCount
end

#num_processorsFixnum Also known as: number_of_processors

Total number of CPUs the system could use

May not be the same as #num_active_processors.

Examples:


Accessibility::SystemInfo.num_processors # => 8

Returns:

  • (Fixnum)


138
139
140
# File 'lib/accessibility/system_info.rb', line 138

def num_processors
  NSProcessInfo.processInfo.processorCount
end

#osx_versionString

OS X version string

Examples:


Accessibility::SystemInfo.osx_version # => "Version 10.8.2 (Build 12C60)"

Returns:

  • (String)


112
113
114
# File 'lib/accessibility/system_info.rb', line 112

def osx_version
  NSProcessInfo.processInfo.operatingSystemVersionString
end

#total_ramFixnum Also known as: ram

Total amount of memory for the system, in bytes

Examples:


Accessibility::SystemInfo.total_ram # => 17179869184

Returns:

  • (Fixnum)


164
165
166
# File 'lib/accessibility/system_info.rb', line 164

def total_ram
  NSProcessInfo.processInfo.physicalMemory
end

#uptimeFloat

System uptime, in seconds

Examples:


Accessibility::SystemInfo.uptime # => 22999.76858776

Returns:

  • (Float)


124
125
126
# File 'lib/accessibility/system_info.rb', line 124

def uptime
  NSProcessInfo.processInfo.systemUptime
end