From 319b495dd0cd50131d332cc390be148b3a73cfe7 Mon Sep 17 00:00:00 2001 From: Tomoki Tsubokawa Date: Fri, 7 Oct 2016 11:33:13 +0900 Subject: [PATCH 1/2] Create getCalendar --- Gemfile | 3 + Gemfile.lock | 197 ++++++++++++++--------- app/assets/javascripts/events.js.coffee | 19 ++- app/controllers/events_controller.rb | 73 +++++++++ app/views/events/_calendar_list.html.erb | 2 +- config/routes.rb | 8 +- 6 files changed, 221 insertions(+), 81 deletions(-) diff --git a/Gemfile b/Gemfile index b8056e2..5029fc5 100644 --- a/Gemfile +++ b/Gemfile @@ -75,3 +75,6 @@ gem 'devise' gem 'omniauth' gem 'omniauth-google-oauth2' gem 'devise_invitable' + +gem 'googleauth' +gem 'google-api-client' diff --git a/Gemfile.lock b/Gemfile.lock index 0cc39bb..69a856f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,24 +27,25 @@ GEM minitest (~> 5.1) thread_safe (~> 0.1) tzinfo (~> 1.1) + addressable (2.4.0) arel (5.0.1.20140414130214) - autodoc (0.5.2) + autodoc (0.6.0) actionpack activesupport (>= 3.0.0) rspec - awesome_nested_set (3.0.2) - activerecord (>= 4.0.0, < 5) + awesome_nested_set (3.1.1) + activerecord (>= 4.0.0, < 5.1) bcrypt (3.1.11) - bootstrap3-datetimepicker-rails (4.0.0) + bootstrap3-datetimepicker-rails (4.17.42) momentjs-rails (>= 2.8.1) builder (3.2.2) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) - coffee-script (2.3.0) + coffee-script (2.4.1) coffee-script-source execjs - coffee-script-source (1.8.0) + coffee-script-source (1.10.0) commonjs (0.2.7) devise (4.2.0) bcrypt (~> 3.0) @@ -52,59 +53,87 @@ GEM railties (>= 4.1.0, < 5.1) responders warden (~> 1.2.3) - devise_invitable (1.6.0) - actionmailer (>= 3.2.6) - devise (>= 3.2.0) + devise_invitable (1.7.0) + actionmailer (>= 4.0.0) + devise (>= 4.0.0) diff-lcs (1.2.5) erubis (2.7.0) - execjs (2.2.2) - factory_girl (4.5.0) + execjs (2.7.0) + factory_girl (4.7.0) activesupport (>= 3.0.0) - factory_girl_rails (4.5.0) - factory_girl (~> 4.5.0) + factory_girl_rails (4.7.0) + factory_girl (~> 4.7.0) railties (>= 3.0.0) faraday (0.9.2) multipart-post (>= 1.2, < 3) - fullcalendar-rails (2.2.2.0) + fullcalendar-rails (2.4.0.0) jquery-rails (>= 3.1.1, < 5.0.0) - momentjs-rails (~> 2.8, >= 2.8.3) - hashie (3.4.4) + momentjs-rails (>= 2.9.0) + google-api-client (0.9.15) + addressable (~> 2.3) + googleauth (~> 0.5) + httpclient (~> 2.7) + hurley (~> 0.1) + memoist (~> 0.11) + mime-types (>= 1.6) + representable (~> 2.3.0) + retriable (~> 2.0) + googleauth (0.5.1) + faraday (~> 0.9) + jwt (~> 1.4) + logging (~> 2.0) + memoist (~> 0.12) + multi_json (~> 1.11) + os (~> 0.9) + signet (~> 0.7) + hashie (3.4.6) hike (1.2.3) - i18n (0.6.11) - icalendar (2.2.2) - jbuilder (2.2.5) - activesupport (>= 3.0.0, < 5) + httpclient (2.8.2.4) + hurley (0.2) + i18n (0.7.0) + icalendar (2.4.1) + jbuilder (2.6.0) + activesupport (>= 3.0.0, < 5.1) multi_json (~> 1.2) - jquery-rails (3.1.2) + jquery-rails (3.1.4) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) - jquery-ui-rails (5.0.3) + jquery-ui-rails (5.0.5) railties (>= 3.2.16) - json (1.8.1) - jwt (1.5.4) - kaminari (0.16.3) + json (1.8.3) + jwt (1.5.6) + kaminari (0.17.0) actionpack (>= 3.0.0) activesupport (>= 3.0.0) - knockoutjs-rails (3.3.0.1) - railties (>= 3.1, < 5) + knockoutjs-rails (3.4.0.1) + railties (>= 3.1, < 6) less (2.6.0) commonjs (~> 0.2.7) - less-rails (2.6.0) - actionpack (>= 3.1) + less-rails (2.7.1) + actionpack (>= 4.0) less (~> 2.6.0) - libv8 (3.16.14.7) - mail (2.6.3) - mime-types (>= 1.16, < 3) - mime-types (2.4.3) - mini_portile (0.6.2) - minitest (5.4.3) - momentjs-rails (2.8.3) + sprockets (> 2, < 4) + tilt + libv8 (3.16.14.15) + little-plugger (1.1.4) + logging (2.1.0) + little-plugger (~> 1.1) + multi_json (~> 1.10) + mail (2.6.4) + mime-types (>= 1.16, < 4) + memoist (0.15.0) + mime-types (3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2016.0521) + mini_portile2 (2.1.0) + minitest (5.9.1) + momentjs-rails (2.15.1) railties (>= 3.1) - multi_json (1.10.1) + multi_json (1.12.1) multi_xml (0.5.5) multipart-post (2.0.0) - nokogiri (1.6.6.2) - mini_portile (~> 0.6.0) + nokogiri (1.6.8.1) + mini_portile2 (~> 2.1.0) oauth2 (1.2.0) faraday (>= 0.8, < 0.10) jwt (~> 1.0) @@ -123,8 +152,9 @@ GEM oauth2 (~> 1.0) omniauth (~> 1.2) orm_adapter (0.5.0) - rack (1.5.2) - rack-test (0.6.2) + os (0.9.6) + rack (1.5.5) + rack-test (0.6.3) rack (>= 1.0) rails (4.1.8) actionmailer (= 4.1.8) @@ -141,32 +171,36 @@ GEM activesupport (= 4.1.8) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (10.4.0) - rdoc (4.1.2) + rake (11.3.0) + rdoc (4.2.2) json (~> 1.4) - ref (1.0.5) + ref (2.0.0) + representable (2.3.0) + uber (~> 0.0.7) responders (1.1.2) railties (>= 3.2, < 4.2) - rspec (3.1.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-core (3.1.7) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) + retriable (2.1.0) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-core (3.5.4) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.1.0) - rspec-mocks (3.1.3) - rspec-support (~> 3.1.0) - rspec-rails (3.1.0) + rspec-support (~> 3.5.0) + rspec-rails (3.5.2) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-support (~> 3.1.0) - rspec-support (3.1.2) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) sass (3.2.19) sass-rails (4.0.5) railties (>= 4.0.0, < 5.0) @@ -177,35 +211,41 @@ GEM json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) settingslogic (2.0.9) - spring (1.2.0) - sprockets (2.12.3) + signet (0.7.3) + addressable (~> 2.3) + faraday (~> 0.9) + jwt (~> 1.5) + multi_json (~> 1.10) + spring (1.7.2) + sprockets (2.12.4) hike (~> 1.2) multi_json (~> 1.0) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - sprockets-rails (2.2.1) + sprockets-rails (2.3.3) actionpack (>= 3.0) activesupport (>= 3.0) sprockets (>= 2.8, < 4.0) - sqlite3 (1.3.10) - therubyracer (0.12.1) + sqlite3 (1.3.11) + therubyracer (0.12.2) libv8 (~> 3.16.14.0) ref thor (0.19.1) - thread_safe (0.3.4) + thread_safe (0.3.5) tilt (1.4.1) - turbolinks (2.5.2) - coffee-rails - twitter-bootstrap-rails (3.2.0) - actionpack (~> 4.1) - execjs (~> 2.2) - rails (~> 4.1) - railties (~> 4.1) + turbolinks (5.0.1) + turbolinks-source (~> 5) + turbolinks-source (5.0.0) + twitter-bootstrap-rails (3.2.2) + actionpack (>= 3.1) + execjs (>= 2.2.2, >= 2.2) + less-rails (>= 2.5.0) + railties (>= 3.1) tzinfo (1.2.2) thread_safe (~> 0.1) - uglifier (2.5.3) - execjs (>= 0.3.0) - json (>= 1.8.0) + uber (0.0.15) + uglifier (3.0.2) + execjs (>= 0.3.0, < 3) warden (1.2.6) rack (>= 1.0) @@ -221,6 +261,8 @@ DEPENDENCIES devise_invitable factory_girl_rails fullcalendar-rails + google-api-client + googleauth icalendar jbuilder (~> 2.0) jquery-rails @@ -242,3 +284,6 @@ DEPENDENCIES turbolinks twitter-bootstrap-rails uglifier (>= 1.3.0) + +BUNDLED WITH + 1.12.5 diff --git a/app/assets/javascripts/events.js.coffee b/app/assets/javascripts/events.js.coffee index 7209c29..3f3f180 100644 --- a/app/assets/javascripts/events.js.coffee +++ b/app/assets/javascripts/events.js.coffee @@ -12,7 +12,7 @@ fullCalendar = -> month: 'HH:mm' week: 'HH:mm' day: 'HH:mm' - events: '/events.json' + events: getCalendar() eventClick: (calEvent) -> @@ -103,6 +103,23 @@ doSubmit = -> error: -> alert "error" +getCalendar = -> + res = $.ajax + type: 'GET' + url: '/events/fetch' + timeout: 9000 + async: false + success: () -> + error: -> + alert "error" + events = [] + res.responseJSON.forEach (event) -> + events.push + title: event.summary + start: event.start.date + console.log(events) + return events + initDraggableOldEvent = -> $('.fc-event').each -> event = { diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index bdbabd2..2df9128 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -1,4 +1,10 @@ require 'date' +require 'googleauth' +require 'googleauth/stores/file_token_store' +require 'google/api_client/client_secrets' +require 'google/apis/calendar_v3' +require 'json' +require 'yaml' class EventsController < ApplicationController before_action :set_event, only: [:show, :edit, :update, :destroy] @@ -116,7 +122,74 @@ def ajax_create_event_from_old_event end end + def fetch + #config = YML.load_file('~/.config/camome/config.yml') + @user_id = "pa7n2xfm@s.okayama-u.ac.jp" + @calendar_ids = ["pa7n2xfm@s.okayama-u.ac.jp"] + @client_id = "180186309378-bk5o6f9dk79cvcj2o4mbpaql6nskpnsu.apps.googleusercontent.com" + @client_secret = "WoEMM9q6kgryHIu4bhmA5kuT" + @oob_url = 'urn:ietf:wg:oauth:2.0:oob' + + collection = [] + timeMax = Time.local(2016, 11, 30, 23, 59, 59).iso8601 + timeMin = Time.local(2016, 9, 1, 0).iso8601 + @calendar_ids.each do |calendar_id| + border = DateTime.parse("2016-10-01T04:21:46+00:00") + response = get_events(border, calendar_id) + #response = get_events(timeMax, timeMin, calendar_id) + last = border + response.items.each do |item| + next if item.status == "cancelled" + created = item.created + collection << item if created > border + last = created if created > last + end + end + render json: collection + end + private + + #def get_events(timeMax, timeMin, calendar_id) + #params = {:order_by => "startTime", :show_deleted => "true", :timeMax => timeMax, :timeMin => timeMin} + def get_events(time, calendar_id) + params = {:order_by => "updated", :show_deleted => "false", :updated_min => time.strftime("%Y-%m-%dT%H:%M:%SZ")} + return google_calendar_api(params, calendar_id) + end + + def authorize + dir_path = "~/.config/camome" + client_id = Google::Auth::ClientId.new(@client_id, @client_secret) + token_store = Google::Auth::Stores::FileTokenStore.new( + file: File.expand_path("#{dir_path}/google_access_tokens.yml", __FILE__)) + scope = 'https://www.googleapis.com/auth/calendar' + authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store) + + credentials = authorizer.get_credentials(@user_id) + if credentials.nil? + url = authorizer.get_authorization_url( + base_url: @oob_url) + puts "Open the following URL in the browser and enter the " + + "resulting code after authorization" + puts url + code = STDIN.gets.chomp + credentials = authorizer.get_and_store_credentials_from_code( + user_id: @user_id, code: code, base_url: @oob_url) + end + return credentials + end + + def google_calendar_api(params, calendar_id) + service = Google::Apis::CalendarV3::CalendarService.new + service.client_options.application_name = @application_name + service.authorization = authorize() + service.authorization.refresh! + #response = service.list_events(calendar_id, single_events: true, order_by: 'startTime', time_max: Time.utc(2016, 11, 30, 23, 59, 59).iso8601, time_min: Time.utc(2016, 11, 30, 23, 59, 59).iso8601) + response = service.list_events(calendar_id, order_by: params[:order_by], show_deleted: params[:show_deleted], updated_min: params[:updated_min]) + + return response + end + # Use callbacks to share common setup or constraints between actions. def set_event @event = Event.find(params[:id]) diff --git a/app/views/events/_calendar_list.html.erb b/app/views/events/_calendar_list.html.erb index 7ac19dc..4eb9b33 100644 --- a/app/views/events/_calendar_list.html.erb +++ b/app/views/events/_calendar_list.html.erb @@ -1,5 +1,5 @@
-<% User.current.calendars.each do |c| %> +<% current_user.calendars.each do |c| %>
>■<%= c.displayname %>
diff --git a/config/routes.rb b/config/routes.rb index 08b7edf..e697221 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,8 +32,7 @@ post "calendars/create_caldav" resources :calendars resources :recurrences - resources :events - + get "gate/index" get "gate/login" get "gate/logout" @@ -41,13 +40,16 @@ match "events/create_recurrence", :via => :post post "events/new" - + get "events/fetch" + resources :events + get "inbox/missions" get "inbox/recurrences" post "recurrences/add_events" post "events/ajax_create_event_from_old_event" + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 07e7a2c59a8900110487cf4183c43d7e5e9bf022 Mon Sep 17 00:00:00 2001 From: Tomoki Tsubokawa Date: Thu, 20 Oct 2016 16:46:02 +0900 Subject: [PATCH 2/2] Get events of display month from Google calendar --- app/assets/javascripts/events.js.coffee | 19 +-------------- app/controllers/events_controller.rb | 32 +++++++++---------------- 2 files changed, 12 insertions(+), 39 deletions(-) diff --git a/app/assets/javascripts/events.js.coffee b/app/assets/javascripts/events.js.coffee index 3f3f180..15b8d03 100644 --- a/app/assets/javascripts/events.js.coffee +++ b/app/assets/javascripts/events.js.coffee @@ -12,7 +12,7 @@ fullCalendar = -> month: 'HH:mm' week: 'HH:mm' day: 'HH:mm' - events: getCalendar() + events: '/events/fetch' eventClick: (calEvent) -> @@ -103,23 +103,6 @@ doSubmit = -> error: -> alert "error" -getCalendar = -> - res = $.ajax - type: 'GET' - url: '/events/fetch' - timeout: 9000 - async: false - success: () -> - error: -> - alert "error" - events = [] - res.responseJSON.forEach (event) -> - events.push - title: event.summary - start: event.start.date - console.log(events) - return events - initDraggableOldEvent = -> $('.fc-event').each -> event = { diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 2df9128..6b6e8a6 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -124,25 +124,19 @@ def ajax_create_event_from_old_event def fetch #config = YML.load_file('~/.config/camome/config.yml') - @user_id = "pa7n2xfm@s.okayama-u.ac.jp" - @calendar_ids = ["pa7n2xfm@s.okayama-u.ac.jp"] - @client_id = "180186309378-bk5o6f9dk79cvcj2o4mbpaql6nskpnsu.apps.googleusercontent.com" - @client_secret = "WoEMM9q6kgryHIu4bhmA5kuT" + @user_id = "*************" + @calendar_ids = [@user_id] + @client_id = ApplicationSettings.oauth.google.application_id + @client_secret = ApplicationSettings.oauth.google.application_secret @oob_url = 'urn:ietf:wg:oauth:2.0:oob' collection = [] - timeMax = Time.local(2016, 11, 30, 23, 59, 59).iso8601 - timeMin = Time.local(2016, 9, 1, 0).iso8601 + timeMax = Time.parse(params["end"]).iso8601 + timeMin = Time.parse(params["start"]).iso8601 @calendar_ids.each do |calendar_id| - border = DateTime.parse("2016-10-01T04:21:46+00:00") - response = get_events(border, calendar_id) - #response = get_events(timeMax, timeMin, calendar_id) - last = border + response = get_events(timeMax, timeMin, calendar_id) response.items.each do |item| - next if item.status == "cancelled" - created = item.created - collection << item if created > border - last = created if created > last + collection << {title: item.summary, start: item.start.date, end: item.end.date} end end render json: collection @@ -150,10 +144,8 @@ def fetch private - #def get_events(timeMax, timeMin, calendar_id) - #params = {:order_by => "startTime", :show_deleted => "true", :timeMax => timeMax, :timeMin => timeMin} - def get_events(time, calendar_id) - params = {:order_by => "updated", :show_deleted => "false", :updated_min => time.strftime("%Y-%m-%dT%H:%M:%SZ")} + def get_events(timeMax, timeMin, calendar_id) + params = {:order_by => "startTime", :single_events => "true", :show_deleted => "false", :time_max => timeMax, :time_min => timeMin} return google_calendar_api(params, calendar_id) end @@ -184,9 +176,7 @@ def google_calendar_api(params, calendar_id) service.client_options.application_name = @application_name service.authorization = authorize() service.authorization.refresh! - #response = service.list_events(calendar_id, single_events: true, order_by: 'startTime', time_max: Time.utc(2016, 11, 30, 23, 59, 59).iso8601, time_min: Time.utc(2016, 11, 30, 23, 59, 59).iso8601) - response = service.list_events(calendar_id, order_by: params[:order_by], show_deleted: params[:show_deleted], updated_min: params[:updated_min]) - + response = service.list_events(calendar_id, order_by: params[:order_by], show_deleted: params[:show_deleted], single_events: params[:single_events], time_max: params[:time_max], time_min: params[:time_min]) return response end