<commit_before>'use strict';

var _ = require('lodash');
var request = require('request');

var Joi     = require('joi');
var Promise = require('bluebird');

function buildQuery(options) {
  var results = [];
  Object.keys(options).forEach(function(key) {
    var value = options[key];
    if (_.isArray(value)) {
      value = value.join(',');
    }

    results.push(key + '=' + value);
  });
  return results.length ? '?' + results.join('&') : '' ;
}

function requestCallback(resolve, reject, err, res, body) {
  if (err) {
    return reject(err);
  }

  if (res.statusCode !== 200) {
    return reject(
      new Error('Problem retrieving data: ' + JSON.stringify(res))
    );
  }

  return resolve(body ? JSON.parse(body) : null);
}

function requestHandler(resolve, reject, path, token, options) {
  options = options || {};
  var requestOptions = {
    url     : 'https://api.twitter.com/1.1/' + path + '.json' +
              buildQuery(options),
    method  : 'GET',
    headers : {
      'Authorization'  : 'Bearer ' + token,
      'User-Agent'     : 'request'
    },
  };

  request.get(requestOptions, requestCallback.bind(null, resolve, reject));
}

function validateOptions(token, schema, options) {
  return new Promise(function(resolve, reject) {
    if (typeof token === 'undefined') {
      return reject(false);
    }
    if (options == null || typeof options !== 'object') {
      return reject(new TypeError('\'options\' must be an object.'));
    }

    Joi.validate(options, schema, function(err, value) {
      if (err) {
        return reject(err);
      }

      resolve();
    });
  });
}

function generatedNoSchemaHandler(path, token, options) {
  return new Promise(function(resolve, reject) {
    requestHandler(resolve, reject, path, token, options);
  });
}

function generatedApiHandler(path, schema, token, options) {
  return new Promise(function(resolve, reject) {
    return validateOptions(token, schema, options)
    .then(function() {
      requestHandler(resolve, reject, path, token, options);
    });
  });
}

function generateApiHandler(path, schema) {
  return generatedApiHandler.bind(null, path, schema);
}

function generateUrlInsertedHandler(insertedValueNames, pathInterleves, schema){
  // pathInterleves[i] + options[insertedValueName[i]]
  // ex. /test/2/foo ivn = ['k'], pi = ['test', 'foo'], options={ k : 2 }

  if (insertedValueNames.length < (pathInterleves - 1)) {
    throw new Error(
      'There must be at least one more path interleve than inserted value'
    );
  }

  return function filterOptionsAndContinue(token, options) {
    return new Promise(function(resolve, reject) {
      return validateOptions(token, schema, options)
      .then(function() {
        var insertedValues = _.map(
          insertedValueNames,
          function mapValueNameToValue(valueName) {
            var tmp = options[valueName];
            delete options[valueName];
            return tmp;
          }
        );

        var path = '';
        _.forEach(insertedValues, function(value, index) {
          path += pathInterleves[index] + value;
        });

        if (pathInterleves.length > insertedValues) {
          path += pathInterleves[pathInterleves.length - 1];
        }

        return requestHandler(resolve, reject, path, token, options);
      });
    });
  };
}

function generateNoSchemaHandler(path) {
  return generatedNoSchemaHandler.bind(null, path);
}

module.exports = {
  buildQuery : buildQuery,
  generateApiHandler : generateApiHandler,
  generateNoSchemaHandler : generateNoSchemaHandler,
  generateUrlInsertedHandler : generateUrlInsertedHandler,
};<commit_msg>Added missing '/' for url generation<commit_after>'use strict';

var _ = require('lodash');
var request = require('request');

var Joi     = require('joi');
var Promise = require('bluebird');

function buildQuery(options) {
  var results = [];
  Object.keys(options).forEach(function(key) {
    var value = options[key];
    if (_.isArray(value)) {
      value = value.join(',');
    }

    results.push(key + '=' + value);
  });
  return results.length ? '?' + results.join('&') : '' ;
}

function requestCallback(resolve, reject, err, res, body) {
  if (err) {
    return reject(err);
  }

  if (res.statusCode !== 200) {
    return reject(
      new Error('Problem retrieving data: ' + JSON.stringify(res))
    );
  }

  return resolve(body ? JSON.parse(body) : null);
}

function requestHandler(resolve, reject, path, token, options) {
  options = options || {};
  var requestOptions = {
    url     : 'https://api.twitter.com/1.1/' + path + '.json' +
              buildQuery(options),
    method  : 'GET',
    headers : {
      'Authorization'  : 'Bearer ' + token,
      'User-Agent'     : 'request'
    },
  };

  request.get(requestOptions, requestCallback.bind(null, resolve, reject));
}

function validateOptions(token, schema, options) {
  return new Promise(function(resolve, reject) {
    if (typeof token === 'undefined') {
      return reject(false);
    }
    if (options == null || typeof options !== 'object') {
      return reject(new TypeError('\'options\' must be an object.'));
    }

    Joi.validate(options, schema, function(err, value) {
      if (err) {
        return reject(err);
      }

      resolve();
    });
  });
}

function generatedNoSchemaHandler(path, token, options) {
  return new Promise(function(resolve, reject) {
    requestHandler(resolve, reject, path, token, options);
  });
}

function generatedApiHandler(path, schema, token, options) {
  return new Promise(function(resolve, reject) {
    return validateOptions(token, schema, options)
    .then(function() {
      requestHandler(resolve, reject, path, token, options);
    });
  });
}

function generateApiHandler(path, schema) {
  return generatedApiHandler.bind(null, path, schema);
}

function generateUrlInsertedHandler(insertedValueNames, pathInterleves, schema){
  // pathInterleves[i] + options[insertedValueName[i]]
  // ex. /test/2/foo ivn = ['k'], pi = ['test', 'foo'], options={ k : 2 }

  if (insertedValueNames.length < (pathInterleves - 1)) {
    throw new Error(
      'There must be at least one more path interleve than inserted value'
    );
  }

  return function filterOptionsAndContinue(token, options) {
    return new Promise(function(resolve, reject) {
      return validateOptions(token, schema, options)
      .then(function() {
        var insertedValues = _.map(
          insertedValueNames,
          function mapValueNameToValue(valueName) {
            var tmp = options[valueName];
            delete options[valueName];
            return tmp;
          }
        );

        var path = '';
        _.forEach(insertedValues, function(value, index) {
          path += pathInterleves[index] + '/' + value;
        });

        if (pathInterleves.length > insertedValues) {
          path += '/' + pathInterleves[pathInterleves.length - 1];
        }

        return requestHandler(resolve, reject, path, token, options);
      });
    });
  };
}

function generateNoSchemaHandler(path) {
  return generatedNoSchemaHandler.bind(null, path);
}

module.exports = {
  buildQuery : buildQuery,
  generateApiHandler : generateApiHandler,
  generateNoSchemaHandler : generateNoSchemaHandler,
  generateUrlInsertedHandler : generateUrlInsertedHandler,
};
{'fraction_non_alphanumeric': 0.1387259582594462, 'fraction_numerical': 0.002455326694857455, 'mean_word_length': 3.1447145279819106, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1587257', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2132, 'n_words': 506}