class PieceOfWax

Public Instance Methods

activity(x) click to toggle source
# File lib/piece_of_wax.rb, line 6
def activity(x)
  write_to_file("#{Time.now} - #{x}")
  puts "wrote #{x} to file"
end
read(date = Date.today.strftime('%Y-%m-%d')) click to toggle source
# File lib/piece_of_wax.rb, line 12
def read(date = Date.today.strftime('%Y-%m-%d'))
  output =
    begin
      generate_output(File.open(filename(date)).readlines)
    rescue Errno::ENOENT
      "Sorry, I couldn't find any logs for #{print_date(date)}."
    end
  puts output
end

Private Instance Methods

date_format(str) click to toggle source
# File lib/piece_of_wax.rb, line 60
def date_format(str)
  year, month, day = str.split('-').map(&:to_i)
  Date.new(year, month, day).strftime('%-m/%-d/%y')
end
filename(date) click to toggle source
# File lib/piece_of_wax.rb, line 31
def filename(date)
  "%s.txt" % Date.parse(date).strftime("%y%m%d")
end
generate_output(lines) click to toggle source
# File lib/piece_of_wax.rb, line 43
def generate_output(lines)
  date = date_format(lines[0].split(' ')[0])
  result =
    lines.each_cons(2).reduce([date]) do |output, (line1, line2)|
      output << output_line(line1, line2)
    end
  result << output_line(lines.last, "#{date} 05:45:00 -0500 - #{lines.last.split(' - ')[1]}")
end
output_line(line1, line2) click to toggle source
# File lib/piece_of_wax.rb, line 52
def output_line(line1, line2)
  date_info1, activity1 = line1.split(' - ')
  date_info2, _activity2 = line2.split(' - ')
  _date1, time1, _time_delta1 = date_info1.split(' ')
  _date2, time2, _time_delta2 = date_info2.split(' ')
  "#{time_format(time1)}-#{time_format(time2)}    - #{activity1}"
end
print_date(date) click to toggle source
time_format(str) click to toggle source
# File lib/piece_of_wax.rb, line 65
def time_format(str)
  str.split(':').first(2).join(':')
end
write_to_file(str) click to toggle source
# File lib/piece_of_wax.rb, line 24
def write_to_file(str)
  today = Time.now.strftime("%y%m%d")
  File.open("#{today}.txt", "a+") do |f|
    f.puts(str)
  end
end