module Accessibility::SystemInfo
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.
Public Instance Methods
All IP addresses the system has interfaces for
@example
Accessibility::SystemInfo.addresses # => ["fe80::6aa8:6dff:fe20:822%en1", "192.168.0.17", "fe80::1%lo0", "127.0.0.1", "::1"]
@return [Array<String>]
# File lib/accessibility/system_info.rb, line 62 def addresses NSHost.currentHost.addresses end
Returns the charge percentage of the battery (if present)
A special value of ‘-1.0` is returned if you have no battery.
@example
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
@return [Float]
# File lib/accessibility/system_info.rb, line 203 def battery_charge_level Battery.level end
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.
@example
# AC cord plugged in battery_life_estimate # => 0 # unplug AC cord battery_life_estimate # => -1 # wait a few minutes battery_life_estimate # => 423
@return [Fixnum]
# File lib/accessibility/system_info.rb, line 228 def battery_life_estimate Battery.time_to_empty end
Return the current state of the battery
@example
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
@return [Symbol]
# File lib/accessibility/system_info.rb, line 184 def battery_state Battery.state end
The first, and likely common, name the system responds to
@example
Accessibility::SystemInfo.hostname # => "ferrous.local"
@return [Array<String>]
# File lib/accessibility/system_info.rb, line 49 def hostname hostnames.first end
All hostnames that the system responds to
@example
Accessibility::SystemInfo.hostnames # => ["ferrous.local", "localhost"]
@return [Array<String>]
# File lib/accessibility/system_info.rb, line 37 def hostnames NSHost.currentHost.names end
All IPv4 addresses the system has interfaces for
@example
Accessibility::SystemInfo.ipv4_addresses # => ["192.168.0.17", "127.0.0.1"]
@return [Array<String>]
# File lib/accessibility/system_info.rb, line 75 def ipv4_addresses addresses.select { |address| address.match /\./ } end
All IPv6 addresses the system has interfaces for
@example
Accessibility::SystemInfo.ipv6_addresses # => ["fe80::6aa8:6dff:fe20:822%en1", "fe80::1%lo0", "::1"]
@return [Array<String>]
# File lib/accessibility/system_info.rb, line 88 def ipv6_addresses addresses.select { |address| address.match /:/ } end
System model string
@example
Accessibility::SystemInfo.model # => "MacBookPro8,2"
@return [String]
# File lib/accessibility/system_info.rb, line 100 def model @model ||= `sysctl hw.model`.split.last.chomp end
The name the machine uses for Bonjour
@example
Accessibility::SystemInfo.name # => "ferrous"
@return [String]
# File lib/accessibility/system_info.rb, line 24 def name NSHost.currentHost.localizedName end
Number of CPUs the system current has enabled
@example
Accessibility::SystemInfo.num_active_processors # => 8
@return [Fixnum]
# File lib/accessibility/system_info.rb, line 151 def num_active_processors NSProcessInfo.processInfo.activeProcessorCount end
Total number of CPUs the system could use
May not be the same as {#num_active_processors}.
@example
Accessibility::SystemInfo.num_processors # => 8
@return [Fixnum]
# File lib/accessibility/system_info.rb, line 138 def num_processors NSProcessInfo.processInfo.processorCount end
OS X version string
@example
Accessibility::SystemInfo.osx_version # => "Version 10.8.2 (Build 12C60)"
@return [String]
# File lib/accessibility/system_info.rb, line 112 def osx_version NSProcessInfo.processInfo.operatingSystemVersionString end
Total amount of memory for the system, in bytes
@example
Accessibility::SystemInfo.total_ram # => 17179869184
@return [Fixnum]
# File lib/accessibility/system_info.rb, line 164 def total_ram NSProcessInfo.processInfo.physicalMemory end
System uptime, in seconds
@example
Accessibility::SystemInfo.uptime # => 22999.76858776
@return [Float]
# File lib/accessibility/system_info.rb, line 124 def uptime NSProcessInfo.processInfo.systemUptime end