// kill logs
var Log = function () {}

GoogleApi = {
  // host component, shouldn't change
  _host: 'https://www.googleapis.com',

  _callAndRefresh: function(method, path, options, callback) {
    var self = this;
    options = options || {};
        
    self._call(method, path, options,       
      // need to bind the env here so we can do mongo writes in the callback 
      // (when refreshing), if we call this on the server
      Meteor.bindEnvironment(function(error, result) {
        if (error && error.response && error.response.statusCode == 401) {
          Log('google-api attempting token refresh');

          return self._refresh(options.user, function(error) {
            if (error)
              return callback(error);
            
            // if we have the user, we'll need to re-fetch them, as their
            // access token will have changed.
            if (options.user)
              options.user = Meteor.users.findOne(options.user._id);
            
            self._call(method, path, options, callback);
          });
        } else {
          callback(error, result);
        }
    }, 'Google Api callAndRefresh'));
  },
  
  // call a GAPI Meteor.http function if the accessToken is good
  _call: function(method, path, options, callback) {
    Log('GoogleApi._call, path:' + path);
    
    options = options || {};
    var user = options.user || Meteor.user();
    
    if (user && user.services && user.services.google && 
        user.services.google.accessToken) {
      options.headers = options.headers || {};
      options.headers.Authorization = 'Bearer ' + user.services.google.accessToken;
    
      HTTP.call(method, this._host + '/' + path, options, function(error, result) {
        callback(error, result && result.data);
      });
    } else {
      callback(new Meteor.Error(403, "Auth token not found." +
        "Connect your google account"));
    }
  },

  _refresh: function(user, callback) {
    Log('GoogleApi._refresh');

    Meteor.call('exchangeRefreshToken', user && user._id, function(error, result) {
      callback(error, result && result.access_token)
    });
  }
}

// setup HTTP verbs
httpVerbs = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
_.each(httpVerbs, function(verb) {
  GoogleApi[verb.toLowerCase()] = wrapAsync(function(path, options, callback) {
    if (_.isFunction(options)) {
      callback = options;
      options = {};
    }

    return this._callAndRefresh(verb, path, options, callback);
  })
});

@@ -237,24 +237,16 @@
  %7C%7C %7B%7D;%0A
-        
 %0A    sel
@@ -275,23 +275,16 @@
 options,
-       
 %0A      /
@@ -352,17 +352,16 @@
 callback
- 
 %0A      /
@@ -720,29 +720,17 @@
 error);%0A
-            
 %0A
+
         
@@ -945,21 +945,9 @@
 d);%0A
-            
 %0A
+
     
@@ -1118,19 +1118,17 @@
 );%0A  %7D,%0A
-  
 %0A
+
   // cal
@@ -1279,21 +1279,17 @@
  path);%0A
-    
 %0A
+
     opti
@@ -1355,20 +1355,16 @@
 user();%0A
-    
 %0A    if 
@@ -1412,17 +1412,16 @@
 oogle &&
- 
 %0A       
@@ -1588,20 +1588,61 @@
 sToken;%0A
+%0A
     
+  var host = options.host %7C%7C this._host;
 %0A      H
@@ -1658,22 +1658,16 @@
 method, 
-this._
 host + '

set _host as an option