Class: RunIcal::Generator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(race_date:, program:) ⇒ Generator

Returns a new instance of Generator.



18
19
20
21
# File 'lib/run_ical.rb', line 18

def initialize(race_date:, program:)
  @race_date = race_date
  @program = RunIcal::Data.const_get(program)
end

Class Method Details

.generate(race_date:, program:) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/run_ical.rb', line 10

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

Instance Method Details

#generateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/run_ical.rb', line 23

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.saturday? ? 2 : 1
  start_date = @race_date - (data.size - start_date_padding).days

  date_list = (start_date..@race_date).to_a
  date_list.each.with_index do |date, index|
    activity = data[index][:activity]
    if activity =~ /rest/i && @race_date.saturday? && index == date_list.size - 1
      activity = data[index + 1][:activity]
    end

    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