require "run_ical/version" Dir[File.expand_path("../run_ical/data/*.rb", __FILE__)].each do |file| require file end require "icalendar" module RunIcal class Generator def self.generate(race_date:, program:) unless RunIcal::Data.constants.include?(program.to_sym) raise ArgumentError, "#{program} is unsupported" end program = program.to_sym new(race_date: race_date, program: program).generate end def initialize(race_date:, program:) @race_date = race_date @program = RunIcal::Data.const_get(program) end def generate description = @program.const_get(:DESCRIPTION) data = @program.const_get(:DATA) cal = Icalendar::Calendar.new cal.x_wr_calname = "#{description} for #{@race_date}" start_date_padding = @race_date.sunday? ? 1 : 2 start_date = @race_date - (data.size - start_date_padding).days (start_date..@race_date).to_a.each.with_index do |date, index| activity = data[index][:activity] cal.event do |e| e.dtstart = Icalendar::Values::Date.new(date) e.dtend = Icalendar::Values::Date.new(date) e.summary = activity e.ip_class = "PUBLIC" e.alarm do |a| a.summary = activity a.trigger = "-P1DT0H0M0S" # 1 day before end end end cal.publish cal.to_ical end end end