从Rails模型动态创建iCal/ICS文件

8
鉴于我在Ruby on 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文件提供给用户?
1个回答

21

icalendar宝石运行良好。 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接