Sha256: 5d460397c1684c35c048d086a1afa3de0ca79398c55ca908d2ca283a1cde1ebb

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

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 = @race_date - (data.size - 1).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    = "PRIVATE"

          e.alarm do |a|
            a.summary = activity
            a.trigger = "-P1DT0H0M0S" # 1 day before
          end
        end
      end

      cal.publish
      cal.to_ical
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
run_ical-0.2.0 lib/run_ical.rb
run_ical-0.1.0 lib/run_ical.rb