blob: eab3759ffc93e96cead5899a3fd3f6013177f4d0 (
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
require 'rubygems'
require 'bundler/setup'
require 'date'
require 'active_support/core_ext/date_and_time/calculations'
require 'icalendar/recurrence'
require 'open-uri'
require 'json'
class SublabCalendar
URL = "https://sublab.org:5232/calendars/events"
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.end_of_month]
end
end
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
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 recurring
events.select(&:"recurring?")
end
def next(count=1)
future.take(count)
end
def readable(event)
Event.new(event)
end
end
|