From bac4f137c3967fb343f5800346bb86ed392e34a2 Mon Sep 17 00:00:00 2001 From: Absurdon Date: Mon, 3 Apr 2017 01:02:09 +0200 Subject: [PATCH] Made Calendar Integration failsafe and use google-api-client gem --- app/helpers/application_helper.rb | 17 +-- app/services/google_calendar.rb | 150 +++++-------------------- app/views/layouts/application.html.erb | 2 +- app/views/widgets/_calendar.html.erb | 20 ++-- 4 files changed, 46 insertions(+), 143 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b62ef42..2ce2609 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/services/google_calendar.rb b/app/services/google_calendar.rb index a965a12..1c9be13 100644 --- a/app/services/google_calendar.rb +++ b/app/services/google_calendar.rb @@ -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, '\2'). - gsub(nsltv_regex, '').html_safe - end - - def [](key) - @entry[key] - end - - def method_missing(method) - self[method.to_s] - end - end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d4a1dc3..2d49571 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -22,7 +22,7 @@ diff --git a/app/views/widgets/_calendar.html.erb b/app/views/widgets/_calendar.html.erb index 90ab71d..c6794aa 100644 --- a/app/views/widgets/_calendar.html.erb +++ b/app/views/widgets/_calendar.html.erb @@ -3,14 +3,14 @@

<%= t('widget.schedule') %>

- <% upcoming_matches.group_by{ |e| e.start.month }.each do |month, events| %> - <% events.group_by { |e| e.start.day }.each do |day, day_events| %> -
<%= day_events.first.start.strftime("%A, %e %B") %>
+ <% 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| %> +
<%= event_start_time(day_events.first).strftime("%A, %e %B") %>
<% day_events.each do |event| %>

- <%= event.start.strftime("%H:%M %Z") %> - <%= event.formatted_summary %> + <%= event_start_time(event).strftime("%H:%M %Z") %> + <%= event.summary %>

<% end %> @@ -24,14 +24,14 @@

NSLTV

- <% upcoming_nsltv.group_by{ |e| e.start.month }.each do |month, events| %> - <% events.group_by { |e| e.start.day }.each do |day, day_events| %> -
<%= day_events.first.start.strftime("%A, %e %B") %>
+ <% 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| %> +
<%= event_start_time(day_events.first).strftime("%A, %e %B") %>
<% day_events.each do |event| %>

- <%= event.start.strftime("%H:%M %Z") %> - <%= event.formatted_summary %> + <%= event_start_time(event).strftime("%H:%M %Z") %> + <%= event.summary %>

<% end %>