<commit_before>'use strict';

var restify = require('restify');
var xtend = require('xtend');
var format = require('util').format;

var createURLRegex = require('./lib/create-regex');

var updateHandler = require('./handlers/update');
var createHandler = require('./handlers/create');
var getHandler = require('./handlers/get');
var deleteHandler = require('./handlers/delete');

var opts;
var methods;
var _apiBaseString = 'api';

var defaults = {
  apiBase: createURLRegex(_apiBaseString),
  deletedColumn: 'deleted',
  allowAccess: function(){
    return true;
  },
  logger: 'default',
  _actuallyDelete: false
};

/**
 * Listener for node-orm2 route method
 * @method  routeHandler
 * @async
 * @private
 * @param   {object} req Restify request object
 * @param   {object} res Resitfy response object
 * @returns {object} undefined
 */
function routeHandler(req, res){
  var method = req.method.toLowerCase();
  var url = req.url;
  var apiCall = url.split('/').slice(1);
  var resourceName = apiCall[1];
  var resourceId = apiCall[2];

  if(method === 'patch' && !methods[method]){
    method = 'put';
  }

  if(Object.keys(methods).indexOf(method) !== -1){
    if(!opts.db.models[resourceName]){
      opts.logger.info('%s is not a model on this db instance', Object.keys(opts.db.models));
      return res.send(new restify.ResourceNotFoundError(format('%s: not found', resourceName)));
    }

    if(!opts.allowAccess(req, method, resourceName, resourceId)){
      opts.logger.info('Access has been denied for', req.url);
      return res.send(new restify.InvalidCredentialsError('Not authorized'));
    }

    methods[method].call(null, resourceName, resourceId, req.body, function(){
      opts.logger.info('%s for %s/%s', method, resourceName, resourceId);
      return res.send.apply(res, Array.prototype.slice.call(arguments));
    });

  } else {<commit_msg>better error message<commit_after>'use strict';

var restify = require('restify');
var xtend = require('xtend');
var format = require('util').format;

var createURLRegex = require('./lib/create-regex');

var updateHandler = require('./handlers/update');
var createHandler = require('./handlers/create');
var getHandler = require('./handlers/get');
var deleteHandler = require('./handlers/delete');

var opts;
var methods;
var _apiBaseString = 'api';

var defaults = {
  apiBase: createURLRegex(_apiBaseString),
  deletedColumn: 'deleted',
  allowAccess: function(){
    return true;
  },
  logger: 'default',
  _actuallyDelete: false
};

/**
 * Listener for node-orm2 route method
 * @method  routeHandler
 * @async
 * @private
 * @param   {object} req Restify request object
 * @param   {object} res Resitfy response object
 * @returns {object} undefined
 */
function routeHandler(req, res){
  var method = req.method.toLowerCase();
  var url = req.url;
  var apiCall = url.split('/').slice(1);
  var resourceName = apiCall[1];
  var resourceId = apiCall[2];

  if(method === 'patch' && !methods[method]){
    method = 'put';
  }

  if(Object.keys(methods).indexOf(method) !== -1){
    if(!opts.db.models[resourceName]){
      opts.logger.info('%s is not a model on this db instance', resourceName, Object.keys(opts.db.models));
      return res.send(new restify.ResourceNotFoundError(format('%s: not found', resourceName)));
    }

    if(!opts.allowAccess(req, method, resourceName, resourceId)){
      opts.logger.info('Access has been denied for', req.url);
      return res.send(new restify.InvalidCredentialsError('Not authorized'));
    }

    methods[method].call(null, resourceName, resourceId, req.body, function(){
      opts.logger.info('%s for %s/%s', method, resourceName, resourceId);
      return res.send.apply(res, Array.prototype.slice.call(arguments));
    });

  } else {
{'fraction_non_alphanumeric': 0.15803452855245684, 'fraction_numerical': 0.0026560424966799467, 'mean_word_length': 4.28932584269663, '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': '4748074', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1145, 'n_words': 287}