<commit_before>var Promise = require("bluebird");
var request = require("request");
var colors = require("colors");

function Action(specObject) {
  this.specObject = specObject;
  this.baseRequest = this.parseDefaults();
}

Action.prototype.list = function() {
  var data = this.specObject.action;
  var names = [];
  for (var key in data) {
    names[key] = data[key].name;
  }
  return names;
};

Action.prototype.parseAction = function(actionName) {
  var data = this.specObject.action;

  for (var key in data) {
    if (data[key].name === actionName) {
      return data[key];
    }
  }
};

Action.prototype.buildRequest = function(parsedAction) {
  var requestPromisified = Promise.promisify(request);
  var options = this.createOptionsObject(parsedAction);
  var extractedData = this.extractData(parsedAction);
  var self = this;
  var data;
  var returnedFunction = function(param) {
    if (param !== undefined) {
      var fullUri = options.uri + param;
      options.uri = fullUri;
    }
    requestPromisified(options)
      .then(function(response) {
        // console.log(response.body);
        data = JSON.parse(response.body);
      })
      .then(function() {
        processedResponse = self.processResponse(data, extractedData);
        console.log(processedResponse);
      })
      .catch(function(error) {
        throw error;
      });
  };
  return returnedFunction;
};

Action.prototype.createOptionsObject = function(parsedAction) {

  var options = {}

  if (parsedAction.options !== undefined) {
    return parsedAction.options
  }

  var excludedOptions = ['name', 'after', 'extract']
  for (var key in parsedAction) {
    if (excludedOptions.indexOf(key) === -1) {
      options[key] = parsedAction[key];
    }
  }

  return options;
};
Action.prototype.processArguments = function(string) {
  this.isArgument(parsedAction[key]) ? arguments[parsedAction[key][1]] : parsedAction[key];
};
Action.prototype.isArgument = function(string) {
  return string[0] === '<' && string[string.length - 1] === '>';
};

Action.prototype.parseDefaults = function() {
  var defaultsObject = {};
  if (this.specObject.defaults !== undefined) {
    defaultsObject = this.specObject.defaults;
  }

  defaultsObject.jar = true;
  return request.defaults(defaultsObject);
};

Action.prototype.extractData = function(parsedAction) {
  var fieldsToExtract = parsedAction.extract;
  console.log("fields to extract: ", fieldsToExtract);
  if (fieldsToExtract !== undefined) {
    return fieldsToExtract.map(function(value) {
      return value.split(".");
    });
  }
};

Action.prototype.processResponse = function(response, extractedData) {
  // console.log(extractedData);
  // console.log(typeof response);
  var result;

  function processObject(object, fieldsToExtract) {
    return fieldsToExtract.map(function(innerArray) {
      return innerArray.reduce(function(previous, current) {
        previous = previous[current];
        // console.log(previous);
        return previous;
      }, object);
    });
  }

  function processArray(array, fieldsToExtract){
    return array.map(function(innerObject){
      processObject(innerObject, fieldsToExtract);
    })
  }

  result = processObject(response, extractedData);



  return result;






  // function getFieldRecursively (response){
  //   if (response instanceof String){
  //     // result.push(response);
  //     // return;
  //     return response;
  //   }

  //   return getFieldRecursively (response[])
  // };


  // var responseBackup = response;

  // for (key in extractedData) {
  //   var auxiliar = extractedData[key];

  //   for (key2 in extractedData[key]) {
  //     // console.log(auxiliar2);
  //     var auxiliar2 = extractedData[key][key2];
  //     if (response[auxiliar2]) {
  //       response = response[auxiliar2];
  //     }
  //   }
  //   console.log(auxiliar2.yellow + ":\n");
  //   if (response !== undefined) {
  //     console.log(response.green + "\n");
  //   } else {
  //     console.log("Not available for this user \n");
  //   }

  //   response = responseBackup;
  // }
};

module.exports = Action;
<commit_msg>request.defaults did not work. Added cookie jar and started work on the arguments<commit_after>var Promise = require("bluebird");
var request = require("request");
var colors = require("colors");

function Action(specObject) {
  this.specObject = specObject;
  this.baseRequest = this.parseDefaults();
  this.cookieJar = request.jar();
}

Action.prototype.list = function() {
  var data = this.specObject.action;
  var names = [];
  for (var key in data) {
    names[key] = data[key].name;
  }
  return names;
};

Action.prototype.parseAction = function(actionName) {
  var data = this.specObject.action;

  for (var key in data) {
    if (data[key].name === actionName) {
      return data[key];
    }
  }
};

Action.prototype.buildRequest = function(parsedAction) {
  var requestPromisified = Promise.promisify(request);
  var options = this.createOptionsObject(parsedAction);
  var extractedData = this.extractData(parsedAction);
  var self = this;
  var data;
  var returnedFunction = function(param) {


    if (param !== undefined) {
      var fullUri = options.uri + param;
      options.uri = fullUri;
    }
    // console.log(request(options));
    requestPromisified(options)
      .then(function(response) {
        // console.log(response.body);
        data = JSON.parse(response.body);
      })
      .then(function() {
        processedResponse = self.processResponse(data, extractedData);
        console.log(processedResponse);
      })
      .catch(function(error) {
        throw error;
      });
  };
  return returnedFunction;
};

Action.prototype.createOptionsObject = function(parsedAction) {

  var options = {}

  if (parsedAction.options !== undefined) {
    return parsedAction.options
  }

  var excludedOptions = ['name', 'after', 'extract', 'pathParam']
  for (var key in parsedAction) {
    if (excludedOptions.indexOf(key) === -1) {
      options[key] = parsedAction[key];
    }
  }
  options.jar = this.cookieJar;

  return options;
};
Action.prototype.processArguments = function(string) {
  this.isArgument(parsedAction[key]) ? arguments[parsedAction[key][1]] : parsedAction[key];
};
Action.prototype.isArgument = function(string) {
  return string[0] === '<' && string[string.length - 1] === '>';
};

Action.prototype.parseDefaults = function() {


  // var defaultsObject = {};
  // if (this.specObject.defaults !== undefined) {
  //   defaultsObject = this.specObject.defaults;
  // }

  // defaultsObject.jar = true;
  // return request.defaults(defaultsObject);
};

Action.prototype.extractData = function(parsedAction) {
  var fieldsToExtract = parsedAction.extract;
  // console.log("fields to extract: ", fieldsToExtract);
  if (fieldsToExtract !== undefined) {
    return fieldsToExtract.map(function(value) {
      return value.split(".");
    });
  }
};

Action.prototype.processResponse = function(response, extractedData) {
  // console.log(extractedData);
  // console.log(typeof response);
  var result;

  function processObject(object, fieldsToExtract) {
    return fieldsToExtract.map(function(innerArray) {
      return innerArray.reduce(function(previous, current) {
        previous = previous[current];
        // console.log(previous);
        return previous;
      }, object);
    });
  }

  function processArray(array, fieldsToExtract){
    return array.map(function(innerObject){
      processObject(innerObject, fieldsToExtract);
    })
  }

  result = processObject(response, extractedData);



  return result;
};

module.exports = Action;

{'fraction_non_alphanumeric': 0.1413228552717747, 'fraction_numerical': 0.0019646365422396855, 'mean_word_length': 3.3165630299604296, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5065863', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2263, 'n_words': 452}