Skip to content

Instantly share code, notes, and snippets.

@byronferguson
Last active September 4, 2015 19:46
Show Gist options
  • Save byronferguson/2384b4dcd00fef170c7f to your computer and use it in GitHub Desktop.
Save byronferguson/2384b4dcd00fef170c7f to your computer and use it in GitHub Desktop.
Work in progress: attempting to create a simple REST interface object
(function(global, $) {
var ccREST = function(studentID, institutionID, clusterID) {
return new ccREST.init(studentID, institutionID, clusterID);
};
var BASE_API_URL = '/api/v1/index.cfm';
var createResponsePackage = function(clusterID) {
clusterID = clusterID || 0;
return { responses: [], clusterID: clusterID };
};
var deleteQuestionURL = function(studentID, questionID) {
if(!questionID) {
throw 'QuestionID is required!';
}
return BASE_API_URL + '/students/id/' + studentID + '/responses?questionID=' + questionID;
};
var deleteOptionURL = function(studentID, optionID) {
if(!optionID) {
throw 'OptionID is required!';
}
return BASE_API_URL + '/students/id/' + studentID + '/responses?optionID=' + optionID;
};
var postResponseURL = function(studentID) {
return BASE_API_URL + '/students/id/' + studentID + '/responses';
};
var ajaxErrorOutput = function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
};
ccREST.prototype = {
resetResponsePackage: function() {
this.responseObject.responses.length = 0;
return this;
},
addResponse: function(optionID, responseValue) {
responseValue = (responseValue !== undefined) ? responseValue : 1;
this.responseObject.responses.push( { optionID: optionID, responseValue: responseValue } );
return this;
},
submitResponses: function() {
var self = this;
$.ajax({
url : postResponseURL(this.studentID),
method : 'POST',
dataType : 'json',
data : JSON.stringify(this.responseObject),
contentType : "application/json; charset=utf-8",
})
.fail(function(jqXHR, textStatus, errorThrown) {
ajaxErrorOutput(jqXHR, textStatus, errorThrown);
})
.done(function(data, status) {
self.resetResponsePackage();
});
return this;
},
replaceResponses: function(questionID) {
var self = this;
$.ajax({
url : deleteQuestionURL(this.studentID, questionID),
method : 'DELETE',
dataType : 'text',
error : this.ajaxErrorOutput
})
.fail(function(jqXHR, textStatus, errorThrown) {
ajaxErrorOutput(jqXHR, textStatus, errorThrown);
})
.done(function(data, textStatus, jqXHR) {
self.submitResponses();
});
return this;
},
updateResponse: function() {
this.submitResponses();
},
removeResponse: function(optionID) {
$.ajax({
url : deleteOptionURL(this.studentID, optionID),
method : 'DELETE',
dataType : 'text',
data : {},
success : function(data, status) { },
error : this.ajaxErrorOutput
});
return this;
}
};
ccREST.init = function(studentID, institutionID, clusterID) {
var self = this;
self.studentID = studentID || 459;
self.institutionID = institutionID || 0;
self.clusterID = clusterID || 0;
self.responseObject = createResponsePackage(self.clusterID);
};
ccREST.init.prototype = ccREST.prototype;
global.ccREST = ccREST;
}(window, jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment