Class: IcsEventBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/services/ics_event_builder.rb

Overview

Builds a single-event RFC-5545 iCalendar document.

Consolidates the calendar scaffolding — the timezone block,
Icalendar::Calendar setup and event construction — shared by
SchedulerBookingMailer and SupportCase. Wraps the icalendar gem so
callers only describe the event, not the iCalendar object graph.

Examples:

A simple appointment

IcsEventBuilder.new(
  tzid: 'America/Chicago',
  starts_at: starts_at,
  ends_at: ends_at,
  summary: 'Onsite service'
).calendar

Instance Method Summary collapse

Constructor Details

#initialize(tzid:, starts_at:, ends_at:, summary:, description: nil, uid: nil, sequence: nil, location: nil, organizer: nil, attendees: [], ip_method: nil, cancelled: false) ⇒ IcsEventBuilder

Returns a new instance of IcsEventBuilder.

Parameters:

  • tzid (String)

    IANA timezone id, e.g. "America/Chicago"

  • starts_at (Time, DateTime)

    event start

  • ends_at (Time, DateTime)

    event end

  • summary (String)

    event title

  • description (String, nil) (defaults to: nil)

    event body text

  • uid (String, nil) (defaults to: nil)

    stable unique identifier for the event

  • sequence (Integer, nil) (defaults to: nil)

    revision counter, bumped on each update

  • location (String, nil) (defaults to: nil)

    event location

  • organizer (Hash, nil) (defaults to: nil)

    organizer as { email:, name: }

  • attendees (Array<String>) (defaults to: [])

    attendee email addresses

  • ip_method (Symbol, String, nil) (defaults to: nil)

    iTIP method (:request/:publish/:cancel)

  • cancelled (Boolean) (defaults to: false)

    mark the event STATUS:CANCELLED



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/ics_event_builder.rb', line 30

def initialize(tzid:, starts_at:, ends_at:, summary:, description: nil,
               uid: nil, sequence: nil, location: nil, organizer: nil,
               attendees: [], ip_method: nil, cancelled: false)
  @tzid = tzid
  @starts_at = starts_at
  @ends_at = ends_at
  @summary = summary
  @description = description
  @uid = uid
  @sequence = sequence
  @location = location
  @organizer = organizer
  @attendees = Array(attendees)
  @ip_method = ip_method
  @cancelled = cancelled
end

Instance Method Details

#calendarIcalendar::Calendar

Returns the assembled calendar.

Returns:

  • (Icalendar::Calendar)

    the assembled calendar



48
49
50
51
52
53
54
55
56
57
# File 'app/services/ics_event_builder.rb', line 48

def calendar
  require 'icalendar'
  require 'icalendar/tzinfo'

  cal = Icalendar::Calendar.new
  cal.ip_method = @ip_method.to_s.upcase if @ip_method
  cal.add_timezone(TZInfo::Timezone.get(@tzid).ical_timezone(@starts_at))
  cal.event { |event| populate(event) }
  cal
end

#to_icalString

Returns the serialized .ics payload.

Returns:

  • (String)

    the serialized .ics payload



60
61
62
# File 'app/services/ics_event_builder.rb', line 60

def to_ical
  calendar.to_ical
end