Skip to content

Instantly share code, notes, and snippets.

@beOn
Last active December 10, 2015 01:28
Show Gist options
  • Save beOn/4359016 to your computer and use it in GitHub Desktop.
Save beOn/4359016 to your computer and use it in GitHub Desktop.
// Copyright 2012 Ben Acland
// See License.txt for details.
//
// Mode d'emploi...
//
// for testing:
// var g = new GDFeed(testData, successCallback, errorCallback)
// g.fetchData()
//
// for reals:
// var g = new GDFeed(googleDocKey, successCallback, errorCallback)
// g.fetchData()
//
// ... callback arguments are optional. On success, successCallback will be passed
// an array of objects corresponding to the rows in your google spreadsheet.
var GDFeed = (function (window, document, $) {
// class level variables... if you want them.
// var foo = 'bar';
function fetchData(isRetry) {
if (typeof(isRetry) == 'undefined') { isRetry == false};
if ((this.isFetching == true && isRetry == false)
|| (this.docKey == null && this.mockData == null)) {
return
};
this.isFetching = true;
// if you're using dummy data, skip the fetch
if (this.mockData != null) {
this.handleResponse(this.mockData);
this.isFetching = false;
return;
}
var addr = "https://spreadsheets.google.com/feeds/list/" + this.docKey + "/od6/public/values?alt=json";
var feedObj = this;
var json = $.getJSON(addr, function(data) {
feedObj.handleResponse(data);
})
.error(function() {
if (this.errorCallback != null) {
this.errorCallback("error fetching json");
};
})
.complete(function() {
this.isFetching = false;
});
}
function handleResponse(theResponse) {
if (typeof(theResponse) == 'undefined') {return};
this.response = theResponse;
this.parseResponse();
if (this.entries == null) {
// there was some kind of error.
if (this.errorCallback != null) {
this.errorCallback("error parsing json");
}
}
else if (this.successCallback != null) {
this.successCallback(this.entries);
};
}
function parseResponse() {
this.entries = null;
if (this.response == null
|| !this.response.hasOwnProperty('feed')
|| !this.response['feed'].hasOwnProperty('entry'))
{
// there's no valid data to parse.
return;
}
// turn the gsx$... values into values in a dict
var es = [];
var raw = this.response['feed']['entry'];
for (var i in raw) {
var ent = raw[i];
var e = {};
var foundOne = false;
for (var key in ent) {
if (key.length > 4
&& key.slice(0,4) == 'gsx$') {
foundOne = true;
e[key.slice(4)] = ent[key]['$t'];
};
}
if (foundOne == true) {
// we only add the entry if we found at least one column key
es.push(e);
}
}
this.entries = es;
}
// constructor.
// dKey can be your spreadsheet's key, or a mock data object.
var module = function (dKey, onSuccess, onError) {
// instance variables
this.response = null;
this.entries = null;
this.isFetching = false;
this.docKey = null;
this.mockData = null;
this.successCallback = null;
this.errorCallback = null;
if (typeof(dKey) == 'string') {
this.docKey = dKey;
}
else if (typeof(dKey) == 'object') {
this.mockData = dKey;
}
if (typeof(onSuccess) == 'function') {
this.successCallback = onSuccess;
};
if (typeof(onError) == 'function') {
this.errorCallback = onError;
};
};
module.prototype = {
constructor: module,
fetchData: fetchData,
handleResponse: handleResponse,
parseResponse: parseResponse,
};
return module;
})(window, document, jQuery);
(c) Copyright 2012 Ben Acland.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
Neither the name of the GDFeed project nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment