Last active
May 17, 2016 11:25
-
-
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)
This file contains 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
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