class Quarterweek

Public Class Methods

to_ical(code) click to toggle source
# File lib/quarterweek.rb, line 8
def self.to_ical(code)
        @url = "http://roosters.saxion.nl"
        @data = [];
        @doc = Nokogiri::HTML(open("#{@url}/teacher/teacher:#{code}"))

        @nodeSet = @doc.css("div.span6:nth-child(2) > table:nth-child(1) tr")
        2.times do @nodeSet.delete(@nodeSet[0]) end

        @nodeSet.each do |node|
                @data << {
                        q: node.css("td:nth-child(1) a").text,
                        w: node.css("td:nth-child(2)").text.to_i,
                        y: node.css("td:nth-child(3)").text.to_i,
                        l: node.css("td:nth-child(1) a").map { |link| link['href'] }.first
                }
        end

        @cal = Icalendar::Calendar.new

        @cal.timezone do |t|
          t.tzid = "Europe/Amsterdam"

          t.daylight do |d|
            d.tzoffsetfrom = "+0100"
            d.tzoffsetto   = "+0200"
            d.tzname       = "CEST"
            d.dtstart      = "19700329T020000"
            d.rrule        = "FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU"
          end

          t.standard do |s|
            s.tzoffsetfrom = "+0200"
            s.tzoffsetto   = "+0100"
            s.tzname       = "CET"
            s.dtstart      = "19701025T030000"
            s.rrule        = "FREQ=YEARLY;BYMONTH=11;BYDAY=-1SU"
          end
        end

        @data.each do |hash|
                @cal.event do |e|            
                        start_date  = Date.commercial(hash[:y], hash[:w], 1).strftime("%Y%m%d")
                        end_date            = Date.commercial(hash[:y], hash[:w], 6).strftime("%Y%m%d")
                        e.dtstart           = Icalendar::Values::Date.new(start_date)
                        e.dtend                     = Icalendar::Values::Date.new(end_date)
                        e.summary           = "Kwartielweek: #{hash[:q]}"
                        e.url                       = @url + hash[:l]
                end
        end

        @cal.publish
        @cal.to_ical
end