Class: Batt::Reader
- Inherits:
-
Object
- Object
- Batt::Reader
- Defined in:
- lib/batt/reader.rb
Instance Attribute Summary collapse
-
#battery_reader ⇒ Object
Returns the value of attribute battery_reader.
Class Method Summary collapse
-
.color_for_capacity(capacity) ⇒ Object
Given a capacity percentage, return the color.
Instance Method Summary collapse
-
#get_battery_status_osx ⇒ Object
get the battery status for OSX returns a hash.
-
#get_os ⇒ Object
figure out which OS is running this might need to be a little more solid returns a downcased symbol (eg:
:darwin
or:linux
). -
#initialize ⇒ Reader
constructor
A new instance of Reader.
- #parse_battery_status_osx(output) ⇒ Object
-
#status ⇒ Object
runs the batteryreader this could probably be refactored to leverage a class per OS.
Constructor Details
#initialize ⇒ Reader
Returns a new instance of Reader.
6 7 8 9 10 11 12 13 14 |
# File 'lib/batt/reader.rb', line 6 def initialize os = get_os case os when :darwin @battery_reader = lambda { parse_battery_status_osx(get_battery_status_osx) } else raise "Unsupported OS: #{ os }" end end |
Instance Attribute Details
#battery_reader ⇒ Object
Returns the value of attribute battery_reader.
4 5 6 |
# File 'lib/batt/reader.rb', line 4 def battery_reader @battery_reader end |
Class Method Details
.color_for_capacity(capacity) ⇒ Object
Given a capacity percentage, return the color
54 55 56 57 58 59 60 61 62 |
# File 'lib/batt/reader.rb', line 54 def color_for_capacity(capacity) case capacity.to_i when -100..20 then :red # if there's any weird errors where things go negative, be red. when 20..30 then :orange when 30..75 then :yellow else :green end end |
Instance Method Details
#get_battery_status_osx ⇒ Object
get the battery status for OSX returns a hash. spits out something like: Currently drawing from ‘AC Power’ -InternalBattery-0 98%; charging; 0:30 remaining TODO: write some docs
38 39 40 41 |
# File 'lib/batt/reader.rb', line 38 def get_battery_status_osx line = Cocaine::CommandLine.new('pmset', '-g batt') output = line.run end |
#get_os ⇒ Object
figure out which OS is running this might need to be a little more solid returns a downcased symbol (eg: :darwin
or :linux
)
25 26 27 28 29 30 |
# File 'lib/batt/reader.rb', line 25 def get_os line = Cocaine::CommandLine.new('uname') output = line.run output.chomp.downcase.intern end |
#parse_battery_status_osx(output) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/batt/reader.rb', line 43 def parse_battery_status_osx(output) output = output.split(/\n/) status = output.shift.scan(/'(.+?)'/).first result = status + output.shift.scan(/(\d+%);\s*(.+?);\s*(.+)/).first Hash[[:source, :capacity, :status, :remaining].zip(result)] end |
#status ⇒ Object
runs the batteryreader this could probably be refactored to leverage a class per OS
18 19 20 |
# File 'lib/batt/reader.rb', line 18 def status @battery_reader.call end |