Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

nodejs support #18

Merged
merged 2 commits into from Apr 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
node_modules
.DS_Store
*.swp
15 changes: 15 additions & 0 deletions examples/nodejs/test.js
@@ -0,0 +1,15 @@
var Tabletop = require('../../').Tabletop;

var testURL = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AmYzu_s7QHsmdDNZUzRlYldnWTZCLXdrMXlYQzVxSFE&output=html';

function onLoad(data, tabletop) {
console.log(data);
};

var options = {
key: testURL,
callback: onLoad,
simpleSheet: true
};

Tabletop.init(options);
26 changes: 26 additions & 0 deletions package.json
@@ -0,0 +1,26 @@
{
"name": "tabletop",
"version": "0.0.0",
"description": "**Tabletop.js** takes a Google Spreadsheet and makes it easily accessible through JavaScript. With zero dependencies!",
"main": "src/tabletop.js",
"directories": {
"example": "examples"
},
"scripts": {
"test": "node examples/nodejs/test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/jsoma/tabletop.git"
},
"author": "",
"license": "BSD",
"readmeFilename": "README.md",
"gitHead": "fdb1c3ee3bc0f7f3be6ab223a56f9f050fe32496",
"dependencies": {
"request": "~2.16.6"
},
"engines": {
"node": ">=0.10.0"
}
}
68 changes: 56 additions & 12 deletions src/tabletop.js
@@ -1,6 +1,12 @@
(function(global) {
"use strict";

var inNodeJS = false;
if (typeof process !== 'undefined') {
inNodeJS = true;
var request = require('request');
}

if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (obj, fromIndex) {
if (fromIndex === null) {
Expand Down Expand Up @@ -30,7 +36,7 @@
if(!this || !(this instanceof Tabletop)) {
return new Tabletop(options);
}

if(typeof(options) === 'string') {
options = { key : options };
}
Expand Down Expand Up @@ -79,7 +85,13 @@
this.models = {};
this.model_names = [];

this.base_json_path = "/feeds/worksheets/" + this.key + "/public/basic?alt=json-in-script";
this.base_json_path = "/feeds/worksheets/" + this.key + "/public/basic?alt=";

if (inNodeJS) {
this.base_json_path += 'json';
} else {
this.base_json_path += 'json-in-script';
}

if(!this.wait) {
this.fetch();
Expand All @@ -104,7 +116,20 @@
if(typeof(callback) !== "undefined") {
this.callback = callback;
}
this.injectScript(this.base_json_path, this.loadSheets);
this.requestData(this.base_json_path, this.loadSheets);
},

/*
This will call the environment appropriate request method.

In browser it will use JSON-P, in node it will use request()
*/
requestData: function(path, callback) {
if (inNodeJS) {
this.serverSideFetch(path, callback);
} else {
this.injectScript(path, callback);
}
},

/*
Expand Down Expand Up @@ -158,6 +183,19 @@

document.getElementsByTagName('script')[0].parentNode.appendChild(script);
},

/*
This will only run if tabletop is being run in node.js
*/
serverSideFetch: function(path, callback) {
var self = this
request({url: this.endpoint + path, json: true}, function(err, resp, body) {
if (err) {
return console.error(err);
}
callback.call(self, body);
});
},

/*
Is this a sheet you want to pull?
Expand Down Expand Up @@ -212,22 +250,27 @@
*/
loadSheets: function(data) {
var i, ilen;
var toInject = [];
var toLoad = [];
this.foundSheetNames = [];

for(i = 0, ilen = data.feed.entry.length; i < ilen ; i++) {
this.foundSheetNames.push(data.feed.entry[i].title.$t);
// Only pull in desired sheets to reduce loading
if( this.isWanted(data.feed.entry[i].content.$t) ) {
var sheet_id = data.feed.entry[i].link[3].href.substr( data.feed.entry[i].link[3].href.length - 3, 3);
var json_path = "/feeds/list/" + this.key + "/" + sheet_id + "/public/values?alt=json-in-script&sq=" + this.query;
toInject.push(json_path);
var json_path = "/feeds/list/" + this.key + "/" + sheet_id + "/public/values?sq=" + this.query + '&alt='
if (inNodeJS) {
json_path += 'json';
} else {
json_path += 'json-in-script';
}
toLoad.push(json_path);
}
}

this.sheetsToLoad = toInject.length;
for(i = 0, ilen = toInject.length; i < ilen; i++) {
this.injectScript(toInject[i], this.loadSheet);
this.sheetsToLoad = toLoad.length;
for(i = 0, ilen = toLoad.length; i < ilen; i++) {
this.requestData(toLoad[i], this.loadSheet);
}
},

Expand Down Expand Up @@ -274,8 +317,9 @@
Tests this.sheetsToLoad just in case a race condition happens to show up
*/
doCallback: function() {
if(this.sheetsToLoad === 0)
this.callback.apply(this.callbackContext || this, [this.data(), this]);
if(this.sheetsToLoad === 0) {
this.callback.apply(this.callbackContext || this, [this.data(), this]);
}
},

log: function(msg) {
Expand Down Expand Up @@ -360,4 +404,4 @@
}
};

})(this);
})(typeof process === 'undefined' ? this : module.exports);