Skip to content

Instantly share code, notes, and snippets.

@robtuley
Last active December 11, 2015 07:48
Show Gist options
  • Select an option

  • Save robtuley/4568642 to your computer and use it in GitHub Desktop.

Select an option

Save robtuley/4568642 to your computer and use it in GitHub Desktop.
node snippet to create a local river data cache from Rainchasers API
var rivers = [];
function parseUrl(url){
var parsed = require('url').parse(url);
return {
host: parsed.host,
path: parsed.pathname+(parsed.search||'')
};
}
function onRiverDataComplete(resumeUrl){
process.stdout.write(' retrieved '+rivers.length+
" rivers\nresume from "+resumeUrl+"\n");
}
function getRiverData(url){
process.stdout.write('.');
require('http').get(parseUrl(url),function(response){
var json = '';
response.on('data',function(data){
json += data;
});
response.on('end',function(){
var result = JSON.parse(json);
rivers = rivers.concat(result.data);
if(result.meta.link.next){
getRiverData(result.meta.link.next);
}else{
onRiverDataComplete(result.meta.link.resume);
}
});
});
};
getRiverData('http://api.rainchasers.com/v1/river');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment