从 rails 模型动态创建一个 ical / ics 文件

dynamically create an ical / ics file from a rails model

考虑到我在 rails 的 ruby 中有以下事件数据作为散列:

event = {
  start_at: Time.now(),
  end_at: Time.now() + 3600,
  summary: 'My meeting',
  description: 'A zoom meeting about food',
  event_url: 'https://food.example.com',
  formatted_address: ''
}

如何通过动态创建的 ical/ics 文件将该信息传递给用户?

icalendar gem 运行良好。 https://github.com/icalendar/icalendar

我用它来生成 outlook 和 ical 版本。效果很好。

cal = Icalendar::Calendar.new
filename = "Foo at #{foo.name}"

if params[:format] == 'vcs'
  cal.prodid = '-//Microsoft Corporation//Outlook MIMEDIR//EN'
  cal.version = '1.0'
  filename += '.vcs'
else # ical
  cal.prodid = '-//Acme Widgets, Inc.//NONSGML ExportToCalendar//EN'
  cal.version = '2.0'
  filename += '.ics'
end

cal.event do |e|
  e.dtstart     = Icalendar::Values::DateTime.new(foo.start_at, tzid: foo.time_zone)
  e.dtend       = Icalendar::Values::DateTime.new(foo.end_at, tzid: foo.course.time_zone)
  e.summary     = foo.summary
  e.description = foo.description
  e.url         = event_url(foo)
  e.location    = foo.formatted_address
end

send_data cal.to_ical, type: 'text/calendar', disposition: 'attachment', filename: filename