/**
 * advanced-email-validation: 03/2015
 * eslint.js created on 02/03/15
 * http://www.github.com/roughcoder/advanced-email-validation
 *
 * Copyright (c) 2015 "Roughcoder" Neil Barton
 * Dual licensed under the MIT and GPL licenses.
 * http://www.github.com/roughcoder/advanced-email-validation/license
 */

var https = require('https'),
    sources = require('./data/sources.json'),
    fs = require('fs');

function fetch (type, whitelist) {

    var fileName = type.file,
        output = [];

    function error (e) {
        throw new Error('An error when fetching a source: ' + e);
    }

    function update (res) {

        function data (chunk) {
            var content = chunk.toString().split('\n');
            output = output.concat(content);
        }

        function end () {
            output = output.filter(function (n) {
                return n !== '';
            });
            output = output.filter(function (n) {
                return n.indexOf('.') > 0;
            });
            if (whitelist){
                output = output.filter(function (n) {
                    return whitelist.indexOf(n) === -1;
                });
            }
            output.sort();
            fs.writeFileSync('./data/' + fileName, JSON.stringify(output, null, 4), 'utf8');
        }

        res.on('data', data);
        res.on('end', end);

    }

    type.sources.forEach(function (source) {
        https.get(source, update).on('error', error);
    });

}

fetch(sources.disposable);
fetch(sources.free);
