Class: Timestream::Hamster

Inherits:
Object
  • Object
show all
Defined in:
lib/timestream/hamster.rb

Defined Under Namespace

Classes: Fact, Timestream

Instance Method Summary collapse

Constructor Details

#initialize(start_time, end_time) ⇒ Hamster

Returns a new instance of Hamster.



48
49
50
51
# File 'lib/timestream/hamster.rb', line 48

def initialize(start_time, end_time)
  @start_time = start_time
  @end_time = end_time
end

Instance Method Details

#factsObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/timestream/hamster.rb', line 37

def facts
  dbus_facts = proxy.GetFacts(@start_time.to_i, @end_time.to_i, '')
  # XXX for some reason GetFacts returns facts in a one-element array
  dbus_facts = dbus_facts[0]
  
  # convert dbus facts into instances
  dbus_facts.collect { |dbus_fact|
    Fact.new(*dbus_fact)
  }
end

#proxyObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/timestream/hamster.rb', line 53

def proxy
  @proxy ||= begin
    session_bus = DBus::SessionBus.instance
    hamster_service = session_bus.service('org.gnome.Hamster')
    hamster = hamster_service.object('/org/gnome/Hamster')
    # interfaces and therefore methods won't work if not first introspected
    hamster.introspect
    # set interface so methods can be called directly on object
    hamster.default_iface = 'org.gnome.Hamster'
    
    hamster
  end
end

#timestream_by_categoryObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/timestream/hamster.rb', line 85

def timestream_by_category
  if @timestream_by_category.nil?
    @timestream_by_category = Hash.new { |hash, category|
      hash[category] = Timestream.new
    }
    
    facts.each do |fact|
      timestream = @timestream_by_category[fact.category_name]
      # convert seconds to fractional hours
      timestream[fact.date] += fact.delta_in_seconds.fdiv(60 * 60)
    end 
  end
  
  @timestream_by_category
end