http://github.com/icalendar/icalendar
更好的文档尚未出现,但与此同时,从 1.x 迁移到 2.0 所需的更改已通过更新 README 所需的差异进行了总结
iCalendar 是一个 Ruby 库,用于处理 RFC-5545 定义的 iCalendar 格式的 iCalendar 文件。
require 'icalendar'
# Create a calendar with an event (standard method)
cal = Icalendar :: Calendar . new
cal . event do | e |
e . dtstart = Icalendar :: Values :: Date . new ( '20050428' )
e . dtend = Icalendar :: Values :: Date . new ( '20050429' )
e . summary = "Meeting with the man."
e . description = "Have a long lunch meeting and decide nothing..."
e . ip_class = "PRIVATE"
end
cal . publish
event = Icalendar :: Event . new
event . dtstart = DateTime . civil ( 2006 , 6 , 23 , 8 , 30 )
event . summary = "A great event!"
cal . add_event ( event )
event2 = cal . event # This automatically adds the event to the calendar
event2 . dtstart = DateTime . civil ( 2006 , 6 , 24 , 8 , 30 )
event2 . summary = "Another great event!"
params = { "altrep" => "http://my.language.net" , "language" => "SPANISH" }
event = cal . event do | e |
e . dtstart = Icalendar :: Values :: Date . new ( '20050428' )
e . dtend = Icalendar :: Values :: Date . new ( '20050429' )
e . summary = Icalendar :: Values :: Text . new "This is a summary with params." , params
end
event . summary . ical_params #=> {'altrep' => 'http://my.language.net', 'language' => 'SPANISH'}
# or
event = cal . event do | e |
e . dtstart = Icalendar :: Values :: Date . new ( '20050428' )
e . dtend = Icalendar :: Values :: Date . new ( '20050429' )
e . summary = "This is a summary with params."
e . summary . ical_params = params
end
event . summary . ical_params #=> {'altrep' => 'http://my.language.net', 'language' => 'SPANISH'}
有时我们并不关心事件的开始或结束是Date
还是DateTime
对象。为此,我们可以使用DateOrDateTime.new(value)
。对返回的DateOrDateTime
调用.call
将立即返回基础Date
或DateTime
对象。
event = cal . event do | e |
e . dtstart = Icalendar :: Values :: DateOrDateTime . new ( '20140924' )
e . dtend = Icalendar :: Values :: DateOrDateTime . new ( '20140925' ) . call
e . summary = 'This is an all-day event, because DateOrDateTime will return Dates'
end
对于可以解析并显示与事件关联的 URL 的客户端,可以分配一个。
event = cal . event do | e |
e . url = 'https://example.com'
end
cal_string = cal.to_ical
puts cal_string
cal . event do | e |
# ...other event properties
e . alarm do | a |
a . action = "EMAIL"
a . description = "This is an event reminder" # email body (required)
a . summary = "Alarm notification" # email subject (required)
a . attendee = %w( mailto:[email protected] mailto:[email protected] ) # one or more email recipients (required)
a . append_attendee "mailto:[email protected]"
a . trigger = "-PT15M" # 15 minutes before
a . append_attach Icalendar :: Values :: Uri . new ( "ftp://host.com/novo-procs/felizano.exe" , "fmttype" => "application/binary" ) # email attachments (optional)
end
e . alarm do | a |
a . action = "DISPLAY" # This line isn't necessary, it's the default
a . summary = "Alarm notification"
a . trigger = "-P1DT0H0M0S" # 1 day before
end
e . alarm do | a |
a . action = "AUDIO"
a . trigger = "-PT15M"
a . append_attach "Basso"
end
end
# BEGIN:VALARM
# ACTION:EMAIL
# ATTACH;FMTTYPE=application/binary:ftp://host.com/novo-procs/felizano.exe
# TRIGGER:-PT15M
# SUMMARY:Alarm notification
# DESCRIPTION:This is an event reminder
# ATTENDEE:mailto:[email protected]
# ATTENDEE:mailto:[email protected]
# END:VALARM
#
# BEGIN:VALARM
# ACTION:DISPLAY
# TRIGGER:-P1DT0H0M0S
# SUMMARY:Alarm notification
# END:VALARM
#
# BEGIN:VALARM
# ACTION:AUDIO
# ATTACH;VALUE=URI:Basso
# TRIGGER:-PT15M
# END:VALARM
如果警报不存在,则调用event.alarm
方法将创建一个警报。要检查事件是否有警报,请使用has_alarm?
方法。
event . has_alarm?
# => false
event . alarm
# => #<Icalendar::Alarm ... >
event . has_alarm?
#=> true
cal = Icalendar :: Calendar . new
cal . timezone do | t |
t . tzid = "America/Chicago"
t . daylight do | d |
d . tzoffsetfrom = "-0600"
d . tzoffsetto = "-0500"
d . tzname = "CDT"
d . dtstart = "19700308T020000"
d . rrule = "FREQ=YEARLY;BYMONTH=3;BYDAY=2SU"
end
t . standard do | s |
s . tzoffsetfrom = "-0500"
s . tzoffsetto = "-0600"
s . tzname = "CST"
s . dtstart = "19701101T020000"
s . rrule = "FREQ=YEARLY;BYMONTH=11;BYDAY=1SU"
end
end
# BEGIN:VTIMEZONE
# TZID:America/Chicago
# BEGIN:DAYLIGHT
# TZOFFSETFROM:-0600
# TZOFFSETTO:-0500
# TZNAME:CDT
# DTSTART:19700308T020000
# RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
# END:DAYLIGHT
# BEGIN:STANDARD
# TZOFFSETFROM:-0500
# TZOFFSETTO:-0600
# TZNAME:CST
# DTSTART:19701101T020000
# RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
# END:STANDARD
# END:VTIMEZONE
iCalendar 对从tzinfo
提取的时区信息创建 VTIMEZONE 块有一些基本支持。您必须手动要求tzinfo
支持才能利用。
iCalendar 已经过测试,可与tzinfo
版本 0.3、1.x 和 2.x 配合使用。可能还需要tzinfo-data
gem,具体取决于您的tzinfo
版本以及可能的操作系统。
require 'icalendar/tzinfo'
cal = Icalendar :: Calendar . new
event_start = DateTime . new 2008 , 12 , 29 , 8 , 0 , 0
event_end = DateTime . new 2008 , 12 , 29 , 11 , 0 , 0
tzid = "America/Chicago"
tz = TZInfo :: Timezone . get tzid
timezone = tz . ical_timezone event_start
cal . add_timezone timezone
cal . event do | e |
e . dtstart = Icalendar :: Values :: DateTime . new event_start , 'tzid' => tzid
e . dtend = Icalendar :: Values :: DateTime . new event_end , 'tzid' => tzid
e . summary = "Meeting with the man."
e . description = "Have a long lunch meeting and decide nothing..."
e . organizer = "mailto:[email protected]"
e . organizer = Icalendar :: Values :: CalAddress . new ( "mailto:[email protected]" , cn : 'John Smith' )
end
# Open a file or pass a string to the parser
cal_file = File . open ( "single_event.ics" )
# Parser returns an array of calendars because a single file
# can have multiple calendars.
cals = Icalendar :: Calendar . parse ( cal_file )
cal = cals . first
# Now you can access the cal object in just the same way I created it
event = cal . events . first
puts "start date-time: #{ event . dtstart } "
puts "start date-time timezone: #{ event . dtstart . ical_params [ 'tzid' ] } "
puts "summary: #{ event . summary } "
您还可以直接创建一个Parser
实例,这可以用于启用严格解析:
# Sometimes you want to strongly verify only rfc-approved properties are
# used
strict_parser = Icalendar :: Parser . new ( cal_file , true )
cal = strict_parser . parse
# Open a file or pass a string to the parser
event_file = File . open ( "event.ics" )
# Parser returns an array of events because a single file
# can have multiple events.
events = Icalendar :: Event . parse ( event_file )
event = events . first
puts "start date-time: #{ event . dtstart } "
puts "start date-time timezone: #{ event . dtstart . ical_params [ 'tzid' ] } "
puts "summary: #{ event . summary } "
通常,在网络应用程序和其他交互式应用程序中,您需要查找日历中的项目以进行更改或获取详细信息。现在,您可以通过与所有组件自动关联的唯一 ID 找到所有内容。
cal = Calendar . new
10 . times { cal . event } # Create 10 events with only default data.
some_event = cal . events [ 5 ] # Grab it from the array of events
# Use the uid as the key in your app
key = some_event . uid
# so later you can find it.
same_event = cal . find_event ( key )
检查单元测试以获取您想要做的大多数事情的示例,但请向我发送示例代码或让我知道缺少什么。
该库的最新发行版本可以在以下位置找到
这都是关于 rubygems 的:
$ gem install icalendar
运行测试:
$ bundle install
$ rake spec
该库是在与 Ruby 本身相同的许可证下发布的。
请从重新定位的主题分支提交拉取请求,并包含对所有错误和功能的测试。
作为该项目的贡献者和维护者,我们承诺尊重所有通过报告问题、发布功能请求、更新文档、提交拉取请求或补丁以及其他活动做出贡献的人。
我们致力于让每个人参与该项目时都获得无骚扰的体验,无论其经验水平、性别、性别认同和表达、性取向、残疾、个人外表、体型、种族、民族、年龄或宗教。
参与者不可接受的行为包括使用性语言或图像、贬损性评论或人身攻击、恶搞、公开或私下骚扰、侮辱或其他不专业行为。
项目维护者有权利和责任删除、编辑或拒绝不符合本行为准则的评论、提交、代码、wiki 编辑、问题和其他贡献。不遵守行为准则的项目维护人员可能会被从项目团队中除名。
当个人代表项目或其社区时,本行为准则适用于项目空间和公共空间。
可以通过提出问题或联系一名或多名项目维护人员来报告辱骂、骚扰或其他不可接受的行为。
本行为准则改编自贡献者契约 1.1.0 版,可从 http://contributor-covenant.org/version/1/1/0/ 获取