Set recurring events

If we know the birth year, recur from there. Otherwise, recur from the
current year.
This commit is contained in:
Trevor Vallender 2024-03-03 20:38:27 +00:00
parent 57024374ee
commit 337f3aa1ca
1 changed files with 15 additions and 6 deletions

View File

@ -59,26 +59,35 @@ class Birthdaze < Thor
@calendar = Icalendar::Calendar.new @calendar = Icalendar::Calendar.new
birthdays.each do |birthday| birthdays.each do |birthday|
@calendar.event do |event| @calendar.event do |event|
event.dtstart = Icalendar::Values::Date.new(start_date(birthday, Date.today.year)) event.dtstart = Icalendar::Values::Date.new(start_date(birthday))
event.dtend = Icalendar::Values::Date.new(end_date(birthday, Date.today.year)) event.dtend = Icalendar::Values::Date.new(end_date(birthday))
event.summary = summary(birthday) event.summary = summary(birthday)
event.description = description(birthday)
event.rrule = "FREQ=YEARLY;"
end end
end end
@calendar.publish @calendar.publish
end end
# Takes a birthday string, with or without a year, and returns a start date # Takes a birthday string, with or without a year, and returns a start date
def start_date(birthday, year) def start_date(birthday)
year = birthday[:birth_year] || Date.today.year
"#{year}#{birthday[:month]}#{birthday[:day]}" "#{year}#{birthday[:month]}#{birthday[:day]}"
end end
def end_date(birthday, year) def end_date(birthday)
date = Date.parse(start_date(birthday, year)).next_day date = Date.parse(start_date(birthday)).next_day
date.strftime("%Y%m%d") date.strftime("%Y%m%d")
end end
def summary(birthday) def summary(birthday)
return "🎂 #{birthday[:name]}s birthday" "🎂 #{birthday[:name]}s birthday"
end
def description(birthday)
return "" unless birthday[:birth_year]
"#{birthday[:name]} was born in #{birthday[:birth_year]}"
end end
def set_reminders def set_reminders