ruby-rescuetime 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,14 @@ module Rescuetime
2
2
  class Application
3
3
  include Rescuetime::Debug
4
4
 
5
+ # NEW && UNTESTED
6
+ def self.to_yaml(apps = [])
7
+ yaml = "---\n"
8
+ [*apps].each { |app| yaml += app.to_yaml }
9
+
10
+ return yaml
11
+ end
12
+
5
13
  def self.current_application_name
6
14
  cmd = <<-CMD
7
15
  xprop -id `xprop -root |
@@ -113,31 +121,35 @@ module Rescuetime
113
121
  return out
114
122
  end
115
123
 
124
+ def to_yaml
125
+ to_upload_data
126
+ end
127
+
116
128
 
117
129
 
118
130
  private
119
- def finish!
120
- @active = false
121
- @finished_at = Time.now
122
- end
123
-
124
- def current_application_name
125
- application_name(self.class.current_application_name)
126
- end
127
-
128
- def current_window_title
129
- window_title(self.class.current_window_title)
130
- end
131
-
132
- # To be overwritten
133
- def application_name(name)
134
- name
135
- end
136
-
137
- # To be overwritten
138
- def window_title(title)
139
- title
140
- end
131
+ def finish!
132
+ @active = false
133
+ @finished_at = Time.now
134
+ end
135
+
136
+ def current_application_name
137
+ application_name(self.class.current_application_name)
138
+ end
139
+
140
+ def current_window_title
141
+ window_title(self.class.current_window_title)
142
+ end
143
+
144
+ # To be overwritten
145
+ def application_name(name)
146
+ name
147
+ end
148
+
149
+ # To be overwritten
150
+ def window_title(title)
151
+ title
152
+ end
141
153
  end
142
154
  end
143
155
 
@@ -22,7 +22,8 @@ module Rescuetime
22
22
  :path => nil
23
23
  }.merge(options)
24
24
 
25
- @debug = options[:debug]
25
+ debug! if options[:debug]
26
+
26
27
  @config_path = options[:config] || File.join(options[:path] || DEFAULT_PATH, DEFAULT_FILE)
27
28
  config = read_config(@config_path)
28
29
 
@@ -31,6 +32,7 @@ module Rescuetime
31
32
  :password => options[:password] || config[:password],
32
33
  :path => DEFAULT_PATH || config[:path]
33
34
  }
35
+
34
36
  update if @config != config
35
37
  end
36
38
 
@@ -59,25 +61,25 @@ module Rescuetime
59
61
 
60
62
 
61
63
  private
62
- def read_config(path)
63
- create_config_file(path) unless File.exist?(path)
64
- YAML::load(File.open(path))
65
- end
64
+ def read_config(path)
65
+ create_config_file(path) unless File.exist?(path)
66
+ YAML::load(File.open(path))
67
+ end
66
68
 
67
- def create_config_file(path)
68
- write_config_file(DEFAULT_CONFIG, path)
69
- end
69
+ def create_config_file(path)
70
+ write_config_file(DEFAULT_CONFIG, path)
71
+ end
70
72
 
71
- def update_config(config, path)
72
- write_config_file(config, path)
73
- end
73
+ def update_config(config, path)
74
+ write_config_file(config, path)
75
+ end
74
76
 
75
- def write_config_file(config, path)
76
- FileUtils.mkdir_p(File.dirname(path))
77
- File.open(path, 'w') do |f|
78
- f.write(YAML::dump(config))
79
- end
77
+ def write_config_file(config, path)
78
+ FileUtils.mkdir_p(File.dirname(path))
79
+ File.open(path, 'w') do |f|
80
+ f.write(YAML::dump(config))
80
81
  end
82
+ end
81
83
  end
82
84
  end
83
85
 
@@ -6,13 +6,17 @@ module Rescuetime
6
6
  end
7
7
 
8
8
  private
9
- # Debug-Wrapper
10
- def debug(msg = nil, &block)
11
- return unless debug?
9
+ def debug!(to_debug = true)
10
+ @debug = to_debug
11
+ end
12
+
13
+ # Debug-Wrapper
14
+ def debug(msg = nil, &block)
15
+ return unless debug?
12
16
 
13
- puts msg if msg
14
- yield if block_given?
15
- end
17
+ puts msg if msg
18
+ yield if block_given?
19
+ end
16
20
  end
17
21
  end
18
22
 
@@ -2,6 +2,8 @@ module Rescuetime
2
2
  class Loop
3
3
  include Rescuetime::Debug
4
4
 
5
+ attr_reader :config
6
+
5
7
  # =Creates and run a new Rescuetime::Loop:
6
8
  # Rescuetime::Loop.new({
7
9
  # :email => "foo@example.com",
@@ -21,12 +23,13 @@ module Rescuetime
21
23
 
22
24
  @config = Config.new(options)
23
25
 
24
- @debug = @config.debug?
26
+ debug! if @config.debug?
27
+
25
28
  debug "[OPTIONS]" do p options end
26
29
 
27
- @apps = []
30
+ @apps = []
28
31
  @current_app = nil
29
- @uploader = Rescuetime::Uploader.new( :debug => @debug,
32
+ @uploader = Rescuetime::Uploader.new( :debug => debug?,
30
33
  :email => @config.email,
31
34
  :password => @config.password)
32
35
 
@@ -39,55 +42,130 @@ module Rescuetime
39
42
  puts "Please call ruby-rescuetime with correct login credentials."
40
43
  puts " * ruby-rescuetime --email foo@example.com --password secret\n OR"
41
44
  puts " * edit: #{@config.location}"
42
- exit
45
+ puts "\n[OFFLINE-MODE]"
46
+ else
47
+ upload!
48
+ upload!("failure")
43
49
  end
44
50
 
45
- @running = true
46
51
  Signal.trap("INT") do
47
- shutdown if @running
52
+ shutdown! if running?
48
53
  end
49
54
 
50
55
  self.run
51
56
  end
52
57
 
58
+ def running!(running = true)
59
+ @running = running
60
+ end
61
+
53
62
  def running?
54
63
  @running == true
55
64
  end
56
65
 
57
66
  # Handler called if 'Ctrl + C' is pressed
58
- def shutdown
59
- @running = false
67
+ def shutdown!
68
+ running!(false)
60
69
  @apps << @current_app
61
- yamldata = @uploader.prepare_data(@apps)
62
- success = @uploader.upload(:yamldata => yamldata)
63
- raise "Upload failed" unless success
64
70
 
71
+ backup!
72
+ upload!
65
73
  exit
66
74
  end
67
75
 
68
76
  # Run the loop
69
77
  def run
70
- @current_app = Application.create(:debug => @debug)
71
- puts "[FOCUS] #{@current_app}"
78
+ running!
79
+ @current_app = Application.create(:debug => debug?)
72
80
 
73
81
  while true
74
82
  sleep 1 # TODO: move to config
75
- new_focus unless @current_app.active?
83
+ focus_changed if @current_app.finished? || backup?
84
+ end
85
+ end
86
+
87
+
88
+
89
+ ### BACKUP DATA ### (may be moved)
90
+
91
+ # NEW && UNTESTED
92
+ def last_backup_at
93
+ @last_backup_at ||= Time.now
94
+ end
95
+
96
+ # NEW && UNTESTED
97
+ def seconds_since_last_backup
98
+ Time.now - last_backup_at
99
+ end
100
+
101
+ # NEW && UNTESTED
102
+ # TODO: read from config
103
+ def backup?
104
+ @apps.length > 100 || seconds_since_last_backup > 30*60
105
+ end
106
+
107
+ # NEW && UNTESTED
108
+ def backup!
109
+ debug "[BACKUP]"
110
+
111
+ @last_backup_at = Time.now
112
+ timestamp = @last_backup_at.to_i
113
+
114
+ path = File.join(config.path, "upload", "todo", "#{timestamp}.yaml")
115
+ FileUtils.mkdir_p(File.dirname(path))
116
+
117
+ File.open(path, 'w') do |f|
118
+ f.write(Rescuetime::Application.to_yaml(@apps))
119
+ end
120
+
121
+ @apps = []
122
+ end
123
+
124
+ # NEW && UNTESTED
125
+ def upload!(mode = "todo") # possible modes: todo, failure
126
+ path = File.join(config.path, "upload", mode, "*.yaml")
127
+ files = Dir.glob(path)
128
+
129
+ success_path = File.join(config.path, "upload", "success")
130
+ failure_path = File.join(config.path, "upload", "failure")
131
+
132
+ # Make sure directories exist
133
+ FileUtils.mkdir_p(success_path)
134
+ FileUtils.mkdir_p(failure_path)
135
+
136
+ debug "[UPLOAD] #{files.count} files"
137
+ files.each_with_index do |f, i|
138
+ debug "#{f} (#{i})"
139
+ file = File.open(f, "rb")
140
+ yamldata = file.read
141
+ file.close
142
+ success = @uploader.upload(:yamldata => yamldata)
143
+
144
+ # CHECK: may another solution for base_name exist?
145
+ base_name = f.split("/").last
146
+
147
+ path = success ? success_path : failure_path
148
+ path = File.join(path, base_name)
149
+
150
+ # HACK: don't copy if mode == failure and failure again
151
+ FileUtils.mv(f, path) unless !success && mode == "failure"
76
152
  end
77
153
  end
78
154
 
79
155
 
80
156
 
81
157
  private
82
- # Old app lost focus, get new one
83
- def new_focus
84
- @apps << @current_app
85
- debug "[OLD FOCUS]" do
86
- puts "#{@current_app.to_s}: #{@current_app.active_time} seconds alive\n"
87
- end
88
- @current_app = Application.create(:debug => @debug)
89
- puts "[FOCUS] #{@current_app}"
158
+ # Old app lost focus, get new one
159
+ def focus_changed
160
+ backup! if backup?
161
+
162
+ @apps << @current_app
163
+ debug "\n[OLD FOCUS]" do
164
+ puts "#{@current_app.to_s}:\n#{@current_app.active_time} seconds alive\n\n"
90
165
  end
166
+ @current_app = Application.create(:debug => debug?)
167
+ puts "#{@current_app}"
168
+ end
91
169
  end
92
170
  end
93
171
 
@@ -20,7 +20,8 @@ module Rescuetime
20
20
  # :debug => false
21
21
  # })
22
22
  def initialize(options = {})
23
- @debug = options[:debug]
23
+ debug! if options[:debug]
24
+
24
25
  @http = Net::HTTP.new(API_HOST, 443)
25
26
  @http.use_ssl = true
26
27
 
@@ -28,27 +29,21 @@ module Rescuetime
28
29
  @password = options[:password]
29
30
  end
30
31
 
31
- # Convert array of app objects to yamldata
32
- def prepare_data(apps)
33
- data = "---\n"
34
- apps.each { |app| data += app.to_upload_data }
35
- debug "[DATA TO SAVE]" do puts data end
36
- # YAML::dump(apps)
37
-
38
- return data
39
- end
40
-
41
32
  # Service available?
42
33
  def connection_alive?
43
34
  resp = @http.get(API_HANDSHAKE_PATH)
44
35
  resp.code == "200"
45
36
  end
46
37
 
47
- # Upload yamldata
38
+ # =Uploads yaml-formatted data
39
+ # ==Usage:
40
+ # @uploader.upload(:yamldata => yamldata)
41
+ # ==Returns:
42
+ # true if upload successful, false otherwise
48
43
  def upload(options = {})
49
- hash = { :email => @email,
50
- :password => @password,
51
- :yamldata => options[:yamldata] }
44
+ hash = { :email => @email,
45
+ :password => @password,
46
+ :yamldata => options[:yamldata] }
52
47
 
53
48
  data = []
54
49
  hash.each do |key, value|
@@ -59,18 +54,32 @@ module Rescuetime
59
54
  debug "[YAMLDATA]" do puts data end
60
55
 
61
56
  headers = { 'User-agent' => USER_AGENT }
62
- resp = @http.post(API_UPLOAD_PATH, data, headers)
63
- debug "[UPLOAD]" do p resp.body end
57
+
58
+ begin
59
+ resp = @http.post(API_UPLOAD_PATH, data, headers)
60
+ rescue
61
+ return false
62
+ end
63
+
64
+ debug "[UPLOAD]" do puts resp.body end
64
65
 
65
66
  return (resp.code == "200" && !resp.body["<error>"])
66
67
  end
67
68
 
68
- # Handshake with login credentials
69
+ # =Handshake with login credentials
70
+ # ==Returns:
71
+ # true if handshake successful, false otherwise
69
72
  def handshake
70
73
  data = "email=#{@email}&password=#{@password}"
71
74
  headers = { 'User-agent' => USER_AGENT }
72
- resp = @http.post(API_HANDSHAKE_PATH, data, headers)
73
- debug "[HANDSHAKE]" do p resp.body end
75
+
76
+ begin
77
+ resp = @http.post(API_HANDSHAKE_PATH, data, headers)
78
+ rescue SocketError
79
+ return false
80
+ end
81
+
82
+ debug "[HANDSHAKE]" do puts resp.body end
74
83
 
75
84
  return (resp.code == "200" && !resp.body["login failure"])
76
85
  end
@@ -1,4 +1,4 @@
1
1
  module RubyRescuetime
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
4
4
 
@@ -0,0 +1,4 @@
1
+ module RubyRescuetime
2
+ VERSION = "0.0.5"
3
+ end
4
+
@@ -1,8 +1,6 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
- describe Rescuetime::Config do
4
- it "foobar" do
5
- pending
6
- end
3
+ describe Rescuetime do
4
+ pending
7
5
  end
8
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-rescuetime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-14 00:00:00.000000000 Z
12
+ date: 2012-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &80669200 !ruby/object:Gem::Requirement
16
+ requirement: &84534550 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '2.6'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *80669200
24
+ version_requirements: *84534550
25
25
  description: ! 'Upload your data to: https://www.rescuetime.com'
26
26
  email:
27
27
  - deradon87@gmail.com
@@ -32,11 +32,11 @@ extra_rdoc_files: []
32
32
  files:
33
33
  - bin/ruby-rescuetime
34
34
  - lib/rescuetime.rb
35
- - lib/tasks/ruby-rescuetime_tasks.rake
36
35
  - lib/rescuetime/config.rb
37
36
  - lib/rescuetime/version.rb
38
37
  - lib/rescuetime/loop.rb
39
38
  - lib/rescuetime/commands.rb
39
+ - lib/rescuetime/version.rb~
40
40
  - lib/rescuetime/extensions/gedit.rb
41
41
  - lib/rescuetime/extensions/chromium.rb
42
42
  - lib/rescuetime/debug.rb
@@ -62,7 +62,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
62
  version: '0'
63
63
  segments:
64
64
  - 0
65
- hash: -643785775
65
+ hash: 61471837
66
66
  required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  none: false
68
68
  requirements:
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  version: '0'
72
72
  segments:
73
73
  - 0
74
- hash: -643785775
74
+ hash: 61471837
75
75
  requirements: []
76
76
  rubyforge_project:
77
77
  rubygems_version: 1.8.10
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :ruby-rescuetime do
3
- # # Task goes here
4
- # end