mirror of
https://github.com/ENSL/ensl.org.git
synced 2025-01-27 03:30:54 +00:00
Made Calendar Integration failsafe and use google-api-client gem
This commit is contained in:
parent
687a50ab6a
commit
bac4f137c3
4 changed files with 46 additions and 143 deletions
|
@ -171,19 +171,20 @@ module ApplicationHelper
|
||||||
end
|
end
|
||||||
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
|
def upcoming_matches
|
||||||
GoogleCalendar.new(ENV['GOOGLE_CALENDAR_ID'], timezone_offset).
|
calendar.upcoming || []
|
||||||
upcoming.sort_by do |event|
|
|
||||||
event.start
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def upcoming_nsltv
|
def upcoming_nsltv
|
||||||
GoogleCalendar.new(ENV['GOOGLE_CALENDAR_ID'], timezone_offset).
|
calendar.upcoming_nsltv || []
|
||||||
upcoming_nsltv.sort_by do |event|
|
|
||||||
event.start
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def latest_rules
|
def latest_rules
|
||||||
|
|
|
@ -1,144 +1,46 @@
|
||||||
|
require 'google/apis/calendar_v3'
|
||||||
|
|
||||||
|
CALENDAR = Google::Apis::CalendarV3
|
||||||
|
|
||||||
class GoogleCalendar
|
class GoogleCalendar
|
||||||
attr_accessor :timezone
|
|
||||||
|
|
||||||
def initialize(id, timezone_offset = Time.zone.name)
|
def initialize (calendar_id, timezone_offset = Time.zone.name)
|
||||||
@id = id
|
@id = calendar_id
|
||||||
@timezone_offset = timezone_offset
|
@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
|
end
|
||||||
|
|
||||||
def summary
|
|
||||||
list.summary
|
|
||||||
end
|
|
||||||
|
|
||||||
def timezone
|
|
||||||
list.timezone
|
|
||||||
end
|
|
||||||
|
|
||||||
def events
|
|
||||||
list.events
|
|
||||||
end
|
|
||||||
|
|
||||||
def upcoming
|
def upcoming
|
||||||
events.select do |event|
|
@events && @events.select do |event|
|
||||||
event.start >= (Time.zone.now - 2.hours) &&
|
(not nsltv_regex =~ event.summary)
|
||||||
(not event.nsltv_regex =~ event.summary)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def upcoming_nsltv
|
def upcoming_nsltv
|
||||||
events.select do |event|
|
@events && @events.select do |event|
|
||||||
event.start >= (Time.zone.now - 2.hours) &&
|
(nsltv_regex =~ event.summary)
|
||||||
event.nsltv_regex =~ (event.summary)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def list
|
def query_events
|
||||||
@list ||= GoogleCalendar::Request.events_list(@id, @timezone_offset)
|
list = nil
|
||||||
end
|
@events.nil? and @service.list_events(@id, time_zone: ActiveSupport::TimeZone::MAPPING[@timezone_offset], time_min: (2.hours.ago).utc.iso8601 ) {|result, err|
|
||||||
end
|
if err
|
||||||
|
puts err.inspect
|
||||||
class GoogleCalendar
|
else
|
||||||
class Request
|
list = result
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
end
|
}
|
||||||
|
@events = (list) ? list.items : nil
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class EventList
|
def nsltv_regex
|
||||||
attr_reader :summary, :events, :timezone
|
/\[NSLTV\]/i
|
||||||
|
|
||||||
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
|
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div id="sidebar">
|
<div id="sidebar">
|
||||||
<%= yield :sidebar %>
|
<%= yield :sidebar %>
|
||||||
<% ['shoutbox', 'posts', 'poll'].each do |widget| %>
|
<% ['calendar','shoutbox', 'posts', 'poll'].each do |widget| %>
|
||||||
<%= render partial: "widgets/#{widget}" %>
|
<%= render partial: "widgets/#{widget}" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -3,14 +3,14 @@
|
||||||
<h4><%= t('widget.schedule') %></h4>
|
<h4><%= t('widget.schedule') %></h4>
|
||||||
|
|
||||||
<div class="widget-content-wrapper">
|
<div class="widget-content-wrapper">
|
||||||
<% upcoming_matches.group_by{ |e| e.start.month }.each do |month, events| %>
|
<% upcoming_matches.group_by{ |e| e.start.date_time.month }.each do |month, events| %>
|
||||||
<% events.group_by { |e| e.start.day }.each do |day, day_events| %>
|
<% events.group_by { |e| e.start.date_time.day }.each do |day, day_events| %>
|
||||||
<div class="separator"><%= day_events.first.start.strftime("%A, %e %B") %></div>
|
<div class="separator"><%= event_start_time(day_events.first).strftime("%A, %e %B") %></div>
|
||||||
<% day_events.each do |event| %>
|
<% day_events.each do |event| %>
|
||||||
<div class="entry">
|
<div class="entry">
|
||||||
<p class="summary">
|
<p class="summary">
|
||||||
<span class="time"><%= event.start.strftime("%H:%M %Z") %></span>
|
<span class="time"><%= event_start_time(event).strftime("%H:%M %Z") %></span>
|
||||||
<%= event.formatted_summary %>
|
<%= event.summary %>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -24,14 +24,14 @@
|
||||||
<h4>NSLTV</h4>
|
<h4>NSLTV</h4>
|
||||||
|
|
||||||
<div class="widget-content-wrapper">
|
<div class="widget-content-wrapper">
|
||||||
<% upcoming_nsltv.group_by{ |e| e.start.month }.each do |month, events| %>
|
<% upcoming_nsltv.group_by{ |e| e.start.date_time.month }.each do |month, events| %>
|
||||||
<% events.group_by { |e| e.start.day }.each do |day, day_events| %>
|
<% events.group_by { |e| e.start.date_time.day }.each do |day, day_events| %>
|
||||||
<div class="separator"><%= day_events.first.start.strftime("%A, %e %B") %></div>
|
<div class="separator"><%= event_start_time(day_events.first).strftime("%A, %e %B") %></div>
|
||||||
<% day_events.each do |event| %>
|
<% day_events.each do |event| %>
|
||||||
<div class="entry">
|
<div class="entry">
|
||||||
<p class="summary">
|
<p class="summary">
|
||||||
<span class="time"><%= event.start.strftime("%H:%M %Z") %></span>
|
<span class="time"><%= event_start_time(event).strftime("%H:%M %Z") %></span>
|
||||||
<%= event.formatted_summary %>
|
<%= event.summary %>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
Loading…
Reference in a new issue