<commit_before>var request = require('superagent');
var utils = require('./utils');
var path = require('path');
var url = require('url');
var Promise = require('bluebird');

var hasOwn = {}.hasOwnProperty;

// Provide extension mechanism for Stripe Resource Sub-Classes
FigureResource.extend = utils.protoExtend;

// Expose method-creator
FigureResource.method = require('./FigureMethod');
FigureResource.BASIC_METHODS = require('./FigureMethod.basic');

function FigureResource(figure) {
  this._figure = figure;
  this.basePath = figure.getApiField('basePath');
  if (this.includeBasic) {
    this.includeBasic.forEach(function(methodName) {
      this[methodName] = FigureResource.BASIC_METHODS[methodName];
    }, this);
  }
  this.initialize.apply(this, arguments);
}

/**
 * Encapsulates request logic for a Figure Resource
 */
FigureResource.prototype = {

  path: '',

  initialize: function() {},

  createFullPath: function(commandPath, urlData) {

    return path.join(
      this.path,
      typeof commandPath == 'function' ?
        commandPath(urlData) : commandPath,
      '/'
    )
  },

  createDeferred: function(callback) {
    var deferred = Promise.defer();

    if (callback) {
      // Callback, if provided, is a simply translated to Promise'esque:
      // (Ensure callback is called outside of promise stack)
      deferred.promise.then(function(res) {
          setTimeout(function() { callback(null, res) }, 0);
        }, function(err) {
          setTimeout(function() { callback(err, null); }, 0);
        });
    }

    return deferred;
  },

  _errorHandler: function(res, callback) {
    callback(new Error('An error occurred with our connection to Figure'), null)
  },

  _responseHandler: function(res, callback) {
    var self = this;
    if (!res.ok) {
      if (res.status == 401) {
        err = new Error('Not Authorized');
      } else if (res.status == 429) {
        err = new Error('Too many requests');
      } else {
        err = new Error('An error occurred');
      }  
      return callback(err, null); 
    } 
    callback(null, res.body)
  },

  _request: function(method, path, data, query, headers, callback) {

    var self = this;
    var req;

    var fullUrl;
    var host = self._figure.getApiField('host');
    var protocol = self._figure.getApiField('protocol');
    if (host != null && protocol != null) {
      fullUrl = url.format({
        protocol: self._figure.getApiField('protocol'),
        host: self._figure.getApiField('host'),
        pathname: path
      });
    } else {
      fullUrl = path.join('/api', path);
    }

    switch (method) {
      case 'GET':
        req = request.get(fullUrl);
        break;
      case 'POST':
        req = request.post(fullUrl);
        break;
      case 'PUT':
        req = request.put(fullUrl);
        break;
      case 'DELETE':
        req = request.del(fullUrl);
        break;
    }

    if (query) {
      req = req.query(query);
    }

    // set headers
    if (headers) {
      Object.keys(headers).forEach(function(k) {
        req.set(k, headers[k]);
      });
    }
    req.set('Accept', 'application/json');
    var token = this._figure.getApiField('token')
    if (token) {
      req.set('Authorization', 'Bearer ' + token);
    }

    var timeout = self._figure.getApiField('timeout');
    req.timeout(timeout);

    if (data) {
      req.send(data);
    }

    req.end(function(err, res) {
      if (err) {
        self._errorHandler(res, callback);
      } else {
        self._responseHandler(res, callback);
      }
    });
  }
}

module.exports = FigureResource;<commit_msg>path.join -> _path.join<commit_after>var request = require('superagent');
var utils = require('./utils');
var _path = require('path');
var url = require('url');
var Promise = require('bluebird');

var hasOwn = {}.hasOwnProperty;

// Provide extension mechanism for Stripe Resource Sub-Classes
FigureResource.extend = utils.protoExtend;

// Expose method-creator
FigureResource.method = require('./FigureMethod');
FigureResource.BASIC_METHODS = require('./FigureMethod.basic');

function FigureResource(figure) {
  this._figure = figure;
  this.basePath = figure.getApiField('basePath');
  if (this.includeBasic) {
    this.includeBasic.forEach(function(methodName) {
      this[methodName] = FigureResource.BASIC_METHODS[methodName];
    }, this);
  }
  this.initialize.apply(this, arguments);
}

/**
 * Encapsulates request logic for a Figure Resource
 */
FigureResource.prototype = {

  path: '',

  initialize: function() {},

  createFullPath: function(commandPath, urlData) {

    return _path.join(
      this.path,
      typeof commandPath == 'function' ?
        commandPath(urlData) : commandPath,
      '/'
    )
  },

  createDeferred: function(callback) {
    var deferred = Promise.defer();

    if (callback) {
      // Callback, if provided, is a simply translated to Promise'esque:
      // (Ensure callback is called outside of promise stack)
      deferred.promise.then(function(res) {
          setTimeout(function() { callback(null, res) }, 0);
        }, function(err) {
          setTimeout(function() { callback(err, null); }, 0);
        });
    }

    return deferred;
  },

  _errorHandler: function(res, callback) {
    callback(new Error('An error occurred with our connection to Figure'), null)
  },

  _responseHandler: function(res, callback) {
    var self = this;
    if (!res.ok) {
      if (res.status == 401) {
        err = new Error('Not Authorized');
      } else if (res.status == 429) {
        err = new Error('Too many requests');
      } else {
        err = new Error('An error occurred');
      }  
      return callback(err, null); 
    } 
    callback(null, res.body)
  },

  _request: function(method, path, data, query, headers, callback) {

    var self = this;
    var req;

    var fullUrl;
    var host = self._figure.getApiField('host');
    var protocol = self._figure.getApiField('protocol');
    if (host != null && protocol != null) {
      fullUrl = url.format({
        protocol: self._figure.getApiField('protocol'),
        host: self._figure.getApiField('host'),
        pathname: path
      });
    } else {
      fullUrl = _path.join('/api', path);
    }

    switch (method) {
      case 'GET':
        req = request.get(fullUrl);
        break;
      case 'POST':
        req = request.post(fullUrl);
        break;
      case 'PUT':
        req = request.put(fullUrl);
        break;
      case 'DELETE':
        req = request.del(fullUrl);
        break;
    }

    if (query) {
      req = req.query(query);
    }

    // set headers
    if (headers) {
      Object.keys(headers).forEach(function(k) {
        req.set(k, headers[k]);
      });
    }
    req.set('Accept', 'application/json');
    var token = this._figure.getApiField('token')
    if (token) {
      req.set('Authorization', 'Bearer ' + token);
    }

    var timeout = self._figure.getApiField('timeout');
    req.timeout(timeout);

    if (data) {
      req.send(data);
    }

    req.end(function(err, res) {
      if (err) {
        self._errorHandler(res, callback);
      } else {
        self._responseHandler(res, callback);
      }
    });
  }
}

module.exports = FigureResource;
{'fraction_non_alphanumeric': 0.14342959789968218, 'fraction_numerical': 0.002210860853945005, 'mean_word_length': 2.6153846153846154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '3909811', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2159, 'n_words': 502}