diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sublab_calendar.rb | 23 | ||||
-rw-r--r-- | lib/sublab_calendar/calendar.rb | 48 | ||||
-rw-r--r-- | lib/sublab_calendar/event.rb | 39 | ||||
-rw-r--r-- | lib/sublab_calendar/occurrence.rb | 38 |
4 files changed, 148 insertions, 0 deletions
diff --git a/lib/sublab_calendar.rb b/lib/sublab_calendar.rb new file mode 100644 index 0000000..66de90d --- /dev/null +++ b/lib/sublab_calendar.rb @@ -0,0 +1,23 @@ +require 'rubygems' +require 'bundler/setup' + +require 'date' +require 'active_support/core_ext/date_and_time/calculations' +require 'icalendar/recurrence' +require 'httpclient' +require 'httpi' +require 'json' + +require 'sublab_calendar/calendar' +require 'sublab_calendar/event' +require 'sublab_calendar/occurrence' + +module SublabCalendar + + URL = "https://dav.nashipur.org/cal.php/calendars/sublars/events?export" + USER = "sublars" + PASS = "sublars14" + + HTTPI.adapter = :httpclient + +end diff --git a/lib/sublab_calendar/calendar.rb b/lib/sublab_calendar/calendar.rb new file mode 100644 index 0000000..e136ff5 --- /dev/null +++ b/lib/sublab_calendar/calendar.rb @@ -0,0 +1,48 @@ +module SublabCalendar + + class Calendar + + attr_reader :calendar + + def initialize(ical) + @calendar = Icalendar.parse(ical).first + end + + def self.load(url=URL) + request = HTTPI::Request.new + request.url = URL + request.auth.digest(USER, PASS) + request.auth.ssl.verify_mode = :none + response = HTTPI.get(request) + + ical = response.body + 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 recurring + events.select(&:"recurring?") + end + + def next(count=1) + future.take(count) + end + + def readable(event) + SublabCalendar::Event.new(event) + end + + end + +end diff --git a/lib/sublab_calendar/event.rb b/lib/sublab_calendar/event.rb new file mode 100644 index 0000000..d350d51 --- /dev/null +++ b/lib/sublab_calendar/event.rb @@ -0,0 +1,39 @@ +module SublabCalendar + + class Event < SimpleDelegator + + BASIC_ATTRIBUTES = [:summary, :dtstart, :dtend] + + def to_h + BASIC_ATTRIBUTES.inject({}) {|hsh, attr| hsh[attr] = send(attr).to_s; hsh } + end + + def to_json(*args) + to_h.to_json(*args) + end + + def to_s + "<#{self.class} #{self.to_h}>" + end + + def inspect + to_s + end + + def recurring? + ! rrule.empty? + end + + def occurrences_this_month + return nil unless recurring? + occurrences_between(*this_month).map {|occ| Occurrence.new(occ, self) } + end + + private + + def this_month + [Date.today.beginning_of_month, Date.today.next_month.beginning_of_month] + end + + end +end diff --git a/lib/sublab_calendar/occurrence.rb b/lib/sublab_calendar/occurrence.rb new file mode 100644 index 0000000..fad58a8 --- /dev/null +++ b/lib/sublab_calendar/occurrence.rb @@ -0,0 +1,38 @@ +module SublabCalendar + + class Occurrence < SimpleDelegator + + attr_reader :event + + def initialize(occurrence, event) + @event = event + super(occurrence) + end + + def to_h + { + summary: summary, + start: start_time, + end: end_time + } + end + + def inspect + to_s + end + + def summary + event.summary + end + + def description + event.description + end + + def to_s + "<#{self.class} #{self.to_h}>" + end + + end + +end |