2017-04-02 23:02:09 +00:00
|
|
|
require 'google/apis/calendar_v3'
|
2014-04-01 23:07:21 +00:00
|
|
|
|
2017-04-02 23:02:09 +00:00
|
|
|
CALENDAR = Google::Apis::CalendarV3
|
2014-04-01 23:07:21 +00:00
|
|
|
|
2017-04-02 23:02:09 +00:00
|
|
|
class GoogleCalendar
|
2014-04-01 23:07:21 +00:00
|
|
|
|
2017-04-02 23:02:09 +00:00
|
|
|
def initialize (calendar_id, timezone_offset = Time.zone.name)
|
|
|
|
@id = calendar_id
|
|
|
|
@timezone_offset = timezone_offset
|
|
|
|
@events = nil
|
|
|
|
@service = CALENDAR::CalendarService.new
|
|
|
|
#@service.authorization = CALENDAR::AUTH_CALENDAR # change this if write access needed
|
|
|
|
@service.key = ENV['GOOGLE_API_KEY']
|
|
|
|
query_events
|
2014-04-01 23:07:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def upcoming
|
2017-04-02 23:02:09 +00:00
|
|
|
@events && @events.select do |event|
|
|
|
|
(not nsltv_regex =~ event.summary)
|
2015-07-19 10:33:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upcoming_nsltv
|
2017-04-02 23:02:09 +00:00
|
|
|
@events && @events.select do |event|
|
|
|
|
(nsltv_regex =~ event.summary)
|
2014-04-01 23:07:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-02 23:02:09 +00:00
|
|
|
def query_events
|
|
|
|
list = nil
|
|
|
|
@events.nil? and @service.list_events(@id, time_zone: ActiveSupport::TimeZone::MAPPING[@timezone_offset], time_min: (2.hours.ago).utc.iso8601 ) {|result, err|
|
|
|
|
if err
|
|
|
|
puts err.inspect
|
|
|
|
else
|
|
|
|
list = result
|
2014-04-01 23:07:21 +00:00
|
|
|
end
|
2017-04-02 23:02:09 +00:00
|
|
|
}
|
|
|
|
@events = (list) ? list.items : nil
|
2014-04-01 23:07:21 +00:00
|
|
|
end
|
|
|
|
|
2017-04-02 23:02:09 +00:00
|
|
|
def nsltv_regex
|
|
|
|
/\[NSLTV\]/i
|
2014-04-01 23:07:21 +00:00
|
|
|
end
|
|
|
|
|
2015-07-19 10:33:49 +00:00
|
|
|
end
|