class WhatHaveIDone::Runner

Attributes

client[R]

Public Class Methods

new() click to toggle source
# File lib/what_have_i_done/runner.rb, line 5
def initialize()
  @client = TogglV8::API.new
end

Public Instance Methods

run() click to toggle source
# File lib/what_have_i_done/runner.rb, line 9
def run
  entries = client.get_time_entries(start_date: Date.today.to_datetime)
  entries_by_project = entries.group_by { |entry| entry["pid"] }
  projects = entries_by_project.keys.map { |pid| client.get_project(pid) }

  projects.each do |project|
    project_entries = entries_by_project[project["id"]]
    time_spent = duration_for_entries(project_entries)
    entries_str = project_entries.map do |entry|
      entry["description"]
    end.uniq.join('; ')

    subtitle project['name']
    subtitle "#{seconds_as_time(time_spent)} (#{seconds_as_hours(time_spent)})"
    text "#{entries_str}\n\n"
  end

  total_time = duration_for_entries(entries)

  title "Total time today:"
  title "#{seconds_as_time(total_time)} (#{seconds_as_hours(total_time)})"
end

Private Instance Methods

duration_for_entries(entries) click to toggle source
# File lib/what_have_i_done/runner.rb, line 36
def duration_for_entries(entries)
  entries.map { |entry| entry["duration"] }.inject(0, &:+)
end
seconds_as_hours(seconds) click to toggle source
# File lib/what_have_i_done/runner.rb, line 44
def seconds_as_hours(seconds)
  (seconds / (60.0 * 60.0)).round(2).to_s + "h"
end
seconds_as_time(seconds) click to toggle source
# File lib/what_have_i_done/runner.rb, line 40
def seconds_as_time(seconds)
  Time.at(seconds).utc.strftime("%H:%M:%S")
end
subtitle(txt) click to toggle source
# File lib/what_have_i_done/runner.rb, line 48
def subtitle(txt)
  puts Rainbow("# #{txt}").blue
end
text(txt) click to toggle source
# File lib/what_have_i_done/runner.rb, line 56
def text(txt)
  puts txt
end
title(txt) click to toggle source
# File lib/what_have_i_done/runner.rb, line 52
def title(txt)
  puts Rainbow("# #{txt}").green
end