Created
July 10, 2013 15:02
-
-
Save bevacqua/5967065 to your computer and use it in GitHub Desktop.
API Usage example of `https://getclever.com/developers/docs/`. You might run this in the `console` of any Chrome tab.
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
(function(){ | |
'use strict'; | |
function ajax(url, done){ | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', url, true); | |
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('DEMO_KEY')); | |
xhr.responseType = 'json'; | |
xhr.onload = function(e){ | |
if(xhr.status === 200){ | |
done(JSON.parse(xhr.response), xhr.status, xhr); | |
}else{ | |
done(null, xhr.status, xhr); | |
} | |
}; | |
xhr.send(); | |
} | |
ajax('https://api.getclever.com/v1.1/sections', function(res, status, xhr){ | |
var sum, avg; | |
if(status !== 200){ | |
console.log('Request failed with status code ' + status); | |
return; | |
} | |
sum = res.data.reduce(function(sumSoFar, section){ | |
return sumSoFar + section.data.students.length; | |
}, 0); | |
avg = sum / (res.data.length || 1); // avoid Infinity | |
result(avg); | |
}); | |
function format(data){ | |
var rfixed = /(\d+\.\d{2})(\d+)/; | |
return data.toString().replace(rfixed, '$1'); | |
} | |
function result(data){ | |
console.log('The average number of students per section is: ' + format(data)); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment