<commit_before>'use strict';

exports.register = function (plugin, options, next) {
  next();
};


var inflect = require('i')();
var _ = require('underscore');
var internals = {
  Hapi: null,
  server: null
};

var config = {};

config.get = function (resource, schema, server) {
  return {
    handler: function (request, reply) {
      if (request.params.id) {
        server.helpers.find(resource, request.params.id, function (docs) {
          reply(internals.serialize(resource, docs, schema));
        });
      } else {
        server.helpers.findMany(resource, function (docs) {
          reply(internals.serialize(resource, docs, schema));
        });
      }
    }
  };
};

config.post = function (resource, schema, server) {
  return {
    //validate: internals.validate(resource, schema, types),
    handler: function (request, reply) {
      server.helpers.insert(resource, internals.deserialize(resource, request.payload), function (docs) {
        reply(internals.serialize(resource, docs, schema));
      });
    }
  };
};

config.put = function (resource, schema, server) {
  return {
    validate: internals.validate(resource, schema, types),
    handler: function (request, reply) {
      server.helpers.update(resource, request.params.id, internals.deserialize(resource, request.payload), function (docs) {
        reply(internals.serialize(resource, docs, schema));
      });
    }
  };
};

config.patch = function (resource, schema, server) {
  return {
    validate: internals.validate(resource, schema, types),
    handler: function (request, reply) {
      reply('Not yet implemented. Sorry!');
    }
  };
};

config.delete = function (resource, schema, server) {
  return {
    handler: function (request, reply) {
      server.helpers.remove(resource, request.params.id, function (docs) {
        reply(docs);
      });
    }
  };
};

exports.config = config;

internals.validate = function (resource, schema) {
  var validation = {payload: {}},
    resourceSchema = {};

  _.each(schema, function (value, key) {
    if (typeof value === "string" ||
        (typeof value === "object" && value.ref) ||
        (value instanceof Array && value.length === 1 && value[0].ref)
        ) {
      if (!resourceSchema.links) {
        resourceSchema.links = {};
      }
      resourceSchema.links[key] = types.String();
    } else {
      resourceSchema[key] = value;
    }
  });

  validation.payload[inflect.pluralize(resource)] = types.Array().includes(types.Object(resourceSchema)).nullOk();

  return validation;
};

// Parses a resource and transforms it to be sent to clients
internals.serialize = function (resource, data, schema) {
  var json = {};

  json[inflect.pluralize(resource)] = [];<commit_msg>Changed index.js to correctly expose plugin spec<commit_after>'use strict';

exports.register = function (plugin, options, next) {

  plugin.expose(get, internals.get);
  plugin.expose(post, internals.post);
  plugin.expose(put, internals.put);
  plugin.expose(patch, internals.patch);
  plugin.expose(delete, internals.delete);

  next();
};


var inflect = require('i')();
var _ = require('underscore');
var internals = {
  Hapi: null,
  server: null
};

internals.get = function (resource, schema, server) {
  return {
    handler: function (request, reply) {
      if (request.params.id) {
        server.helpers.find(resource, request.params.id, function (docs) {
          reply(internals.serialize(resource, docs, schema));
        });
      } else {
        server.helpers.findMany(resource, function (docs) {
          reply(internals.serialize(resource, docs, schema));
        });
      }
    }
  };
};

internals.post = function (resource, schema, server) {
  return {
    //validate: internals.validate(resource, schema, types),
    handler: function (request, reply) {
      server.helpers.insert(resource, internals.deserialize(resource, request.payload), function (docs) {
        reply(internals.serialize(resource, docs, schema));
      });
    }
  };
};

internals.put = function (resource, schema, server) {
  return {
    validate: internals.validate(resource, schema, types),
    handler: function (request, reply) {
      server.helpers.update(resource, request.params.id, internals.deserialize(resource, request.payload), function (docs) {
        reply(internals.serialize(resource, docs, schema));
      });
    }
  };
};

internals.patch = function (resource, schema, server) {
  return {
    validate: internals.validate(resource, schema, types),
    handler: function (request, reply) {
      reply('Not yet implemented. Sorry!');
    }
  };
};

internals.delete = function (resource, schema, server) {
  return {
    handler: function (request, reply) {
      server.helpers.remove(resource, request.params.id, function (docs) {
        reply(docs);
      });
    }
  };
};

internals.validate = function (resource, schema) {
  var validation = {payload: {}},
    resourceSchema = {};

  _.each(schema, function (value, key) {
    if (typeof value === "string" ||
        (typeof value === "object" && value.ref) ||
        (value instanceof Array && value.length === 1 && value[0].ref)
        ) {
      if (!resourceSchema.links) {
        resourceSchema.links = {};
      }
      resourceSchema.links[key] = types.String();
    } else {
      resourceSchema[key] = value;
    }
  });

  validation.payload[inflect.pluralize(resource)] = types.Array().includes(types.Object(resourceSchema)).nullOk();

  return validation;
};

// Parses a resource and transforms it to be sent to clients
internals.serialize = function (resource, data, schema) {
  var json = {};

  json[inflect.pluralize(resource)] = [];
{'fraction_non_alphanumeric': 0.1499382607161757, 'fraction_numerical': 0.0007055918151349444, 'mean_word_length': 3.2376681614349776, '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': '429263', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1659, 'n_words': 380}