summaryrefslogtreecommitdiff
path: root/lib/sublab_calendar/calendar.rb
blob: e136ff585da11d7c3b45824f09a9f61a1793a87b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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