/**
 * Define apemanId method for requests.
 * @function serverside.req.defineReqApemanId
 * @param {object} context - Server context object.
 * @param {string} context.APEMAN_ID_COOKIE_KEY - Cookie key of apeman id.
 * @returns {function} - Req apemanId function.
 * @author Taka Okunishi
 *
 */
module.exports = function (context) {
    context.assertKeys(['APEMAN_ID_COOKIE_KEY']);
    var Cookies = require("cookies"),
        cookieKey = context.APEMAN_ID_COOKIE_KEY;
    return  function () {
        var req = this;
        if (req._apemanIdCache) {
            return req._apemanIdCache;
        } else {
            req._apemanIdCache = new Cookies(req).get(cookieKey);
            return  req._apemanIdCache;
        }
    };
};