Class: Batt::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/batt/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReader

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_readerObject

Returns the value of attribute battery_reader.



4
5
6
# File 'lib/batt/reader.rb', line 4

def battery_reader
  @battery_reader
end

Instance Method Details

#get_battery_status_osxObject

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



36
37
38
39
# File 'lib/batt/reader.rb', line 36

def get_battery_status_osx
  line = Cocaine::CommandLine.new('pmset', '-g batt')
  output = line.run
end

#get_osObject

figure out which OS is running this might need to be a little more solid returns a downcased symbol



23
24
25
26
27
28
# File 'lib/batt/reader.rb', line 23

def get_os
  line = Cocaine::CommandLine.new('uname')
  output = line.run

  output.chomp.downcase.intern
end

#parse_battery_status_osx(output) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/batt/reader.rb', line 41

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

#statusObject



16
17
18
# File 'lib/batt/reader.rb', line 16

def status
  @battery_reader.call
end