Class: TimesheetNags::Nagger

Inherits:
Object
  • Object
show all
Defined in:
lib/timesheet_nags/nagger.rb

Overview

Growl me if my timesheets are outdated

Instance Method Summary collapse

Instance Method Details

#age_of_timestamp(line) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/timesheet_nags/nagger.rb', line 59

def age_of_timestamp(line)
  last_log_date = Date.parse(line)

  age = (Date.today - last_log_date).to_i

  day_of_week = Date.today.to_time.wday

  if age < 2
    puts 'Time log status: good'
  elsif day_of_week < 2
    puts "No new logs, but it's a monday"
  else
    puts 'Start a new time log, dummy'
  end
end

#check_and_nagObject



8
9
10
11
12
13
14
# File 'lib/timesheet_nags/nagger.rb', line 8

def check_and_nag
  if should_nag?
    send_nag("Your timesheet is out of date 😭 Age: #{latest_timestamp_age} days.", is_good: false)
  else
    send_nag('You rock at timesheets!')
  end
end

#latest_timesheetsObject

Raises:

  • (StandardError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/timesheet_nags/nagger.rb', line 37

def latest_timesheets
  uri = URI('https://api.harvestapp.com/v2/time_entries')

  req = Net::HTTP::Get.new(uri)

  req['Authorization'] = 'Bearer ' + ENV.fetch('HARVEST_TOKEN')
  req['Harvest-Account-Id'] = ENV.fetch('HARVEST_ACCOUNT_ID')

  @_res ||= Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  res = @_res

  raise StandardError, "Bad HTTP request: #{res.body}" unless res.code == '200'

  body = res.body
  parsed = JSON.parse(body)

  parsed.fetch('time_entries')
end

#latest_timestamp_ageObject



21
22
23
24
25
26
# File 'lib/timesheet_nags/nagger.rb', line 21

def latest_timestamp_age
  sheet = latest_timesheets.first
  stamp = sheet.fetch('spent_date')

  (Date.today - Date.parse(stamp)).to_i
end

#send_nag(nag = 'Your timesheets are a mystery!', is_good: true) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/timesheet_nags/nagger.rb', line 28

def send_nag(nag = 'Your timesheets are a mystery!', is_good: true)
  if is_good
    args = "osascript -e 'display notification \" 💚 #{nag} 💚 \" with title \"Time Sheet Update\"'"
  else
    args = "osascript -e 'display notification \" 🛑 #{nag} 🛑 \" with title \"Time Sheet Update\"'"
  end
  IO.popen(args).close
end

#should_nag?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/timesheet_nags/nagger.rb', line 16

def should_nag?
  day_of_week = Date.today.to_time.wday
  latest_timestamp_age > 0 && day_of_week > 1
end