Skip to content

Instantly share code, notes, and snippets.

@howdoicomputer
Created April 26, 2015 05:00
Show Gist options
  • Save howdoicomputer/390c55e3ca290fcdca65 to your computer and use it in GitHub Desktop.
Save howdoicomputer/390c55e3ca290fcdca65 to your computer and use it in GitHub Desktop.
describe('#fetchHydroAreaIDList()', function(){
it('should return an array of IDs from a hydro search', function(done){
var hydroArea = 'CENTRAL+COAST';
station.fetchHydroAreaIDList(hydroArea, function(stationIDs){
console.log(stationIDs);
done();
});
});
});
/**
* Goes through a hydro search area and collects station ids
*
* @param {String} hydroArea
* @return {Array} ids
*/
fetchHydroAreaIDList: function(hydroArea, callback) {
var url =
config.get('CDEC').endpoints.base +
config.get('CDEC').endpoints.hydroUrl +
hydroArea;
fetchStationIDList(url, function(stationIDs){
callback(stationIDs);
});
},
};
/**
* Fetch a list of station IDs
*
* @param {String} url
* @return {Array} ids
*/
function fetchStationIDList(url, callback) {
request(url, function(error, response, html) {
if (!error && response.code == 200) {
var $ = cheerio.load(html);
var table = $('#main_content table td');
var regex = /^[A-Z]{3}$/;
var ids = [];
table.each( function() {
var td = _.trim($(this).text());
if (td.match(regex)) {
ids.push(td);
}
});
callback(ids);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment