Made Calendar Integration failsafe and use google-api-client gem

This commit is contained in:
Absurdon 2017-04-03 01:02:09 +02:00
parent 6d5536ea94
commit b411f559b8
4 changed files with 46 additions and 143 deletions

View file

@ -171,19 +171,20 @@ module ApplicationHelper
end
end
def calendar
@calendar ||= GoogleCalendar.new(ENV['GOOGLE_CALENDAR_ID'], timezone_offset)
end
def event_start_time event
event.start.date_time.to_datetime.in_time_zone(timezone_offset)
end
def upcoming_matches
GoogleCalendar.new(ENV['GOOGLE_CALENDAR_ID'], timezone_offset).
upcoming.sort_by do |event|
event.start
end
calendar.upcoming || []
end
def upcoming_nsltv
GoogleCalendar.new(ENV['GOOGLE_CALENDAR_ID'], timezone_offset).
upcoming_nsltv.sort_by do |event|
event.start
end
calendar.upcoming_nsltv || []
end
def latest_rules

View file

@ -1,144 +1,46 @@
require 'google/apis/calendar_v3'
CALENDAR = Google::Apis::CalendarV3
class GoogleCalendar
attr_accessor :timezone
def initialize(id, timezone_offset = Time.zone.name)
@id = id
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
end
def summary
list.summary
end
def timezone
list.timezone
end
def events
list.events
end
def upcoming
events.select do |event|
event.start >= (Time.zone.now - 2.hours) &&
(not event.nsltv_regex =~ event.summary)
@events && @events.select do |event|
(not nsltv_regex =~ event.summary)
end
end
def upcoming_nsltv
events.select do |event|
event.start >= (Time.zone.now - 2.hours) &&
event.nsltv_regex =~ (event.summary)
@events && @events.select do |event|
(nsltv_regex =~ event.summary)
end
end
def list
@list ||= GoogleCalendar::Request.events_list(@id, @timezone_offset)
end
end
class GoogleCalendar
class Request
BASE_URL = "https://www.googleapis.com/"
EVENTS_ENDPOINT = "events"
def self.events_list(id, timezone_offset)
request = self.new(id, EVENTS_ENDPOINT)
GoogleCalendar::EventList.new(request.parsed_response, timezone_offset)
end
def initialize(id, endpoint)
@id = id
@endpoint = endpoint
@response = get_data
end
def parsed_response
JSON.parse(@response.body)
end
private
def get_data
Rails.cache.fetch(cache_key, expires_in: 5.minutes) do
if Rails.env.development?
conn = Faraday.new(BASE_URL, ssl: {verify: false})
else
conn = Faraday.new(BASE_URL)
end
conn.get(request_url)
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
end
end
def cache_key
"/google_calendar/#{@id}/#{@endpoint}"
end
def request_url
#The default number of events pulled is 250.
#We reached that number and events didn't show any more.
#So now I filter all events that have a start time
#that is longer ago then 7 days.
#Alternative: maxResults=2500
time_min = (Time.now - 7.days).utc.iso8601
"calendar/v3/calendars/#{@id}/#{@endpoint}/?key=#{ENV['GOOGLE_API_KEY']}&timeMin=#{time_min}"
end
}
@events = (list) ? list.items : nil
end
class EventList
attr_reader :summary, :events, :timezone
def initialize(list, timezone_offset)
@list = list
@timezone_offset = timezone_offset
parse_list
parse_events
end
private
def parse_list
@summary = @list["summary"]
@timezone = @list["timeZone"]
end
def parse_events
@events = @list["items"].map do |item|
GoogleCalendar::Event.new(item, @timezone_offset)
end
end
def nsltv_regex
/\[NSLTV\]/i
end
class Event
def initialize(entry, timezone_offset)
@entry = entry
@timezone_offset = timezone_offset
end
def start
@entry["start"]["dateTime"].to_datetime.in_time_zone(@timezone_offset)
end
def end
@entry["end"]["dateTime"].to_datetime.in_time_zone(@timezone_offset)
end
def nsltv_regex
/\[NSLTV\]/i
end
def formatted_summary
summary.gsub(/(http\:\/\/)(.*[^)])/i, '<a href="\1\2">\2</a>').
gsub(nsltv_regex, '').html_safe
end
def [](key)
@entry[key]
end
def method_missing(method)
self[method.to_s]
end
end
end

View file

@ -22,7 +22,7 @@
</div>
<div id="sidebar">
<%= yield :sidebar %>
<% ['shoutbox', 'posts', 'poll'].each do |widget| %>
<% ['calendar','shoutbox', 'posts', 'poll'].each do |widget| %>
<%= render partial: "widgets/#{widget}" %>
<% end %>
</div>

View file

@ -3,14 +3,14 @@
<h4><%= t('widget.schedule') %></h4>
<div class="widget-content-wrapper">
<% upcoming_matches.group_by{ |e| e.start.month }.each do |month, events| %>
<% events.group_by { |e| e.start.day }.each do |day, day_events| %>
<div class="separator"><%= day_events.first.start.strftime("%A, %e %B") %></div>
<% upcoming_matches.group_by{ |e| e.start.date_time.month }.each do |month, events| %>
<% events.group_by { |e| e.start.date_time.day }.each do |day, day_events| %>
<div class="separator"><%= event_start_time(day_events.first).strftime("%A, %e %B") %></div>
<% day_events.each do |event| %>
<div class="entry">
<p class="summary">
<span class="time"><%= event.start.strftime("%H:%M %Z") %></span>
<%= event.formatted_summary %>
<span class="time"><%= event_start_time(event).strftime("%H:%M %Z") %></span>
<%= event.summary %>
</p>
</div>
<% end %>
@ -24,14 +24,14 @@
<h4>NSLTV</h4>
<div class="widget-content-wrapper">
<% upcoming_nsltv.group_by{ |e| e.start.month }.each do |month, events| %>
<% events.group_by { |e| e.start.day }.each do |day, day_events| %>
<div class="separator"><%= day_events.first.start.strftime("%A, %e %B") %></div>
<% upcoming_nsltv.group_by{ |e| e.start.date_time.month }.each do |month, events| %>
<% events.group_by { |e| e.start.date_time.day }.each do |day, day_events| %>
<div class="separator"><%= event_start_time(day_events.first).strftime("%A, %e %B") %></div>
<% day_events.each do |event| %>
<div class="entry">
<p class="summary">
<span class="time"><%= event.start.strftime("%H:%M %Z") %></span>
<%= event.formatted_summary %>
<span class="time"><%= event_start_time(event).strftime("%H:%M %Z") %></span>
<%= event.summary %>
</p>
</div>
<% end %>