Class: CalInvite::Providers::IcsContent

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

Overview

ICS content provider for generating calendar files in iCalendar format. This provider focuses on generating standards-compliant ICS content that can be used directly or wrapped for file download.

Examples:

Generate ICS content for a single event

event = CalInvite::Event.new(
  title: "Team Meeting",
  start_time: Time.now,
  end_time: Time.now + 3600
)
generator = CalInvite::Providers::IcsContent.new(event)
ics_content = generator.generate

Generate ICS content for a multi-day event

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::IcsContent.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 all event details. Handles both single events and multi-day sessions.

Returns:

  • (String)

    The complete ICS calendar content in iCalendar format



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cal_invite/providers/ics_content.rb', line 33

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

  if 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