<commit_before>'use strict';

var angular   = require('angular');
var url       = require('url');
var qs        = require('qs');
var internals = {};

module.exports = function ($http, $q, ConvexCache, convexConfig) {

  function ConvexRequest (config) {
    this.config = internals.config(config);
    this.deferred = $q.defer();
  }

  internals.qs = function (params) {
    if (Object.keys(params).length) {
      return '?' + qs.stringify(params);
    }
    else {
      return '';
    }
  };

  internals.config = function (input) {
    input = input || {};
    var output = angular.extend({}, input);
    output.method = (input.method || 'get').toUpperCase();
    if (output.url) {
      var parsed = url.parse(output.url, true);
      output.base = parsed.protocol + '//' + parsed.host;
      output.path = parsed.pathname;
      output.params = parsed.query;
    }
    else {
      output.base = input.base || convexConfig.base || '';
      output.path = input.path || '';
      output.params = input.params || {};
      output.url = output.base + output.path; 
      output.url += internals.qs(output.params);
    }
    return output;
  };

  ConvexRequest.prototype.$$cache = new ConvexCache('ConvexRequest');

  ConvexRequest.prototype.toJSON = function () {
    return {
      method: this.config.method,
      path: this.config.path + internals.qs(this.config.params),
      payload: this.config.data
    };
  };

  internals.getCachedRequest = function (request) {
    if (request.config.cache && request.config.method === 'GET') { 
      var cached = request.$$cache.get(
        request.config.url,
        request.config.cache === 'persist'
      );
      if (cached) request.cacheHit = true;
      return cached;
    }
  };

  internals.putCachedResponse = function (request, response) {
    if (request.config.cache && !request.cacheHit) {
      request.$$cache.put(
        request.config.url,
        response.data,
        request.config.cache === 'persist'
      );
    }
  };

  ConvexRequest.prototype.send = function () {
    var request = this;
    return $q.when(internals.getCachedRequest(this) || $http({
        method: this.config.method,
        url: this.config.url,
        data: this.config.data
      }))
      .then(function (response) {
        internals.putCachedResponse(request, response);
        return response;
      })
      .then(angular.bind(this, this.fulfill))
      .catch(angular.bind(this, this.reject));
  };

  ConvexRequest.prototype.fulfill = function (response) {
    $q.when(response)
      .then(function (response) {
        if (typeof response.headers !== 'function') {
          if (response.error) {
            return $q.reject({
              status: response.statusCode,
              data: response,
            });
          }
          else {
            return response;
          }
        }
        else {
          return response.data;
        }
      })
      .then(this.deferred.resolve)
      .catch(angular.bind(this, this.reject));<commit_msg>Replace internals with named fns<commit_after>'use strict';

var angular = require('angular');
var url     = require('url');
var qs      = require('qs');

module.exports = function ($http, $q, ConvexCache, convexConfig) {

  function ConvexRequest (config) {
    this.config = parseConfig(config);
    this.deferred = $q.defer();
  }

  function querystring (params) {
    if (Object.keys(params).length) {
      return '?' + qs.stringify(params);
    }
    else {
      return '';
    }
  }

  function parseConfig (input) {
    input = input || {};
    var output = angular.extend({}, input);
    output.method = (input.method || 'get').toUpperCase();
    if (output.url) {
      var parsed = url.parse(output.url, true);
      output.base = parsed.protocol + '//' + parsed.host;
      output.path = parsed.pathname;
      output.params = parsed.query;
    }
    else {
      output.base = input.base || convexConfig.base || '';
      output.path = input.path || '';
      output.params = input.params || {};
      output.url = output.base + output.path; 
      output.url += querystring(output.params);
    }
    return output;
  }

  ConvexRequest.prototype.$$cache = new ConvexCache('ConvexRequest');

  ConvexRequest.prototype.toJSON = function () {
    return {
      method: this.config.method,
      path: this.config.path + querystring(this.config.params),
      payload: this.config.data
    };
  };

  function getCachedRequest (request) {
    if (request.config.cache && request.config.method === 'GET') { 
      var cached = request.$$cache.get(
        request.config.url,
        request.config.cache === 'persist'
      );
      if (cached) request.cacheHit = true;
      return cached;
    }
  }

  function putCachedResponse (request, response) {
    if (request.config.cache && !request.cacheHit) {
      request.$$cache.put(
        request.config.url,
        response.data,
        request.config.cache === 'persist'
      );
    }
  }

  ConvexRequest.prototype.send = function () {
    var request = this;
    return $q.when(getCachedRequest(this) || $http({
        method: this.config.method,
        url: this.config.url,
        data: this.config.data
      }))
      .then(function (response) {
        putCachedResponse(request, response);
        return response;
      })
      .then(angular.bind(this, this.fulfill))
      .catch(angular.bind(this, this.reject));
  };

  ConvexRequest.prototype.fulfill = function (response) {
    $q.when(response)
      .then(function (response) {
        if (typeof response.headers !== 'function') {
          if (response.error) {
            return $q.reject({
              status: response.statusCode,
              data: response,
            });
          }
          else {
            return response;
          }
        }
        else {
          return response.data;
        }
      })
      .then(this.deferred.resolve)
      .catch(angular.bind(this, this.reject));
{'fraction_non_alphanumeric': 0.1447941078004687, 'fraction_numerical': 0.0, 'mean_word_length': 2.2332251082251084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6561716', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1763, 'n_words': 341}