Skip to content

Instantly share code, notes, and snippets.

@geuis
Created July 11, 2013 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geuis/5979435 to your computer and use it in GitHub Desktop.
Save geuis/5979435 to your computer and use it in GitHub Desktop.
Basic http://getclever.com API accessor. Calculate average number of students per section in dummy data.
<!doctype>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
(function(){
window.clever = function(user, pass){
if( typeof user === 'string' && typeof pass === 'string' ){
var api = new Api();
api.username = user;
api.password = pass;
return api;
}else{
throw new Error('Username or password missing or not strings.');
}
}
var Api = function(){
return {
api_url: 'https://api.getclever.com/v1.1',
username: '',
password: '',
get: function(url, callback){
var self = this;
$.ajax({
type: 'get',
url: this.api_url + url,
dataType: 'json',
async: false,
crossDomain: true,
username: this.username,
password: this.password,
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(this.username+':'+this.password));
},
success: function(){
callback.apply(self, arguments);
}
});
}
}
}
})();
$(function(){
//Its not clear from the API data if a "section" is an individual class identified by the "name" property, or if a section is similar classes sharing the same "course_name" property.
//For this example, we assume each "name" is a "section".
//Its also not specified if we want to filter students enrolled in multiple sections. For this example, we do not.
var client = clever('DEMO_KEY','');
client.get('/sections', function(sections, status, xhr){
var students_total = 0;
sections.data.forEach(function(item, index){
students_total += item.data.students.length;
});
//Average of students in all sections
console.log( 'Average number of students in', sections.data.length, 'sections:', students_total / sections.data.length );
});
});
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment