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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/timesheet_nags/nagger.rb', line 69

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
    maybe_compliment 'You rock at timesheets!'
  end
end

#latest_timesheetsObject

Raises:

  • (StandardError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/timesheet_nags/nagger.rb', line 43

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')

  begin
  @_res ||= Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, timeout: 2) do |http|
    http.request(req)
  end
  rescue SocketError
    abort "Unable to connect to Harvest!"
  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



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

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

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

#maybe_compliment(message) ⇒ Object



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

def maybe_compliment(message)
  return unless rand(10).eql?(5)
  send_nag message
end

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



34
35
36
37
38
39
40
41
# File 'lib/timesheet_nags/nagger.rb', line 34

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)


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

def should_nag?
  tuesday_through_friday = [2,3,4,5]
  day_of_week = Date.today.to_time.wday
  latest_timestamp_age > 0 && tuesday_through_friday.include?(day_of_week)
end