Class: Timeclock::Clock

Inherits:
Object
  • Object
show all
Defined in:
lib/timeclock/clock.rb

Class Method Summary collapse

Class Method Details

.automateObject



194
195
196
197
198
199
200
201
202
# File 'lib/timeclock/clock.rb', line 194

def automate
  if `ps aux | grep Adium | grep -v grep`.split("\n").size > 0
    puts "AUTOMATE CLOCK IN"
    automate_clock_in
  else
    puts "AUTOMATE CLOCK OUT"
    automate_clock_out
  end
end

.automate_clock_inObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/timeclock/clock.rb', line 72

def automate_clock_in
  string = get_log_string
  array = string.split("\n")
  if !array.empty?
    if array.last.include? "in"
      exit(1)
    end
  end
  array << "in del11009 #{Time.now}"
  put_log_string array.join("\n")
  puts "clocking in at #{Time.now}"
end

.automate_clock_outObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/timeclock/clock.rb', line 97

def automate_clock_out
  string = get_log_string
  array = string.split("\n")
  if array.last.include? "out"
    exit(1)
  end
  array << "out #{Time.now}"
  put_log_string array.join("\n")
  puts "clocking out at #{Time.now.to_s}"
end

.check_emailObject



214
215
216
# File 'lib/timeclock/clock.rb', line 214

def check_email
  `touch #{email_file}` unless File.exist? email_file
end

.check_fileObject



218
219
220
# File 'lib/timeclock/clock.rb', line 218

def check_file
  `touch #{log_file}` unless File.exist? log_file
end

.clayObject



204
205
206
207
# File 'lib/timeclock/clock.rb', line 204

def clay
  require 'erb'
  ERB.new(@template).result(binding)
end

.clearObject



222
223
224
225
226
227
228
# File 'lib/timeclock/clock.rb', line 222

def clear
  print "Are you sure you want delete your existing log? (yes/no): "
  if STDIN.gets.strip == 'yes'
    `rm #{log_file}`
    check_file
  end
end

.clock_inObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/timeclock/clock.rb', line 56

def clock_in
  string = get_log_string
  array = string.split("\n")
  if !array.empty?
    if array.last.include? "in"
      puts "You are already clocked in"
      exit(1)
    end
  end
  print "what are you working on: "
  job = STDIN.gets.strip
  array << "in #{job} #{Time.now}"
  put_log_string array.join("\n")
  puts "clocking in at #{Time.now}"
end

.clock_outObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/timeclock/clock.rb', line 85

def clock_out
  string = get_log_string
  array = string.split("\n")
  if array.last.include? "out"
    puts "You are already clocked out"
    exit(1)
  end
  array << "out #{Time.now}"
  put_log_string array.join("\n")
  puts "clocking out at #{Time.now.to_s}"
end

.collectObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/timeclock/clock.rb', line 133

def collect
  string = get_log_string
  array = string.split("\n")
  rtn = []
  if array.size.even?
    until array.empty?
      clock_in = array.shift
      clock_out = array.shift
      in_array = clock_in.split(" ")
      out_array = clock_out.split(" ")
      in_time = Time.parse("#{in_array[2]} #{in_array[3]} #{in_array[4]}")
      out_time = Time.parse("#{out_array[1]} #{out_array[2]} #{out_array[3]}")
      hash = {}
      hash[:hours] = (out_time - in_time) / 3600 #convert to hours
      hash[:clock_in] = in_time.ctime 
      hash[:clock_out] = out_time.ctime
      hash[:project] = in_array[1]
      rtn << hash
    end
  else
    puts "You do not have an even number of clock in's and out's"
    exit(1)
  end
  rtn
end

.daily_logObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/timeclock/clock.rb', line 114

def daily_log
  collection = collect
  new_hash = {}
  time_array = []
  collection.each do |element|
    day = Time.parse(element[:clock_in]).day
    if new_hash[day]
      new_hash[day][:total] += element[:hours]
      new_hash[day][:log] << {:in => element[:clock_in], :out => element[:clock_out]}
    else
      new_hash[day] = {}
      new_hash[day][:total] = element[:hours]
      new_hash[day][:log] = [{:in => element[:clock_in], :out => element[:clock_out]}]
    end
  end
  new_hash[:total] = total
  new_hash
end

.emailObject



209
210
211
212
# File 'lib/timeclock/clock.rb', line 209

def email
  string = get_email_string
  array = string.split("\n")
end

.email_fileObject



43
44
45
# File 'lib/timeclock/clock.rb', line 43

def email_file
  "#{home_directory}/.email"
end

.get_email_stringObject



39
40
41
# File 'lib/timeclock/clock.rb', line 39

def get_email_string
  File.open(email_file, 'r') { |f| f.read }
end

.get_log_stringObject



31
32
33
# File 'lib/timeclock/clock.rb', line 31

def get_log_string
  File.open(log_file, 'r') { |f| f.read }
end

.home_directoryObject



22
23
24
# File 'lib/timeclock/clock.rb', line 22

def home_directory
  running_on_windows? ? ENV['USERPROFILE'] : ENV['HOME']
end

.log_fileObject



48
49
50
# File 'lib/timeclock/clock.rb', line 48

def log_file
  "#{home_directory}/.timeclock"
end

.put_email_string(contents) ⇒ Object



35
36
37
# File 'lib/timeclock/clock.rb', line 35

def put_email_string(contents)
  File.open(email_file, "w").write(contents)      
end

.put_log_string(contents) ⇒ Object



26
27
28
# File 'lib/timeclock/clock.rb', line 26

def put_log_string(contents)
  File.open(log_file, "w").write(contents)
end

.running_on_windows?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/timeclock/clock.rb', line 52

def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end

.sendObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/timeclock/clock.rb', line 159

def send
  puts collect
  print "who should I send this to: "
  to = STDIN.gets.strip
  print "what email should I send it from: "
  from = STDIN.gets.strip
  
  # begin
    require 'pony'
    require 'erb'

    Pony.mail(
      :to           => to, 
      :from         => from, 
      :subject      => "Time card", 
      :content_type => 'text/html',
      :domain       => "localhost.localdomain",
      :html_body    => ERB.new(@template).result(binding),
      :body         => "You are reading this because your email client sux and cant interperate html... fix it."
      )
    puts "Time card has been sent to #{to}."
  # rescue Exception => e
  #   puts "Time card not sent because pony is dumb."
  # end

end

.setup_automateObject



186
187
188
# File 'lib/timeclock/clock.rb', line 186

def setup_automate
  puts `whenever -w config/schedule.rb`
end

.totalObject



108
109
110
111
112
# File 'lib/timeclock/clock.rb', line 108

def total
  total = 0.0
  collect.each {|value|total += value[:hours]}
    total
end

.unsetup_automateObject



190
191
192
# File 'lib/timeclock/clock.rb', line 190

def unsetup_automate
  puts `whenever -c config/schedule.rb`
end