http://github.com/icalendar/icalendar
より良いドキュメントは今後も提供される予定ですが、それまでの間、1.x から 2.0 への移行に必要な変更は、README の更新に必要な diff にまとめられています。
iCalendar は、RFC-5545 で定義された iCalendar 形式の iCalendar ファイルを扱うための Ruby ライブラリです。
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 を解析して表示できるクライアントの場合は、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
のバージョンやオペレーティング システムによっては、 tzinfo-data
gem も必要になる場合があります。
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 } "
Web アプリやその他の対話型アプリケーションでは、変更を加えたり詳細を取得したりするために、カレンダー内のアイテムを検索する必要があることがよくあります。すべてのコンポーネントに自動的に関連付けられた一意の 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 )
単体テストで、実行したいほとんどのことの例を確認できますが、サンプルコードを送っていただくか、何が足りないのかをお知らせください。
このライブラリの最新リリース バージョンは、次の場所にあります。
すべては Rubygem に関するものです。
$ gem install icalendar
テストを実行するには:
$ bundle install
$ rake spec
このライブラリは、Ruby 自体と同じライセンスの下でリリースされています。
リベースされたトピック ブランチからプル リクエストを送信し、すべてのバグと機能のテストを含めてください。
このプロジェクトの貢献者および保守者として、私たちは問題の報告、機能リクエストの投稿、ドキュメントの更新、プル リクエストやパッチの送信、その他の活動を通じて貢献するすべての人々を尊重することを誓います。
私たちは、経験のレベル、性別、性自認と性表現、性的指向、障害、外見、体の大きさ、人種、民族、年齢、宗教に関わらず、誰もがハラスメントのないプロジェクトに参加できるよう努めます。
参加者による容認できない行為の例としては、性的な言葉や画像の使用、軽蔑的なコメントや個人攻撃、荒らし行為、公的または私的な嫌がらせ、侮辱、その他のプロフェッショナルでない行為が挙げられます。
プロジェクト管理者は、この行動規範に従わないコメント、コミット、コード、Wiki 編集、問題、その他の投稿を削除、編集、または拒否する権利と責任を有します。行動規範に従わないプロジェクト管理者は、プロジェクト チームから削除される場合があります。
この行動規範は、プロジェクト スペース内と、個人がプロジェクトまたはそのコミュニティを代表する場合の公共スペースの両方に適用されます。
虐待、嫌がらせ、またはその他の容認できない行為の事例は、問題を開くか、1 人以上のプロジェクト管理者に連絡することによって報告できます。
この行動規範は、http://contributor-covenant.org/version/1/1/0/ で入手可能な Contributor Covenant バージョン 1.1.0 から改変されたものです。