'use strict';

const path = require('path');
const co = require('co');
const Request = require('./request');
const Stash = require('./stash');

const headers = {
  accept: 'application/json',
  platform: 'web',
  device_type: 'webportal',
  client: 'mxd_package',
  language: 'de_DE'
};

class Dumpinator {
  static run() {
    const tests = [
      { url: 'http://localhost:3000/api/v1/assets/1629266', headers, id: 'assets-left' },
      { url: 'http://localhost:3000/api/v1/assets/1629266', headers, id: 'assets-right' }
    ];

    const paralelRequests = 2;
    const jobs = [];

    tests.forEach((test) => {
      jobs.push(co(function* task() {
        const request = new Request();
        const response = yield request.load(test);

        const stash = new Stash(path.join(__dirname, `../tmp/${test.id}.json`));
        yield stash.add(response);
      }));
    });

    const p = this.paralize(jobs, paralelRequests);
    return p;
  }

  /**
   *
   * Runs n promises paralel
   *
   * @method paralize
   *
   * @param  {array} jobs Promise queue. Must be an array of promises
   * @param  {number} numParalel Number of paralel jobs
   *
   * @return {Object} Returns a promise after last job hast been done
   */
  static paralize(jobs, numParalel) {
    numParalel = numParalel || 2;
    const slots = [];
    const results = [];
    const beer = 'full';

    for (let i = 0; i < numParalel; i += 1) {
      slots.push(co(function* slotGenerator() {
        while (beer !== 'empty') { // it never gets empty ;)
          const next = jobs.shift();
          if (!next) {
            return; // we're finish now. Lets have a coffee :)
          }

          const res = yield next;
          results.push(res);
        }
      }));
    }

    return Promise.all(slots).then(() => results);
  }
}

module.exports = Dumpinator;

@@ -187,101 +187,8 @@
 son'
-,%0A  platform: 'web',%0A  device_type: 'webportal',%0A  client: 'mxd_package',%0A  language: 'de_DE'
 %0A%7D;%0A
@@ -266,47 +266,91 @@
 http
+s
 ://
-localhost:3000/api/v1/assets/1629266
+raw.githubusercontent.com/maxdome/dumpinator/develop/test/fixtures/v1/test.json
 ', h
@@ -400,47 +400,91 @@
 http
+s
 ://
-localhost:3000/api/v1/assets/1629266
+raw.githubusercontent.com/maxdome/dumpinator/develop/test/fixtures/v1/test.json
 ', h

Change test url