Skip to content

Instantly share code, notes, and snippets.

@carsonwright
Created June 12, 2015 17:22
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 carsonwright/38d1462c02f50c9eb127 to your computer and use it in GitHub Desktop.
Save carsonwright/38d1462c02f50c9eb127 to your computer and use it in GitHub Desktop.
var ApiClient, SimplePromise, Traitify, console;
if (!Array.prototype.map) {
Array.prototype.map = function(callback, thisArg) {
var A, O, T, k, kValue, len, mappedValue;
T = void 0;
A = void 0;
k = void 0;
if (typeof this === "undefined" || this === null) {
throw new TypeError(" this is null or not defined");
}
O = Object(this);
len = O.length >>> 0;
if (typeof callback !== "function") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {
T = thisArg;
}
A = new Array(len);
k = 0;
while (k < len) {
kValue = void 0;
mappedValue = void 0;
if (k in O) {
kValue = O[k];
mappedValue = callback.call(T, kValue, k, O);
A[k] = mappedValue;
}
k++;
}
return A;
};
}
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun) {
"use strict";
var i, len, res, t, thisp, val;
if (this === void 0 || this === null) {
throw new TypeError();
}
t = Object(this);
len = t.length >>> 0;
if (typeof fun !== "function") {
throw new TypeError();
}
res = [];
thisp = arguments[1];
i = 0;
while (i < len) {
if (i in t) {
val = t[i];
if (fun.call(thisp, val, i, t)) {
res.push(val);
}
}
i++;
}
return res;
};
}
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt) {
var from, len;
len = this.length >>> 0;
from = Number(arguments[1]) || 0;
from = (from < 0 ? Math.ceil(from) : Math.floor(from));
if (from < 0) {
from += len;
}
while (from < len) {
if (from in this && this[from] === elt) {
return from;
}
from++;
}
return -1;
};
}
if (!console) {
console = {
log: function() {}
};
}
if (!Object.keys) {
Object.keys = (function() {
"use strict";
var dontEnums, dontEnumsLength, hasDontEnumBug, hasOwnProperty;
hasOwnProperty = Object.prototype.hasOwnProperty;
hasDontEnumBug = !{
toString: null
}.propertyIsEnumerable("toString");
dontEnums = ["toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "constructor"];
dontEnumsLength = dontEnums.length;
return function(obj) {
var i, prop, result;
if (typeof obj !== "object" && (typeof obj !== "function" || obj === null)) {
throw new TypeError("Object.keys called on non-object");
}
result = [];
prop = void 0;
i = void 0;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
i = 0;
while (i < dontEnumsLength) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
i++;
}
}
return result;
};
})();
}
SimplePromise = function(callback) {
var localPromise;
localPromise = Object();
localPromise.then = function(callback) {
localPromise.thenCallback = callback;
if (localPromise.resolved) {
localPromise.thenCallback(localPromise.data);
}
return localPromise;
};
localPromise.resolved = false;
localPromise.resolve = function(data) {
localPromise.data = data;
if (localPromise.thenCallback) {
localPromise.thenCallback(data);
} else {
localPromise.resolved = true;
}
return localPromise;
};
localPromise["catch"] = function(callback) {
if (localPromise.rejected) {
callback(localPromise.error);
localPromise;
} else {
localPromise.rejectCallback = callback;
}
return localPromise;
};
localPromise.rejected = false;
localPromise.reject = function(error) {
localPromise.error = error;
if (localPromise.rejectCallback) {
localPromise.rejectCallback(error);
} else {
localPromise.rejected = true;
}
return localPromise;
};
callback(localPromise.resolve, localPromise.reject);
return localPromise;
};
ApiClient = (function() {
function ApiClient() {
this.host = "https://api.traitify.com";
this.version = "v1";
if (typeof XDomainRequest !== "undefined") {
this.oldIE = true;
} else {
this.oldIE = false;
}
this.beautify = false;
this.XHR = XMLHttpRequest;
this;
}
ApiClient.prototype.online = function() {
return navigator.onLine;
};
ApiClient.prototype.setBeautify = function(mode) {
this.beautify = mode;
return this;
};
ApiClient.prototype.setHost = function(host) {
if (!host.match(/http/)) {
host = "https://" + host;
}
if (this.oldIE) {
host = host.replace("https://", "").replace("http://", "");
host = location.protocol + "//" + host;
}
this.host = host;
return this;
};
ApiClient.prototype.setPublicKey = function(key) {
this.publicKey = key;
return this;
};
ApiClient.prototype.setVersion = function(version) {
this.version = version;
return this;
};
ApiClient.prototype.ajax = function(method, path, callback, params) {
var beautify, oldIE, online, promise, that, time, url, xhr;
beautify = this.beautify;
url = this.host + "/" + this.version + path;
xhr = new this.XHR();
if ("withCredentials" in xhr && !this.oldIE) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest !== "undefined") {
if (this.oldIE) {
time = (new Date).getTime();
if (url.indexOf("?") === -1) {
url += "?authorization=" + this.publicKey + "&reset_cache=" + time;
} else {
url += "&authorization=" + this.publicKey + "&reset_cache=" + time;
}
}
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
return new SimplePromise(function(resolve, reject) {
return reject("CORS is Not Supported By This Browser");
});
}
xhr;
if (xhr && !this.oldIE) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(this.publicKey + ":x"));
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
}
that = this;
online = this.online();
oldIE = this.oldIE;
promise = new SimplePromise(function(resolve, reject) {
var error;
that.reject = reject;
if (!online) {
return that.reject();
}
try {
xhr.onload = function() {
var data;
if (xhr.status === 404) {
return that.reject(xhr.response);
} else {
if (oldIE) {
data = xhr.responseText;
} else {
data = xhr.response;
}
if (beautify) {
data = data.replace(/_([a-z])/g, function(m, w) {
return w.toUpperCase();
}).replace(/_/g, "");
}
data = JSON.parse(data);
if (callback) {
callback(data);
}
that.resolve = resolve;
return that.resolve(data);
}
};
xhr.onprogress = function() {};
xhr.ontimeout = function() {};
xhr.onerror = function() {};
window.setTimeout(function() {
var error;
try {
return xhr.send(JSON.stringify(params));
} catch (_error) {
error = _error;
return that.reject(error);
}
}, 0);
return xhr;
} catch (_error) {
error = _error;
return that.reject(error);
}
});
return promise;
};
ApiClient.prototype.put = function(path, params, callback) {
if (this.oldIE) {
return this.ajax("POST", path, callback, params);
} else {
return this.ajax("PUT", path, callback, params);
}
};
ApiClient.prototype.get = function(path, callback) {
return this.ajax("GET", path, callback, "");
};
ApiClient.prototype.getDecks = function(callback) {
return this.get("/decks", callback);
};
ApiClient.prototype.getSlides = function(assessmentId, callback) {
return this.get("/assessments/" + assessmentId + "/slides", callback);
};
ApiClient.prototype.addSlide = function(assessmentId, slideId, value, timeTaken, callback) {
return this.put("/assessments/" + assessmentId + "/slides/" + slideId, {
"response": value,
"time_taken": timeTaken
}, callback);
};
ApiClient.prototype.addSlides = function(assessmentId, values, callback) {
return this.put("/assessments/" + assessmentId + "/slides", values, callback);
};
ApiClient.prototype.getPersonalityTypes = function(id, options, callback) {
var j, key, len1, params, ref;
if (options == null) {
options = Object();
}
if (options.image_pack == null) {
options.image_pack = "linear";
}
params = Array();
ref = Object.keys(options);
for (j = 0, len1 = ref.length; j < len1; j++) {
key = ref[j];
params.push(key + "=" + options[key]);
}
return this.get("/assessments/" + id + "/personality_types?" + (params.join("&")), callback);
};
ApiClient.prototype.getPersonalityTraits = function(id, options, callback) {
return this.get("/assessments/" + id + "/personality_traits/raw", callback);
};
ApiClient.prototype.getCareers = function(id, options, callback) {
var j, key, len1, params, ref;
if (options == null) {
options = Object();
}
if (options.number_of_matches == null) {
options.number_of_matches = 8;
}
params = Array();
ref = Object.keys(options);
for (j = 0, len1 = ref.length; j < len1; j++) {
key = ref[j];
params.push(key + "=" + options[key]);
}
return this.get("/assessments/" + id + "/matches/careers?" + (params.join("&")), callback);
};
return ApiClient;
})();
Traitify = new ApiClient();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment