<commit_before>/**
 * Copyright (c) 2011-2013, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */


/*jslint node:true, nomen:true*/

/**
@module mojito-contextualizer
**/

'use strict';

var debug = require('debug')('mojito:middleware:contextualizer'),
    // TODO: review qs vs querystring performance
    qs = require('querystring'),
    url = require('url'),
    CONTEXT_SERVER = 'server',
    DEFAULT_LANG = 'en',
    OPERA_MINI = 'opera-mini',
    IPHONE = 'iphone',
    IPAD = 'ipad',
    ANDROID = 'android',
    IE_MOBILE = 'iemobile',
    PALM = 'palm',
    KINDLE = 'kindle',
    BLACKBERRY = 'blackberry',
    // _device
    REGEX_OPERA_MINI = /opera mini/i,
    REGEX_IPHONE = /ipod|iphone/i,
    REGEX_IPAD = /ipad/i,
    REGEX_ANDROID = /android/i,
    REGEX_IE_MOBILE = /iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile/i,
    REGEX_PALM = /pre\/|palm os|palm|hiptop|avantgo|fennec|plucker|xiino|blazer|elaine/i,
    REGEX_KINDLE = /kindle/i,
    REGEX_BLACKBERRY = /blackberry/i,
    // _language
    REGEX_ACCEPT_LANGUAGE = / *, */,
    REGEX_LANGUAGE_MATCH = /^([a-z]+)-([a-z]+)$/;

/**
The request contextualizer. Middleware which adds context to a request.
@constructor
**/
function RequestContextualizer() {
}


RequestContextualizer.prototype = {

    /**
    @method handle
    @public
    @return {Function} express middleware
    **/
    handle: function () {

        var my = this,
            mojito,
            defaultLang;

        return function (req, res, next) {


            if (!defaultLang && req.app && req.app.mojito) {
                mojito = req.app.mojito;
                defaultLang = (mojito.context && mojito.context.lang) ||
                                DEFAULT_LANG;
            }

            var query = url.parse(req.url, true).query || {};

            if (!req.context) {
                req.context = {};
            }

            req.context.runtime = CONTEXT_SERVER;
            req.context.site = query.site || '';
            // TODO: [Issue 86] add configuration switch to detect device
            req.context.device = query.device ||
                my._device(req.headers['user-agent'], '');
            req.context.lang = query.lang ||
                my._language(req.headers['accept-language'], defaultLang);
            req.context.langs = query.langs ||
                my._preferredLanguages(req.context.lang, defaultLang);
            req.context.region = query.region || '';
            req.context.jurisdiction = query.jurisdiction || '';
            req.context.bucket = query.bucket || '';
            req.context.flavor = query.flavor || '';
            req.context.tz = query.tz || '';
            req.context.environment = query.environment || req.context.environment;

            // debug('detected lang: ' + req.context.lang, 'debug',
            //    'request-contextualizer');
            // debug('detected device: ' + req.context.device, 'debug',
            //    'request-contextualizer');
            next();
        };
    },

    /**
    @method _device
    @protected
    **/
    _device: function (ua, def) {
        // debug('detecting device from UA: ' + ua);

        // TODO: [Issue 74] Remove regex creation within this function scope,
        // and eventually offload to device catalog
        if (REGEX_OPERA_MINI.test(ua)) {
            return OPERA_MINI;
        }
        if (REGEX_IPHONE.test(ua)) {
            return IPHONE;
        }
        if (REGEX_IPAD.test(ua)) {
            return IPAD;
        }
        if (REGEX_ANDROID.test(ua)) {<commit_msg>Updated contextualizer for trib app.<commit_after>/**
 * Copyright (c) 2011-2013, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */


/*jslint node:true, nomen:true*/

/**
@module mojito-contextualizer
**/

'use strict';

var debug = require('debug')('mojito:middleware:contextualizer'),
    // TODO: review qs vs querystring performance
    qs = require('querystring'),
    url = require('url'),
    CONTEXT_SERVER = 'server',
    DEFAULT_LANG = 'en',
    OPERA_MINI = 'opera-mini',
    IPHONE = 'iphone',
    IPAD = 'ipad',
    ANDROID = 'android',
    IE_MOBILE = 'iemobile',
    PALM = 'palm',
    KINDLE = 'kindle',
    BLACKBERRY = 'blackberry',
    // _device
    REGEX_OPERA_MINI = /opera mini/i,
    REGEX_IPHONE = /ipod|iphone/i,
    REGEX_IPAD = /ipad/i,
    REGEX_ANDROID = /android/i,
    REGEX_IE_MOBILE = /iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile/i,
    REGEX_PALM = /pre\/|palm os|palm|hiptop|avantgo|fennec|plucker|xiino|blazer|elaine/i,
    REGEX_KINDLE = /kindle/i,
    REGEX_BLACKBERRY = /blackberry/i,
    // _language
    REGEX_ACCEPT_LANGUAGE = / *, */,
    REGEX_LANGUAGE_MATCH = /^([a-z]+)-([a-z]+)$/;

/**
The request contextualizer. Middleware which adds context to a request.
@constructor
**/
function RequestContextualizer() {
}


RequestContextualizer.prototype = {

    /**
    @method handle
    @public
    @return {Function} express middleware
    **/
    handle: function () {

        var my = this,
            mojito,
            defaultLang;

        return function (req, res, next) {


            if (!defaultLang && req.app && req.app.mojito) {
                mojito = req.app.mojito;
                defaultLang = (mojito.context && mojito.context.lang) ||
                                DEFAULT_LANG;
            }

            var query = url.parse(req.url, true).query || {};

            if (!req.context) {
                req.context = {};
            }

            req.context.runtime = CONTEXT_SERVER;
            req.context.site = query.site || '';
            // TODO: [Issue 86] add configuration switch to detect device
            req.context.device = query.device ||
                my._device(req.headers['user-agent'], '');
            req.context.lang = query.lang ||
                my._language(req.headers['accept-language'], defaultLang);
            req.context.langs = query.langs ||
                my._preferredLanguages(req.context.lang, defaultLang);
            req.context.region = query.region || '';
            req.context.jurisdiction = query.jurisdiction || '';
            req.context.bucket = query.bucket || '';
            req.context.flavor = query.flavor || '';
            req.context.tz = query.tz || '';

            // debug('detected lang: ' + req.context.lang, 'debug',
            //    'request-contextualizer');
            // debug('detected device: ' + req.context.device, 'debug',
            //    'request-contextualizer');
            next();
        };
    },

    /**
    @method _device
    @protected
    **/
    _device: function (ua, def) {
        // debug('detecting device from UA: ' + ua);

        // TODO: [Issue 74] Remove regex creation within this function scope,
        // and eventually offload to device catalog
        if (REGEX_OPERA_MINI.test(ua)) {
            return OPERA_MINI;
        }
        if (REGEX_IPHONE.test(ua)) {
            return IPHONE;
        }
        if (REGEX_IPAD.test(ua)) {
            return IPAD;
        }
        if (REGEX_ANDROID.test(ua)) {
{'fraction_non_alphanumeric': 0.1375530894643102, 'fraction_numerical': 0.0035621317988765583, 'mean_word_length': 2.021523178807947, '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': '7476480', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2281, 'n_words': 465}