diff options
-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 |