Last active
August 29, 2015 14:21
-
-
Save johnstoecker/cbe3e36d99ceab964c41 to your computer and use it in GitHub Desktop.
JS Snippet for creating a set and one item using the cerego.com API
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
var COURSE_NAME = "Jazz Musicians"; | |
var LANGUAGE_ID = 1819; //English, see https://api.cerego.com/v2/languages for list | |
ITEMS = [{ | |
anchor: { | |
text: "Jazz Trumpeter of 'What a Wonderful World' fame", | |
image_url: "http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Louis_Armstrong_restored.jpg/250px-Louis_Armstrong_restored.jpg" | |
}, | |
association: { | |
text: "Louis Armstrong" | |
} | |
}]; | |
(function() { | |
window.CeregoApi = {}; | |
CeregoApi._host = "http://api.cerego.com/v2/"; | |
//get your api key from https://cerego.com/configuration | |
CeregoApi._apiKey = "YOUR_API_KEY_HERE" | |
CeregoApi._successCallback = function(data) { | |
console.log(data); | |
}; | |
CeregoApi._errorCallback = function(method, endpoint, jqXHR) { | |
console.log("AJAX " + method + " to " + endpoint + " failed with status " + jqXHR.status + ": " + jqXHR.response); | |
}; | |
CeregoApi._ajax = function(method, endpoint, params, successCallback, errorCallback) { | |
successCallback = (typeof successCallback !== 'undefined') ? successCallback : CeregoApi._successCallback; | |
errorCallback = (typeof errorCallback !== 'undefined') ? errorCallback : CeregoApi._errorCallback; | |
var endpoint = CeregoApi._host + endpoint; | |
$.ajax({ | |
type: method, | |
dataType: 'json', | |
url: endpoint, | |
data: params, | |
headers: { | |
"Authorization": "Bearer " + CeregoApi._apiKey | |
} | |
}).done(function(data, textStatus, jqXHR) { | |
var response = data.response; | |
successCallback(response); | |
}).fail(function(jqXHR, textStatus, errorThrown) { | |
errorCallback(method, endpoint, jqXHR); | |
}); | |
}; | |
CeregoApi.apiKey = function(apiKey) { | |
CeregoApi._apiKey = apiKey; | |
}; | |
CeregoApi.getProfile = function(callback) { | |
CeregoApi._ajax("GET", "my/profile", null, callback); | |
}; | |
CeregoApi.createSet = function(params, callback) { | |
CeregoApi._ajax("POST", "sets", params, callback); | |
}; | |
// Result wrapped with {concept: {}} | |
CeregoApi.createSetConcept = function(set_id, params, callback) { | |
CeregoApi._ajax("POST", "sets/" + set_id + "/concepts", params, callback); | |
}; | |
CeregoApi.createSetItem = function(set_id, params, callback) { | |
CeregoApi._ajax("POST", "sets/" + set_id + "/items", params, callback); | |
}; | |
CeregoApi.createItemFacet = function(item_id, params, callback) { | |
CeregoApi._ajax("POST", "items/" + item_id + "/facets", params, callback); | |
}; | |
// Result wrapped with {image: {}} | |
CeregoApi.createImage = function(params, callback) { | |
CeregoApi._ajax("POST", "images", params, callback); | |
}; | |
// Expects params: {anchor: {text: "anchor1"}, {association: {text: "association1"}}} | |
CeregoApi.createQuickItemAnchorAssociation = function(set_id, params, callback) { | |
anchor = params.anchor; | |
association = params.association; | |
CeregoApi.createSetConcept(set_id, anchor, function(anchorConceptResult) { | |
var anchorConcept = anchorConceptResult.concept; | |
CeregoApi.createSetConcept(set_id, association, function(associationConceptResult) { | |
var associationConcept = associationConceptResult.concept; | |
CeregoApi.createSetItem(set_id, { | |
association_collection: { | |
concept_id: anchorConcept.id | |
} | |
}, function(item) { | |
CeregoApi.createItemFacet(item.id, { | |
set_id: set_id, | |
concept_id: associationConcept.id | |
}, callback); | |
}); | |
}); | |
}); | |
}; | |
})(); | |
function createCourse(){ | |
console.log("Creating course") | |
CeregoApi.createSet({ | |
name: COURSE_NAME, | |
language_id: LANGUAGE_ID | |
}, function(set) { | |
courseCreated(set); | |
}); | |
return false; | |
} | |
function courseCreated(set) { | |
studyPath = "https://cerego.com/sets/" + set.id; | |
console.log("Your course will be available at "+studyPath); | |
$.each(ITEMS, function(index, item) { | |
uploadItem(item, set) | |
}) | |
} | |
function uploadItem(item, set){ | |
var createItem = function(item) { | |
CeregoApi.createQuickItemAnchorAssociation(set.id, item, function() { | |
console.log("Created item") | |
}, function() { | |
console.log('Error creating item') | |
uploadItem(set); | |
}); | |
}; | |
if (item.anchor.image_url) { | |
CeregoApi.createImage({ | |
url: item.anchor.image_url | |
}, function(imageWrapper) { | |
var image = imageWrapper.image; | |
item.anchor.image_id = image.id; | |
createItem(item); | |
}); | |
} else { | |
createItem(item); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment