Class: Rescuetime::Application

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/rescuetime/application.rb

Direct Known Subclasses

Chromium, Gedit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Debug

#debug?

Constructor Details

#initialize(options = {}) ⇒ Application

Constructor: Call with => true to get debug messages



49
50
51
52
53
54
55
56
57
# File 'lib/rescuetime/application.rb', line 49

def initialize(options = {})
  @debug      = options[:debug]
  @name       = options[:name]  || current_application_name
  @title      = options[:title] || current_window_title

  @created_at   = Time.now
  @finished_at  = nil
  @active       = true
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



36
37
38
# File 'lib/rescuetime/application.rb', line 36

def created_at
  @created_at
end

#finished_atObject (readonly)

Returns: Time of where app where detected to not be active anymore, or Time now if not finished yet



69
70
71
# File 'lib/rescuetime/application.rb', line 69

def finished_at
  @finished_at
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/rescuetime/application.rb', line 36

def name
  @name
end

#titleObject (readonly)

Returns the value of attribute title.



36
37
38
# File 'lib/rescuetime/application.rb', line 36

def title
  @title
end

Class Method Details

.create(options = {}) ⇒ Object

TODO



40
41
42
43
44
# File 'lib/rescuetime/application.rb', line 40

def self.create(options = {})
  name  = options[:name] || self.current_application_name
  klass = Rescuetime::Extension.get(name) || self
  klass.new(options)
end

.current_application_nameObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rescuetime/application.rb', line 13

def self.current_application_name
  cmd = <<-CMD
    xprop -id `xprop -root |
      awk '/_NET_ACTIVE_WINDOW/ {print $5; exit;}'` |
      awk -F = '/WM_CLASS/ {print $2; exit;}'|
      awk '{print $2}' |
      sed 's/^"//g' |
      sed 's/"$//g'
  CMD
  `#{cmd}`.strip
end

.current_window_titleObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/rescuetime/application.rb', line 25

def self.current_window_title
  cmd = <<-CMD
    xprop -id `xprop -root |
      awk '/_NET_ACTIVE_WINDOW/ {print $5; exit;}'` |
      awk -F = '/^WM_NAME/ {print $2; exit;}' |
      sed -e's/^ *"//g' |
      sed -e's/\\"$//g'
  CMD
  `#{cmd}`.strip
end

.to_yaml(apps = []) ⇒ Object

NEW && UNTESTED



6
7
8
9
10
11
# File 'lib/rescuetime/application.rb', line 6

def self.to_yaml(apps = [])
  yaml = "---\n"
  [*apps].each { |app| yaml += app.to_yaml }

  return yaml
end

Instance Method Details

#active?Boolean

Returns: boolean, if application is still active window

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
# File 'lib/rescuetime/application.rb', line 78

def active?
  return @active unless @active

  @active = @name  == current_application_name &&
            @title == current_window_title
  finish! unless @active

  return @active
end

#active_timeObject

Returns difference between StartTime and now if still active, or StartTime and FinishedTime if not ative anymore



94
95
96
# File 'lib/rescuetime/application.rb', line 94

def active_time
  (active?) ? (Time.now - @created_at) : (@finished_at - @created_at)
end

#extended_infoObject



102
103
104
# File 'lib/rescuetime/application.rb', line 102

def extended_info
  (@extended_info || "").gsub("\000", "").gsub("'", "`")
end

#finished?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/rescuetime/application.rb', line 88

def finished?
  !active?
end

#os_usernameObject



106
107
108
# File 'lib/rescuetime/application.rb', line 106

def os_username
  @os_username ||= `uname -n`.strip + "\\" + `id -un`.strip
end

#to_sObject



98
99
100
# File 'lib/rescuetime/application.rb', line 98

def to_s
  "#{name}:#{title}:#{extended_info}"
end

#to_upload_dataObject

TODO



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rescuetime/application.rb', line 111

def to_upload_data
  return "" if name.empty?

  out =  "- os_username: '#{os_username}'\n"
  out += "  app_name: '#{name}'\n"
  out += "  window_title: '#{title}'\n"
  out += "  extended_info: '#{extended_info}'\n"
  out += "  start_time: #{created_at}\n"
  out += "  end_time: #{finished_at}\n"

  return out
end

#to_yamlObject



124
125
126
# File 'lib/rescuetime/application.rb', line 124

def to_yaml
  to_upload_data
end