Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Last active May 17, 2016 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germanviscuso/377dac1ff30b8238e3f4cc12f668426b to your computer and use it in GitHub Desktop.
Save germanviscuso/377dac1ff30b8238e3f4cc12f668426b to your computer and use it in GitHub Desktop.
Function for Kii server extension to send events to InitialState.com (Note: object must have flat properties, no nesting supported)
function sendInitialState(accessKey, bucketKey, kiiObject, workerCallback, doneCallback) {
var targetUrl = "https://groker.initialstate.com/api/events";
var customInfo;
$.each(kiiObject, function(key, val){
if(key == "_customInfo")
customInfo = val;
});
if (!customInfo) {
var error = "Error: can't retrieve thing custom data for visualization";
console.log(error);
if (doneCallback) doneCallback(error);
} else {
var normalizedArray = Object.keys(customInfo).map(function(key){return {'key':key, 'value':customInfo[key]}});
// Send visualization data
$.ajax({
url: targetUrl,
headers: {
'X-IS-AccessKey':accessKey,
'X-IS-BucketKey':bucketKey
},
type: "POST",
data: JSON.stringify(normalizedArray),
contentType: "application/json",
success: function (body) {
console.log("OK posting, got: " + JSON.stringify(body));
if (workerCallback) workerCallback(thing);
else if (doneCallback) doneCallback("SUCCESS!");
},
error: function (msg) {
console.log("Error posting:" + JSON.stringify(msg));
if (doneCallback) doneCallback(error);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment