All files / universal.klown/gpii/node_modules/matchMakerFramework/src MatchMakerFramework.js

95.83% Statements 23/24
75% Branches 6/8
83.33% Functions 5/6
95.83% Lines 23/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                    1x 1x 1x   1x   1x                                                                       1x 138x   138x 138x 138x 138x       138x 138x 138x 138x       138x                         1x 140x   1961x 10349x         515x     1961x 1446x        
/*
* Match Maker Framework
*
* Copyright 2014 Raising the Floor - International
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* The research leading to these results has received funding from the European Union's
* Seventh Framework Programme (FP7/2007-2013)
* under grant agreement no. 289016.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
 
"use strict";
 
var fluid = require("infusion"),
    gpii = fluid.registerNamespace("gpii"),
    semver = require("semver");
 
fluid.registerNamespace("gpii.matchMakerFramework");
 
fluid.defaults("gpii.matchMakerFramework", {
    gradeNames: ["fluid.component"],
    matchMakerUrl: "%matchMakerPath/match",
    components: {
        matchMakerService: {
            type: "kettle.dataSource.URL",
            options: {
                url: "{gpii.matchMakerFramework}.options.matchMakerUrl",
                writable: true,
                termMap: {
                    matchMakerPath: "noencode:%matchMakerPath"
                }
            }
        }
    },
    invokers: {
        matchMakerDispatcher: {
            funcName: "gpii.matchMakerFramework.matchMakerDispatcher",
            args: ["{that}", "{arguments}.0"]
        }
    },
    events: {
        onSolutions: null
    }
});
 
/**
 * Asynchronous function responsible for, given a matchMaker input payload, deciding which matchmaker is most
 * suitable to do the actual matching process and send the payload there. Once the matchmaker
 * returns its matched data, the event will be fired with both the returned and original data in a combined payload.
 *
 * @param that {Object} - object containing a matchMakerService datasource component which can be used
 *     when sending the data to the actual match maker
 * @param matchMakerInput {Object} - a valid input payload for the matchmakers
 * @param event {Object} - event to fire when the matchmaker has finished its matching
 */
gpii.matchMakerFramework.matchMakerDispatcher = function (that, matchMakerInput) {
    var matchMakerResponse = fluid.extend({}, matchMakerInput);
    // TODO: some algorithm to decide the MM
    var selectedMatchMaker = "default";
    var matchMakerPath = that.options.matchMakers[selectedMatchMaker].url;
    fluid.log("MatchMaker Framework: dispatching to the " + selectedMatchMaker + " MatchMaker at path " + matchMakerPath);
    var promise = that.matchMakerService.set({
        matchMakerPath: matchMakerPath
    }, matchMakerInput, { writeMethod: "POST" });
 
    var outputPromise = fluid.promise();
    promise.then(function (matchMakerOutput) {
        matchMakerResponse.matchMakerOutput = matchMakerOutput;
        outputPromise.resolve(matchMakerResponse);
    }, function (err) { // TODO: This rejection handler is untested
        outputPromise.reject(err);
    });
    return outputPromise;
};
 
/**
 * Takes a solutions registry object and filters out all solutions that do not match the ones
 * reported by the device reporter.
 *
 * @param solutions {Object} - solutions registry entries as retrieved from solutions registry
 * @param device {Object} - device reporter data as retrieved from the device reporter
 *
 * @return {Object} - the solutions registry object, but with all the solutions not matching
 *      the device filtered out
 */
gpii.matchMakerFramework.filterSolutions = function (solutions, device) {
    return fluid.remove_if(fluid.copy(solutions), function (solution, solutionId) {
        // Match on device solutions.
        var matchesSolutions = fluid.find(device.solutions, function (devSolution) {
            if (devSolution.id === solutionId &&
                (!solution.version ||
                 !devSolution.version ||
                 semver.satisfies(devSolution.version, solution.version)
                )) {
                return true;
            }
        });
        if (!matchesSolutions) {
            return solutions;
        }
    });
};