{"size": 815, "ext": "js", "max_stars_repo_path": "lib/util/remote.js", "max_stars_repo_name": "kristianmandrup/libraries", "max_stars_repo_stars_event_min_datetime": "2015-04-15T20:28:20.000Z", "max_stars_repo_stars_event_max_datetime": "2015-04-22T22:39:57.000Z", "max_issues_repo_path": "lib/util/remote.js", "max_issues_repo_name": "kristianmandrup/libraries", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/util/remote.js", "max_forks_repo_name": "kristianmandrup/libraries", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.9705882353, "max_line_length": 78, "alphanum_fraction": 0.6085889571}
var http = require('http');
var request = require('request');

var download = function(url, dest, cb) {
  var file = fs.createWriteStream(dest);
  http.get(url, function(response) {
    response.pipe(file);
    file.on('finish', function() {
      file.close(cb);  // close() is async, call cb after close completes.
    });
  }).on('error', function(err) { // Handle errors
    fs.unlink(dest); // Delete the file async. (But we don't check the result)
    if (cb) cb(err.message);
  });
};

var retrieve = function (uri, cb) {
  // console.log('fetching', uri);
  return request.get(uri, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      cb(null, body);
    } else {
      cb(error, null);
    }
  });
};


module.exports = {
  download: download,
  retrieve: retrieve,
};

