Created
October 13, 2012 12:34
-
-
Save pirhoo/3884494 to your computer and use it in GitHub Desktop.
Module to get a Google Fusion Table using its key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Google API client | |
* @type {Object} | |
*/ | |
var googleapis = require('googleapis'); | |
/** | |
* API Client | |
* @type {Boolean} | |
*/ | |
var client = false; | |
/** | |
* @author Pirhoo | |
* | |
*/ | |
module.exports = function(app) { | |
// Create the fusiontable client | |
googleapis.load('fusiontables','v1', function(err, c) { | |
// No error | |
if(err == null) { | |
// Record the client | |
client = c; | |
// Sets API key | |
client.setApiKey('<YOUR API KEY>'); | |
} | |
}); | |
}; | |
/** | |
* Get a table | |
* @param {String} key Fusion table key | |
* @param {Function} callback Callback function | |
*/ | |
var getTable = module.exports.getTable = function(key, callback) { | |
// Avoid callback fail | |
callback = callback || function() {}; | |
// Checks the client | |
if(!client) return callback({error: "No client available"}, null); | |
// Creates a batch request | |
client | |
.newBatchRequest() | |
.add('fusiontables.query.sql', { sql: 'SELECT * FROM ' + escape(key)}) | |
.execute(null,function(err, res, headers) { | |
// Spread the error | |
if(res == null) return callback(err, null); | |
// For each rows in the given dataset | |
for(var index in res[0].rows) { | |
// Transforms the array of data into an object according the columns' names | |
res[0].rows[index] = objectify(res[0].rows[index], res[0].columns); | |
} | |
// Sends the dataset objectified | |
return callback(null, res[0].rows); | |
}); | |
}; | |
/** | |
* Transform an array to an object following the given columns names | |
* @param {Array} row Array to transform | |
* @param {Array} columns Fields names | |
* @return {Object} The array after transformation | |
*/ | |
function objectify(row, fields) { | |
var obj = {}; | |
// Fetchs the row | |
for(var index in row) { | |
// Slugify the name | |
var slug = slugify(fields[index]) | |
// Record the value within the right field name | |
obj[slug] = row[index]; | |
} | |
return obj; | |
} | |
/** | |
* Slugify the given string | |
* @src http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/ | |
* @param {String} str String to slugify | |
* @return {String} String slugified | |
*/ | |
function slugify(str) { | |
str = str.replace(/^\s+|\s+$/g, ''); // trim | |
str = str.toLowerCase(); | |
// remove accents, swap ñ for n, etc | |
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;"; | |
var to = "aaaaeeeeiiiioooouuuunc------"; | |
for (var i=0, l=from.length ; i<l ; i++) { | |
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); | |
} | |
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars | |
.replace(/\s+/g, '-') // collapse whitespace and replace by - | |
.replace(/-+/g, '-'); // collapse dashes | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment