<commit_before>var util = require('util');
var lodash = require('lodash');
var Emitter = require('eventemitter3');
var Token = require('./AccessToken');
var Endpoint = require('./Endpoint');


module.exports = Engine;

util.inherits(Engine, Emitter);


/**
 * API engine
 *
 * @param {object} config
 * @param {string} config.clientID
 * @param {string} config.clientSecret
 * @param {string} config.grantType
 */

function Engine (config) {
  this.config = config;
  this.tokens = null;
  this._endpoints = {};
  this._subclasses = [];
}


/**
 * Global custom Endpoint subclass registry.
 *
 * @protected
 */

Engine._endpointClasses = [];


/**
 * Register a global custom Endpoint class.
 *
 * When creating endpoints with the
 * Engine#endpoint() method, if the endpoint
 * URI matches `uriRegex`, we will use `Class`
 * instead of `Endpoint` as constructor.
 *
 * Individual Engine instances can also
 * register custom subclasses with the
 * Engine#register() method.
 *
 * @param {RegExp} uriRegex
 * @param {Function} Class
 */

Engine.register = function (uriRegex, cls) {
  Engine._endpointClasses.push([uriRegex, cls]);
  return Engine;
};


/**
 * Util function to fix API paths.
 */

Engine.fixPath = function (path) {
  path = path.replace(/\/+$/, '');
  path = path[0] === '/' ? path : '/' + path;
  path = path.search(/\.json$/) > 0 ? path : path + '.json';
  return path;
};


/**
 * Start the API engine (fetches access tokens).
 *
 * @returns {Engine} self
 */

Engine.prototype.start = function () {
  if (!this.tokens) {
    if (!this.config.clientID) {
      throw new Error('Engine requires the "clientID" config');
    }
    if (!this.config.clientSecret) {
      throw new Error('Engine requires the "clientSecret" config');
    }
    this.tokens = new Token({
      url: 'https://www.reddit.com/api/v1/access_token',
      id: this.config.clientID,
      secret: this.config.clientSecret,
      type: this.config.grantType
    });
    this.tokens
      .on('error', this.emit.bind(this, 'error'))
      .on('data', this.emit.bind(this, 'token'));
  }

  this.tokens.poll();

  return this;
};


/**
 * Get an endpoint request object.
 *
 * @param {string} path
 * @returns {Endpoint}
 */

Engine.prototype.endpoint = function (path) {
  if (!this.tokens) {
    throw new Error('Engine: call .start() before accessing endpoints');
  }<commit_msg>Check if engine is already polling<commit_after>var util = require('util');
var lodash = require('lodash');
var Emitter = require('eventemitter3');
var Token = require('./AccessToken');
var Endpoint = require('./Endpoint');


module.exports = Engine;

util.inherits(Engine, Emitter);


/**
 * API engine
 *
 * @param {object} config
 * @param {string} config.clientID
 * @param {string} config.clientSecret
 * @param {string} config.grantType
 */

function Engine (config) {
  this.config = config;
  this.tokens = null;
  this._endpoints = {};
  this._subclasses = [];
}


/**
 * Global custom Endpoint subclass registry.
 *
 * @protected
 */

Engine._endpointClasses = [];


/**
 * Register a global custom Endpoint class.
 *
 * When creating endpoints with the
 * Engine#endpoint() method, if the endpoint
 * URI matches `uriRegex`, we will use `Class`
 * instead of `Endpoint` as constructor.
 *
 * Individual Engine instances can also
 * register custom subclasses with the
 * Engine#register() method.
 *
 * @param {RegExp} uriRegex
 * @param {Function} Class
 */

Engine.register = function (uriRegex, cls) {
  Engine._endpointClasses.push([uriRegex, cls]);
  return Engine;
};


/**
 * Util function to fix API paths.
 */

Engine.fixPath = function (path) {
  path = path.replace(/\/+$/, '');
  path = path[0] === '/' ? path : '/' + path;
  path = path.search(/\.json$/) > 0 ? path : path + '.json';
  return path;
};


/**
 * Start the API engine (fetches access tokens).
 *
 * @returns {Engine} self
 */

Engine.prototype.start = function () {
  if (!this.tokens) {
    if (!this.config.clientID) {
      throw new Error('Engine requires the "clientID" config');
    }
    if (!this.config.clientSecret) {
      throw new Error('Engine requires the "clientSecret" config');
    }
    this.tokens = new Token({
      url: 'https://www.reddit.com/api/v1/access_token',
      id: this.config.clientID,
      secret: this.config.clientSecret,
      type: this.config.grantType
    });
    this.tokens
      .on('error', this.emit.bind(this, 'error'))
      .on('data', this.emit.bind(this, 'token'));
  }

  if (!this.tokens.isPolling()) {
    this.tokens.poll();
  }

  return this;
};


/**
 * Get an endpoint request object.
 *
 * @param {string} path
 * @returns {Endpoint}
 */

Engine.prototype.endpoint = function (path) {
  if (!this.tokens) {
    throw new Error('Engine: call .start() before accessing endpoints');
  }
{'fraction_non_alphanumeric': 0.16129032258064516, 'fraction_numerical': 0.0016649323621227888, 'mean_word_length': 3.298747763864043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3992403', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1480, 'n_words': 411}