<commit_before>'use strict';

var joi = require('joi');
var hapi = require('hapi');
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var hoek = require('hoek');
var swig = require('swig');
var extras = require('swig-extras');
var util = require('util');
var async = require('async');
var RepresentationFactory = require('./representation').RepresentationFactory;
var urlTemplate = require('url-template');
var Negotiator = require('negotiator');
var URITemplate = require('URIjs/src/URITemplate');
var url = require('url');
var URI = require('URIjs');

extras.useFilter(swig, 'markdown');

var re = /\{([^\{\}]+)\}|([^\{\}]+)/g;

function reach(object, path) {
    var parts = path ? path.split('.') : [];

    for (var i = 0; i < parts.length && !_.isUndefined(object); i++) {
        object = object[parts[i]];
    }

    return object;
}

/**
 * evaluates and flattens deep expressions (e.g. '/{foo.a.b}') into a single level context object: {'foo.a.b': value}
 * so that it may be used by url-template library
 * @param template
 * @param ctx
 * @return {{}}
 */
function flattenContext(template, ctx) {
    var arr, result = {};

    while ((arr = re.exec(template)) !== null) {
        if (arr[1]) {
            var value = reach(ctx, arr[1]);
            result[arr[1]] = value && value.toString();
        }
    }

    return result;
}

var optionsSchema = {
    absolute: joi.boolean().default(false),
    hostname: joi.string(),
    port: joi.number().integer(),
    protocol: joi.string(),
    strict: joi.boolean().default(false),
    relsPath: joi.string().default('/rels'),
    relsAuth: joi.alternatives().try(joi.boolean().allow(false),joi.object()).default(false),
    autoApi: joi.boolean().default(true),
    apiPath: joi.string().default('/api'),
    apiAuth: joi.alternatives().try(joi.boolean().allow(false),joi.object()).default(false),
    apiServerLabel: joi.string(),
    mediaTypes: joi.array().includes(joi.string()).single().default(['application/hal+json'])
};

/**
 * Registers plugin routes and an "api" object with the hapi server.
 * @param server
 * @param opts
 * @param next
 */
exports.register = function (server, opts, next) {
    var settings = opts;

    joi.validate(opts, optionsSchema, function (err, validated) {
        if (err) throw err;

        settings = validated;
    });<commit_msg>Allow empty apiPath now that the newer version of Joi doesnt allow that by default with just joi.string()<commit_after>'use strict';

var joi = require('joi');
var hapi = require('hapi');
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var hoek = require('hoek');
var swig = require('swig');
var extras = require('swig-extras');
var util = require('util');
var async = require('async');
var RepresentationFactory = require('./representation').RepresentationFactory;
var urlTemplate = require('url-template');
var Negotiator = require('negotiator');
var URITemplate = require('URIjs/src/URITemplate');
var url = require('url');
var URI = require('URIjs');

extras.useFilter(swig, 'markdown');

var re = /\{([^\{\}]+)\}|([^\{\}]+)/g;

function reach(object, path) {
    var parts = path ? path.split('.') : [];

    for (var i = 0; i < parts.length && !_.isUndefined(object); i++) {
        object = object[parts[i]];
    }

    return object;
}

/**
 * evaluates and flattens deep expressions (e.g. '/{foo.a.b}') into a single level context object: {'foo.a.b': value}
 * so that it may be used by url-template library
 * @param template
 * @param ctx
 * @return {{}}
 */
function flattenContext(template, ctx) {
    var arr, result = {};

    while ((arr = re.exec(template)) !== null) {
        if (arr[1]) {
            var value = reach(ctx, arr[1]);
            result[arr[1]] = value && value.toString();
        }
    }

    return result;
}

var optionsSchema = {
    absolute: joi.boolean().default(false),
    hostname: joi.string(),
    port: joi.number().integer(),
    protocol: joi.string(),
    strict: joi.boolean().default(false),
    relsPath: joi.string().default('/rels'),
    relsAuth: joi.alternatives().try(joi.boolean().allow(false),joi.object()).default(false),
    autoApi: joi.boolean().default(true),
    apiPath: joi.string().allow('').default('/api'),
    apiAuth: joi.alternatives().try(joi.boolean().allow(false),joi.object()).default(false),
    apiServerLabel: joi.string(),
    mediaTypes: joi.array().includes(joi.string()).single().default(['application/hal+json'])
};

/**
 * Registers plugin routes and an "api" object with the hapi server.
 * @param server
 * @param opts
 * @param next
 */
exports.register = function (server, opts, next) {
    var settings = opts;

    joi.validate(opts, optionsSchema, function (err, validated) {
        if (err) throw err;

        settings = validated;
    });
{'fraction_non_alphanumeric': 0.1869313482216708, 'fraction_numerical': 0.0016542597187758478, 'mean_word_length': 3.7236328125, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4687512', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1514, 'n_words': 387}