Class: Rescuetime::Loop
- Inherits:
-
Object
- Object
- Rescuetime::Loop
- Includes:
- Debug
- Defined in:
- lib/rescuetime/loop.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#backup! ⇒ Object
NEW && UNTESTED.
-
#backup? ⇒ Boolean
NEW && UNTESTED TODO: read from config.
-
#initialize(options = {}) ⇒ Loop
constructor
Creates and run a new Rescuetime::Loop: Rescuetime::Loop.new({ :email => “[email protected]”, :password => “bar”, :config => “/home/foobar/rescue/bar.yml”, :path => “/home/foobar/rescue/data” , :debug => true }).
-
#last_backup_at ⇒ Object
NEW && UNTESTED.
-
#run ⇒ Object
Run the loop.
- #running!(running = true) ⇒ Object
- #running? ⇒ Boolean
-
#seconds_since_last_backup ⇒ Object
NEW && UNTESTED.
-
#shutdown! ⇒ Object
Handler called if ‘Ctrl + C’ is pressed.
-
#upload!(mode = "todo") ⇒ Object
NEW && UNTESTED.
Methods included from Debug
Constructor Details
#initialize(options = {}) ⇒ Loop
Creates and run a new Rescuetime::Loop:
Rescuetime::Loop.new({
:email => "[email protected]",
:password => "bar",
:config => "/home/foobar/rescue/bar.yml",
:path => "/home/foobar/rescue/data" ,
:debug => true
})
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rescuetime/loop.rb', line 15 def initialize( = {}) = { :debug => false, :email => nil, :password => nil, :config => nil, :path => nil }.merge() @config = Config.new() debug! if @config.debug? debug "[OPTIONS]" do p end @apps = [] @current_app = nil @uploader = Rescuetime::Uploader.new( :debug => debug?, :email => @config.email, :password => @config.password) debug "[LOGIN - DATA]" do puts "#{@config.email}:#{@config.password}" end if !@uploader.handshake puts "[LOGIN FAILED]" puts "Please call ruby-rescuetime with correct login credentials." puts " * ruby-rescuetime --email [email protected] --password secret\n OR" puts " * edit: #{@config.location}" puts "\n[OFFLINE-MODE]" else upload! upload!("failure") end Signal.trap("INT") do shutdown! if running? end self.run end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
5 6 7 |
# File 'lib/rescuetime/loop.rb', line 5 def config @config end |
Instance Method Details
#backup! ⇒ Object
NEW && UNTESTED
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rescuetime/loop.rb', line 108 def backup! debug "[BACKUP]" @last_backup_at = Time.now = @last_backup_at.to_i path = File.join(config.path, "upload", "todo", "#{}.yaml") FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w') do |f| f.write(Rescuetime::Application.to_yaml(@apps)) end @apps = [] end |
#backup? ⇒ Boolean
NEW && UNTESTED TODO: read from config
103 104 105 |
# File 'lib/rescuetime/loop.rb', line 103 def backup? @apps.length > 100 || seconds_since_last_backup > 30*60 end |
#last_backup_at ⇒ Object
NEW && UNTESTED
92 93 94 |
# File 'lib/rescuetime/loop.rb', line 92 def last_backup_at @last_backup_at ||= Time.now end |
#run ⇒ Object
Run the loop
77 78 79 80 81 82 83 84 85 |
# File 'lib/rescuetime/loop.rb', line 77 def run running! @current_app = Application.create(:debug => debug?) while true sleep 1 # TODO: move to config focus_changed if @current_app.finished? || backup? end end |
#running!(running = true) ⇒ Object
58 59 60 |
# File 'lib/rescuetime/loop.rb', line 58 def running!(running = true) @running = running end |
#running? ⇒ Boolean
62 63 64 |
# File 'lib/rescuetime/loop.rb', line 62 def running? @running == true end |
#seconds_since_last_backup ⇒ Object
NEW && UNTESTED
97 98 99 |
# File 'lib/rescuetime/loop.rb', line 97 def seconds_since_last_backup Time.now - last_backup_at end |
#shutdown! ⇒ Object
Handler called if ‘Ctrl + C’ is pressed
67 68 69 70 71 72 73 74 |
# File 'lib/rescuetime/loop.rb', line 67 def shutdown! running!(false) @apps << @current_app backup! upload! exit end |
#upload!(mode = "todo") ⇒ Object
NEW && UNTESTED
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rescuetime/loop.rb', line 125 def upload!(mode = "todo") # possible modes: todo, failure path = File.join(config.path, "upload", mode, "*.yaml") files = Dir.glob(path) success_path = File.join(config.path, "upload", "success") failure_path = File.join(config.path, "upload", "failure") # Make sure directories exist FileUtils.mkdir_p(success_path) FileUtils.mkdir_p(failure_path) debug "[UPLOAD] #{files.count} files" files.each_with_index do |f, i| debug "#{f} (#{i})" file = File.open(f, "rb") yamldata = file.read file.close success = @uploader.upload(:yamldata => yamldata) # CHECK: may another solution for base_name exist? base_name = f.split("/").last path = success ? success_path : failure_path path = File.join(path, base_name) # HACK: don't copy if mode == failure and failure again FileUtils.mv(f, path) unless !success && mode == "failure" end end |