diff options
author | Lars Henrik Mai <lars.mai@kontinui.de> | 2014-05-03 13:30:09 +0200 |
---|---|---|
committer | Lars Henrik Mai <lars.mai@kontinui.de> | 2014-05-03 13:30:09 +0200 |
commit | b4ca05a9aad421fa2d0a7b55564198f998e93b85 (patch) | |
tree | 13d6f3544350a2c34a31d62d05859397ccc3aa01 /sublab_calendar.rb | |
parent | 47b052f6eccb9cb664455d392eb8a60dd899eb7c (diff) |
simple calendar class
Diffstat (limited to 'sublab_calendar.rb')
-rw-r--r-- | sublab_calendar.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/sublab_calendar.rb b/sublab_calendar.rb new file mode 100644 index 0000000..4863759 --- /dev/null +++ b/sublab_calendar.rb @@ -0,0 +1,49 @@ +require 'rubygems' +require 'bundler/setup' + +require 'icalendar' +require 'open-uri' +require 'json' + +class SublabCalendar + + URL = "https://sublab.org:5232/calendars/events" + + Event = Struct.new(:summary, :start, :end) do + def to_json(*args) + to_h.to_json(*args) + end + end + + attr_reader :calendar + + def initialize(ical) + @calendar = Icalendar.parse(ical).first + end + + def self.load(url=URL) + ical = open(url, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}) + new(ical) + end + + def events + calendar.events.map(&method(:readable)) + end + + def future + calendar.events.select {|ev| ev.dtstart >= DateTime.now}.map(&method(:readable)) + end + + def past + calendar.events.select {|ev| ev.dtstart < DateTime.now}.map(&method(:readable)) + end + + def next(count=1) + future.take(count) + end + + def readable(event) + Event.new(event.summary, event.dtstart.to_s, event.dtend.to_s) + end + +end |