Created
August 16, 2014 00:07
-
-
Save shawn-simon/62e08ba6027ea64e153a to your computer and use it in GitHub Desktop.
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
;(function() { | |
function CommandIQ() { | |
// Constants / Configuration | |
var API_VERSION = 1 | |
// Libraries... need to move elsewhere | |
var Base64 = { | |
// private property | |
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", | |
// public method for encoding | |
encode : function (input) { | |
var output = ""; | |
var chr1, chr2, chr3, enc1, enc2, enc3, enc4; | |
var i = 0; | |
input = Base64._utf8_encode(input); | |
while (i < input.length) { | |
chr1 = input.charCodeAt(i++); | |
chr2 = input.charCodeAt(i++); | |
chr3 = input.charCodeAt(i++); | |
enc1 = chr1 >> 2; | |
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); | |
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); | |
enc4 = chr3 & 63; | |
if (isNaN(chr2)) { | |
enc3 = enc4 = 64; | |
} else if (isNaN(chr3)) { | |
enc4 = 64; | |
} | |
output = output + | |
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + | |
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); | |
} | |
return output; | |
}, | |
// private method for UTF-8 encoding | |
_utf8_encode : function (string) { | |
string = string.replace(/\r\n/g,"\n"); | |
var utftext = ""; | |
for (var n = 0; n < string.length; n++) { | |
var c = string.charCodeAt(n); | |
if (c < 128) { | |
utftext += String.fromCharCode(c); | |
} | |
else if((c > 127) && (c < 2048)) { | |
utftext += String.fromCharCode((c >> 6) | 192); | |
utftext += String.fromCharCode((c & 63) | 128); | |
} | |
else { | |
utftext += String.fromCharCode((c >> 12) | 224); | |
utftext += String.fromCharCode(((c >> 6) & 63) | 128); | |
utftext += String.fromCharCode((c & 63) | 128); | |
} | |
} | |
return utftext; | |
} | |
} | |
// Properties | |
var initialized = false | |
var options = { | |
api_version: null, | |
base_url: 'http://api.commandiq.com/', | |
endpoints: { | |
track_user: { | |
route: 'user/track' | |
}, | |
identify_user: { | |
route: 'user/identify' | |
} | |
}, | |
api_key: null, | |
} | |
// Internal Methods | |
var check_initialized = function() | |
{ | |
if (!initialized) throw "commandiq.js: Attempted to access API method prior to initialization."; | |
} | |
var data_encode = function(data) | |
{ | |
var json_string = JSON.stringify(data); | |
var base_64_encoded = Base64.encode(json_string); | |
return base_64_encoded; | |
} | |
var make_request = function(endpoint, data) | |
{ | |
var data = { | |
data: data_encode(data) | |
} | |
var request = { | |
url: options.base_url + options.endpoints[endpoint].route, | |
data: data, | |
success: request_callback, | |
error: request_callback, | |
dataType: 'json', | |
headers: { | |
'Authorization': 'Basic ' + Base64.encode(options.api_key + ':'), | |
'Accept': 'application/vnd.commandiq+json; version=' + API_VERSION.toString() + ';', | |
}, | |
cache: false | |
} | |
console.log('sending request', request) | |
$.ajax(request) | |
} | |
var request_callback = function(data) | |
{ | |
console.log('request complete', data, data.responseText) | |
} | |
// API Methods | |
this.init = function(opt) { | |
console.log('loading, passed options:', opt) | |
if (!window.jQuery) | |
{ | |
throw "commandiq.js: jQuery is required." | |
} | |
if (typeof opt == 'string') | |
{ | |
options.api_key = opt; | |
} | |
else | |
{ | |
$.extend(options, opt); | |
} | |
if (options.api_version != null && options.api_version !== API_VERSION) | |
{ | |
throw "commandiq.js: Mismatch between commandiq.js and site configuration version." | |
} | |
console.log("Inititalized with options", options) | |
initialized = true; | |
} | |
this.track_user = function(track_object) { | |
check_initialized() | |
make_request('track_user', track_object) | |
} | |
this.identify_user = function(identify_object) { | |
check_initialized() | |
make_request('identify_user', identify_object) | |
} | |
} | |
window.commandiq = new CommandIQ(); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment