Class: CalInvite::Providers::Ics

Inherits:
BaseProvider show all
Defined in:
lib/cal_invite/providers/ics.rb

Overview

Generic ICS provider for generating standard iCalendar (.ics) files. This provider generates ICS files that are compatible with most calendar applications. Supports all-day events, regular events, and multi-day sessions with proper timezone handling.

Examples:

Creating a regular event ICS file

event = CalInvite::Event.new(
  title: "Team Meeting",
  start_time: Time.now,
  end_time: Time.now + 3600,
  timezone: 'America/New_York'
)
ics = CalInvite::Providers::Ics.new(event)
ics_content = ics.generate

Creating an all-day event ICS file

event = CalInvite::Event.new(
  title: "Company Holiday",
  all_day: true,
  start_time: Date.today,
  end_time: Date.today + 1
)
ics_content = CalInvite::Providers::Ics.new(event).generate

Creating a multi-day event ICS file

event = CalInvite::Event.new(
  title: "Conference",
  multi_day_sessions: [
    { start_time: Time.parse("2024-04-01 09:00"), end_time: Time.parse("2024-04-01 17:00") },
    { start_time: Time.parse("2024-04-02 09:00"), end_time: Time.parse("2024-04-02 17:00") }
  ]
)
ics_content = CalInvite::Providers::Ics.new(event).generate

Instance Attribute Summary

Attributes inherited from BaseProvider

#event, #method

Instance Method Summary collapse

Methods inherited from BaseProvider

#initialize

Constructor Details

This class inherits a constructor from BaseProvider

Instance Method Details

#generateString

Generates the complete ICS calendar content with proper calendar properties. Handles all event types: all-day, regular, and multi-day sessions.

Returns:

  • (String)

    The complete ICS calendar content in iCalendar format



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
68
69
70
# File 'lib/cal_invite/providers/ics.rb', line 43

def generate
  calendar_lines = [
    "BEGIN:VCALENDAR",
    "VERSION:2.0",
    "PRODID:-//CalInvite//EN",
    "CALSCALE:GREGORIAN",
    "METHOD:#{method_value}"
  ]

  calendar_lines << "X-WR-CALNAME:#{escape_text(event.calendar_name)}" if event.calendar_name

  unless event.all_day
    calendar_lines.concat(vtimezone_lines || [])
  end

  if event.all_day
    calendar_lines.concat(generate_all_day_event)
  elsif event.multi_day_sessions.any?
    event.multi_day_sessions.each do |session|
      calendar_lines.concat(generate_vevent(session[:start_time], session[:end_time]))
    end
  else
    calendar_lines.concat(generate_vevent(event.start_time, event.end_time))
  end

  calendar_lines << "END:VCALENDAR"
  calendar_lines.join("\r\n")
end