RegExp bug in GDocs backend fixed and code refactoring

This commit is contained in:
Krzysztof Trzewiczek
2012-08-05 17:53:45 +02:00
parent 129f7a7cdb
commit cf3fe1a656
2 changed files with 43 additions and 41 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
.DS_Store .DS_Store
sandbox/* sandbox/*
.*.swp .*.swp
.*.swo
_site/* _site/*

View File

@@ -31,8 +31,9 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
my.fetch = function(dataset) { my.fetch = function(dataset) {
var dfd = $.Deferred(); var dfd = $.Deferred();
var url = my.getSpreadsheetAPIUrl(dataset.url); var url = my.getSpreadsheetAPIUrl(dataset.url);
$.getJSON(url, function(d) { $.getJSON(url, function(d) {
result = my.parseData(d); var result = my.parseData(d);
var fields = _.map(result.fields, function(fieldId) { var fields = _.map(result.fields, function(fieldId) {
return {id: fieldId}; return {id: fieldId};
}); });
@@ -42,6 +43,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
useMemoryStore: true useMemoryStore: true
}); });
}); });
return dfd.promise(); return dfd.promise();
}; };
@@ -56,67 +58,66 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
// //
// Issues: seems google docs return columns in rows in random order and not even sure whether consistent across rows. // Issues: seems google docs return columns in rows in random order and not even sure whether consistent across rows.
my.parseData = function(gdocsSpreadsheet) { my.parseData = function(gdocsSpreadsheet) {
var options = {}; var options = arguments[1] || {};
if (arguments.length > 1) { var colTypes = options.colTypes || {};
options = arguments[1];
}
var results = { var results = {
fields : [], fields : [],
records: [] records: []
}; };
// default is no special info on type of columns var entries = gdocsSpreadsheet.feed.entry || [];
var colTypes = {}; var key;
if (options.colTypes) { var colName;
colTypes = options.colTypes; // percentage values (e.g. 23.3%)
} var rep = /^([\d\.\-]+)\%$/;
if (gdocsSpreadsheet.feed.entry.length > 0) {
for (var k in gdocsSpreadsheet.feed.entry[0]) { for(key in entries[0]) {
if (k.substr(0, 3) == 'gsx') { // it's barely possible it has inherited keys starting with 'gsx$'
var col = k.substr(4); if(/^gsx/.test(key)) {
results.fields.push(col); colName = key.substr(4);
} results.fields.push(colName);
} }
} }
// converts non numberical values that should be numerical (22.3%[string] -> 0.223[float]) // converts non numberical values that should be numerical (22.3%[string] -> 0.223[float])
var rep = /^([\d\.\-]+)\%$/; results.records = _.map(entries, function(entry) {
results.records = _.map(gdocsSpreadsheet.feed.entry, function(entry) {
var row = {}; var row = {};
_.each(results.fields, function(col) { _.each(results.fields, function(col) {
var _keyname = 'gsx$' + col; var _keyname = 'gsx$' + col;
var value = entry[_keyname]['$t']; var value = entry[_keyname].$t;
var num;
// TODO decide the entry format of percentage data to be parsed
// TODO cover this part of code with test
// TODO use the regexp only once
// if labelled as % and value contains %, convert // if labelled as % and value contains %, convert
if (colTypes[col] == 'percent') { if(colTypes[col] === 'percent' && rep.test(value)) {
if (rep.test(value)) { num = rep.exec(value)[1];
var value2 = rep.exec(value); value = parseFloat(num) / 100;
var value3 = parseFloat(value2);
value = value3 / 100;
}
} }
row[col] = value; row[col] = value;
}); });
return row; return row;
}); });
return results; return results;
}; };
// Convenience function to get GDocs JSON API Url from standard URL // Convenience function to get GDocs JSON API Url from standard URL
my.getSpreadsheetAPIUrl = function(url) { my.getSpreadsheetAPIUrl = function(url) {
if (url.indexOf('feeds/list') != -1) {
return url;
} else {
// https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=0 // https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=0
var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*/; var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*/;
var matches = url.match(regex); var matches = url.match(regex);
if (matches) { var key;
var key = matches[1]; // TODO check possible worksheet options
var worksheet = 1; var worksheet = 1;
var out = 'https://spreadsheets.google.com/feeds/list/' + key + '/' + worksheet + '/public/values?alt=json';
return out; if(!!matches) {
} else { key = matches[1];
alert('Failed to extract gdocs key from ' + url); url = 'https://spreadsheets.google.com/feeds/list/'+ key +'/'+ worksheet +'/public/values?alt=json';
}
} }
return url;
}; };
}(jQuery, this.recline.Backend.GDocs)); }(jQuery, this.recline.Backend.GDocs));