From b4ca05a9aad421fa2d0a7b55564198f998e93b85 Mon Sep 17 00:00:00 2001 From: Lars Henrik Mai Date: Sat, 3 May 2014 13:30:09 +0200 Subject: simple calendar class --- sublab_calendar.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 sublab_calendar.rb 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 -- cgit v1.2.1