Created
October 19, 2011 00:21
-
-
Save evilmarty/1297150 to your computer and use it in GitHub Desktop.
EDH javascript [old] api
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
/* | |
** Everyday Hero Javascript API (incomplete) | |
** http://api.everydayhero.com.au | |
** | |
** dependencies: jQuery | |
*/ | |
var EDH = { | |
baseURI: 'http://api.everydayhero.com.au/widget/', | |
ordinalize: function(number) { | |
number = parseInt(number); | |
if (11 <= number % 100 && number % 100 <= 13) { | |
return number + "th"; | |
} else { | |
switch (number % 10) { | |
case 1: return number + "st"; | |
case 2: return number + "nd"; | |
case 3: return number + "rd"; | |
default: return number + "th"; | |
} | |
} | |
}, | |
numberFormat: function(number, decimals, dec_point, thousands_sep) { | |
// discuss at: http://phpjs.org/functions/number_format | |
var n = number, prec = decimals; | |
var toFixedFix = function (n,prec) { | |
var k = Math.pow(10,prec); | |
return (Math.round(n*k)/k).toString(); | |
}; | |
n = !isFinite(+n) ? 0 : +n; | |
prec = !isFinite(+prec) ? 0 : Math.abs(prec); | |
var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep; | |
var dec = (typeof dec_point === 'undefined') ? '.' : dec_point; | |
var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0; | |
var abs = toFixedFix(Math.abs(n), prec); | |
var _, i; | |
if (abs >= 1000) { | |
_ = abs.split(/\D/); | |
i = _[0].length % 3 || 3; | |
_[0] = s.slice(0,i + (n < 0)) + | |
_[0].slice(i).replace(/(\d{3})/g, sep+'$1'); | |
s = _.join(dec); | |
} else { | |
s = s.replace('.', dec); | |
} | |
var decPos = s.indexOf(dec); | |
if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) { | |
s += new Array(prec-(s.length-decPos-1)).join(0)+'0'; | |
} | |
else if (prec >= 1 && decPos === -1) { | |
s += dec+new Array(prec).join(0)+'0'; | |
} | |
return s; | |
}, | |
Event: function(eventURL, target) { | |
this.eventURL = eventURL; | |
this.target = parseInt(target) || 1000; | |
return this; | |
} | |
}; | |
$.extend(EDH.Event.prototype, { | |
total: function(callback) { | |
var jsonpCallback = this.eventURL + 'TotalCallback'; | |
$.ajax({ | |
url: EDH.baseURI + 'event/' + this.eventURL + '/total/' + jsonpCallback + '.json', | |
jsonpCallback: jsonpCallback, | |
dataType: 'jsonp', | |
success: function(data) { | |
callback(data); | |
} | |
}); | |
}, | |
totalAsValue: function(callback) { | |
this.total(function(data) { | |
data = parseFloat(data.replace(/[$,]/g, '')); | |
callback(data); | |
}); | |
}, | |
topIndividuals: function(callback, limit) { | |
var jsonpCallback = this.eventURL + 'TopIndividualsCallback'; | |
$.ajax({ | |
url: EDH.baseURI + 'event/' + this.eventURL + '/top_ten/individual/' + jsonpCallback + '.json', | |
jsonpCallback: jsonpCallback, | |
dataType: 'jsonp', | |
success: function(data) { | |
var collection = limit ? data.slice(0, limit) : data; | |
collection = $.map(collection, function(object, index) { | |
object.position = EDH.ordinalize(index + 1); | |
return object; | |
}); | |
callback(collection); | |
} | |
}); | |
}, | |
topTeams: function(callback, limit) { | |
var jsonpCallback = this.eventURL + 'TopTeamsCallback'; | |
$.ajax({ | |
url: EDH.baseURI + 'event/' + this.eventURL + '/top_ten/team/' + jsonpCallback + '.json', | |
jsonpCallback: jsonpCallback, | |
dataType: 'jsonp', | |
success: function(data) { | |
var collection = limit ? data.slice(0, limit) : data; | |
collection = $.map(collection, function(object, index) { | |
object.position = EDH.ordinalize(index + 1); | |
return object; | |
}); | |
callback(collection); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment