Created
January 7, 2017 02:36
-
-
Save orenmizr/c49995eb0908039123867cafb4e6e684 to your computer and use it in GitHub Desktop.
retrieveValue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var CONSTS = require("./components/consts"); | |
var _ = require("./components/utils"); | |
var method = "GET"; | |
var baseUrl; | |
var initialize = function(){ | |
baseUrl = buildEntrypoint(CONSTS.API.version,CONSTS.API.publisher.id); | |
}; | |
var xhr = function() { | |
var xhr = new XMLHttpRequest(); | |
return function(method, url, callback) { | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState === 4) { | |
if (typeof callback === "function") callback(xhr.responseText); | |
} | |
}; | |
xhr.open(method, url); | |
xhr.send(); | |
}; | |
}(); | |
var getRecommendations = function(cb, state){ | |
var requiredQueries = ["app.type","app.apikey", "rec.count", "source.type", "source.id"]; | |
var flatObj = _.flatten(state); | |
var url = buildEntrypoint(baseUrl, CONSTS.API.actions.get) + buildQueryStrings(requiredQueries, flatObj) + initPostfix(state); | |
return xhr(method, url, cb); | |
}; | |
var notifyClick = function(state, index){ | |
var requiredQueries = ["app.type", "app.apikey", "response.id", "response.session", "item.type", "item.id"]; | |
var item = state.data.list[index]; | |
var flatObj = _.extend(_.flatten(state), _.flatten({item: item})); | |
var url = buildEntrypoint(baseUrl, CONSTS.API.actions.click) + buildQueryStrings(requiredQueries, flatObj); | |
return xhr(method, url); | |
}; | |
var notifyVisible = function(state){ | |
var requiredQueries = ["app.type", "app.apikey", "response.id", "response.session"]; | |
var flatObj = _.flatten(state); | |
var url = buildEntrypoint(baseUrl, CONSTS.API.actions.visible) + buildQueryStrings(requiredQueries, flatObj); | |
return xhr(method, url); | |
}; | |
// HELPERS | |
function buildEntrypoint() { | |
return Array.prototype.slice.call(arguments).join('/'); | |
} | |
function retreiveValue(keys,obj){ | |
var val = obj; | |
if(keys.indexOf(".") !== -1) { | |
var subkeys = keys.split("."); | |
for(var i=0 ; i<subkeys.length; i++) { | |
val = val[subkeys[i]]; | |
} | |
return val; | |
} | |
else { | |
return obj[keys]; | |
} | |
} | |
function buildQueryStrings(keys, obj){ | |
var queries = []; | |
keys.forEach(function(key){ | |
queries.push(key + "=" + encodeURIComponent(obj[key])); | |
}); | |
return "?" + queries.join("&"); | |
} | |
function initPostfix(state) { | |
return state.session === "init" ? "&user.session=init" : ""; | |
} | |
module.exports = { | |
initialize: initialize, | |
getRecommendations: getRecommendations, | |
notifyClick: notifyClick, | |
notifyVisible: notifyVisible | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment