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/ 取得