Skip to content

Instantly share code, notes, and snippets.

@gberger
Created October 11, 2014 01:02
Show Gist options
  • Save gberger/d5710d88a2f2122a6077 to your computer and use it in GitHub Desktop.
Save gberger/d5710d88a2f2122a6077 to your computer and use it in GitHub Desktop.
Average number of students per section -- Clever API
var _ = require('underscore');
var request = require('request');
var base = 'https://api.clever.com/'
var headers = { 'Authorization': 'Bearer DEMO_TOKEN' }
var students = 0;
var sections = 0;
var requestNext = function(json, fn) {
var link = _.find(json.links, function(link) {
return link.rel === "next";
});
if(link) {
request({url: base + link.uri, headers: headers}, fn);
return true;
} else {
return false;
}
}
var addStudentsAndSections = function(json) {
students += _.chain(json.data)
.map(function(d){ return d.data.students.length; } )
.reduce(function(a, b){ return a+b; })
.value();
sections += json.data.length;
}
var opts = {
url: base + 'v1.1/sections',
headers: headers
};
var callback = function (error, response, body) {
if (!error && response.statusCode === 200) {
var json = JSON.parse(body);
addStudentsAndSections(json);
if(!requestNext(json, callback)){
console.log(students/sections)
};
} else {
console.log('err');
}
}
request(opts, callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment