<commit_before>var defaultCommands = require('./default-commands/rest');

function createHandler(params) {
  params = params || {};
  var connection = params.connection;
  var command = params.command;
  var requestBuilder = params.requestBuilder;

  if (!requestBuilder)
    throw new Error('no request builder for ' + command);

  return (function(request, response) {
    var message = requestBuilder(request);
    var timeout = message.timeout || null;
    connection.emitMessage(
      command,
      message,
      function(error, responseMessage) {
        if (error) {
          var body = responseMessage && responseMessage.body || null;
          response.contentType('application/json');
          response.send(body, error);
        } else {
          var body = responseMessage.body;
          response.contentType('application/json');
          response.send(body, 200);
        }
      },
      { timeout: timeout }
    );
  });
}
exports.createHandler = createHandler;

function getRegisterationMethod(method) {
  switch (method.toUpperCase()) {
    case 'GET':    return 'get';
    case 'PUT':    return 'put';
    case 'POST':   return 'post';
    case 'DELETE': return 'del';
    default:
      throw new Error(method + ' is unsppported HTTP method!');
  }
}
exports.getRegisterationMethod = getRegisterationMethod;

exports.register = function(application, params) {
  params = params || {};
  var connection = params.connection;
  if (!connection)
    throw new Error('Connection to the backend is required!');

  var prefix = params.prefix || '';
  prefix = prefix.replace(/\/$/, '');

  var commandSets = [defaultCommands].concat(params.plugins || []);
  var unifiedCommandSets = {};
  commandSets.forEach(function(commandSet) {
    if (typeof commandSets == 'string')
      commandSets = require(commandSets);

    Object.keys(commandSets).forEach(function(command) {
      var definition = commandSets[command];
      unifiedCommandSets[command] = definition;
    });
  });

  Object.keys(unifiedCommandSets).forEach(function(command) {
    var definition = unifiedCommandSets[command];
    var method = getRegisterationMethod(definition.method);
    application[method](
      prefix + definition.path,
      createHandler({
        connection:     connection,
        command:        command,
        requestBuilder: definition.requestBuilder
      })
    );<commit_msg>Rename variable to more suitable one: unifiedCommandSets => unifiedCommandSet<commit_after>var defaultCommands = require('./default-commands/rest');

function createHandler(params) {
  params = params || {};
  var connection = params.connection;
  var command = params.command;
  var requestBuilder = params.requestBuilder;

  if (!requestBuilder)
    throw new Error('no request builder for ' + command);

  return (function(request, response) {
    var message = requestBuilder(request);
    var timeout = message.timeout || null;
    connection.emitMessage(
      command,
      message,
      function(error, responseMessage) {
        if (error) {
          var body = responseMessage && responseMessage.body || null;
          response.contentType('application/json');
          response.send(body, error);
        } else {
          var body = responseMessage.body;
          response.contentType('application/json');
          response.send(body, 200);
        }
      },
      { timeout: timeout }
    );
  });
}
exports.createHandler = createHandler;

function getRegisterationMethod(method) {
  switch (method.toUpperCase()) {
    case 'GET':    return 'get';
    case 'PUT':    return 'put';
    case 'POST':   return 'post';
    case 'DELETE': return 'del';
    default:
      throw new Error(method + ' is unsppported HTTP method!');
  }
}
exports.getRegisterationMethod = getRegisterationMethod;

exports.register = function(application, params) {
  params = params || {};
  var connection = params.connection;
  if (!connection)
    throw new Error('Connection to the backend is required!');

  var prefix = params.prefix || '';
  prefix = prefix.replace(/\/$/, '');

  var commandSets = [defaultCommands].concat(params.plugins || []);
  var unifiedCommandSet = {};
  commandSets.forEach(function(commandSet) {
    if (typeof commandSets == 'string')
      commandSets = require(commandSets);

    Object.keys(commandSets).forEach(function(command) {
      var definition = commandSets[command];
      unifiedCommandSet[command] = definition;
    });
  });

  Object.keys(unifiedCommandSet).forEach(function(command) {
    var definition = unifiedCommandSet[command];
    var method = getRegisterationMethod(definition.method);
    application[method](
      prefix + definition.path,
      createHandler({
        connection:     connection,
        command:        command,
        requestBuilder: definition.requestBuilder
      })
    );
{'fraction_non_alphanumeric': 0.12175525339925834, 'fraction_numerical': 0.0012360939431396785, 'mean_word_length': 3.2180712423979148, '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': '1526594', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1264, 'n_words': 305}