Commit 041a998

mo khan <mo@mokhan.ca>
2015-06-14 16:14:16
search for stronglifts backup file from google drive api.
1 parent f083c3b
Changed files (4)
app
assets
controllers
views
training_sessions
app/assets/javascripts/models/google.js.coffee
@@ -1,10 +1,51 @@
 class Stronglifters.GoogleDrive
   constructor: () ->
-    console.log("GOOGLE DRIVE")
+    @client_id = "241601222378-kscpfqhpmc6059704mfcq8ckcp799dvn.apps.googleusercontent.com"
+    @scopes = [
+      'https://www.googleapis.com/auth/drive',
+      'https://www.googleapis.com/auth/drive.install',
+      'https://www.googleapis.com/auth/drive.file',
+      'https://www.googleapis.com/auth/drive.readonly',
+      'https://www.googleapis.com/auth/drive.apps.readonly',
+    ]
 
-  initialize: () ->
-    @drive = new gapi.drive.share.ShareClient('241601222378')
-    @drive.setItemIds(["<FILE_ID>"])
+  initializeDrive: () =>
+    @loadClient =>
+      @printFile('StrongLifts/backup.stronglifts')
 
-  showSettingDialog: () ->
+  showSettingsDialog: () ->
+    @drive = new gapi.drive.share.ShareClient('241601222378')
+    #@drive.setItemIds(['StrongLifts/backup.stronglifts'])
     @drive.showSettingsDialog()
+
+  checkAuth: () ->
+    gapi.auth.authorize({'client_id': @client_id, 'scope': @scopes, 'immediate': false}, @handleAuthResult)
+
+  handleAuthResult: (authResult) =>
+    if (authResult)
+      #console.dir(authResult)
+      gapi.load('drive-share', @initializeDrive)
+    else
+      @checkAuth()
+
+  loadClient: (callback) ->
+    gapi.client.load('drive', 'v2', callback)
+
+  printFile: (fileId) =>
+    #request = gapi.client.drive.files.get({ 'fileId': fileId })
+    request = gapi.client.drive.files.list({
+      'q': "title contains '.stronglifts' and title contains 'backup'"
+    })
+    request.execute (response) =>
+      console.dir(response)
+      debugger
+      if !response.error
+        item = response.items[0]
+        console.log('Title: ' + item.title)
+        console.log('Description: ' + item.description)
+        console.log('MIME type: ' + item.mimeType)
+      else if (item.error.code == 401)
+        #// Access token might have expired.
+        @checkAuth()
+      else
+        console.log('An error occured: ' + response.error.message)
app/assets/javascripts/application.js
@@ -18,8 +18,3 @@
 //= require_tree .
 
 var Stronglifters = Stronglifters || {};
-
-//init = function() {
-  //s = new gapi.drive.share.ShareClient('<YOUR_APP_ID>');
-  //s.setItemIds(["<FILE_ID>"]);
-//};
app/controllers/training_sessions_controller.rb
@@ -1,6 +1,8 @@
 require "temporary_storage"
 
 class TrainingSessionsController < ApplicationController
+  after_action :allow_google_iframe
+
   def index
     @training_sessions = current_user.training_sessions.
       order(occurred_at: :desc)
@@ -20,4 +22,8 @@ class TrainingSessionsController < ApplicationController
   def storage
     @storage ||= TemporaryStorage.new
   end
+
+  def allow_google_iframe
+    response.headers["X-Frame-Options"] = "ALLOW-FROM https://drive.google.com"
+  end
 end
app/views/training_sessions/index.html.erb
@@ -13,6 +13,9 @@
           </div> <!-- /.small-12 -->
         <% end %>
       </div>
+      <div class="row">
+        <button onclick="googleDriver.showSettingsDialog()">Share</button>
+      </div>
     </div>
     <div class="panel callout radius">
       <h6><%= @training_sessions.count %> training sessions completed</h6>
@@ -27,10 +30,14 @@
 </div>
 
 <script type="text/javascript" charset="utf-8">
+  var googleDriver = new Stronglifters.GoogleDrive();
   $(function(){
     window.onload = function() {
-      var driver = new Stronglifters.GoogleDrive();
-      gapi.load('drive-share', driver.initialize);
     };
   });
+  function handleClientLoad() {
+    googleDriver.checkAuth()
+  }
 </script>
+<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
+