streamer-client
Version:
A node.js client for the Duke OIT Streamer API
154 lines (138 loc) • 4.52 kB
JavaScript
var util = require('../util');
var path = require('path');
/**
* Access to the Curriculum resource
* @class
* @param {Dispatcher} dispatcher The API dispatcher
*/
function Curriculum(dispatcher) {
this.dispatcher = dispatcher;
}
Curriculum.BASE_PATH = '/curriculum';
/* Private API */
Curriculum.prototype._fieldName = function(fieldname) {
return this.dispatcher.get(path.join(Curriculum.BASE_PATH, 'list_of_values',
'fieldname', fieldname)).then(function(payload) {
return payload.scc_lov_resp.lovs.lov.values.value;
});
};
Curriculum.prototype._rawClassInfo = function(term_id, course_id) {
var url_path = path.join(Curriculum.BASE_PATH, 'classes', 'strm', term_id,
'crse_id', course_id);
return this.dispatcher.get(url_path);
};
/* Public API */
/**
* Returns all of the terms
* @return {Promise} Result of API call
*/
Curriculum.prototype.rawTerms = function() {
return this._fieldName('STRM');
};
/**
* Returns all of the terms, but converts code to int
* @return {Promise} Result of API call
*/
Curriculum.prototype.terms = function() {
return this.rawTerms().then(function(terms) {
return terms.map(function(term) {
return util.keyMap(term, ['code'], parseInt);
});
});
};
/**
* Returns all of the subjects
* @return {Promise} Result of API call
*/
Curriculum.prototype.subjects = function() {
return this._fieldName('SUBJECT');
};
/**
* Returns the courses for the given subject code
* Strips whitespace from catalog_nbr
* @param {String} subject_code The subject code
* @return {Promise} The result of the API call
*/
Curriculum.prototype.rawCourses = function(subject_code) {
return this.dispatcher.get(
path.join(Curriculum.BASE_PATH, 'courses', 'subject', encodeURIComponent(subject_code)))
.then(function(payload) {
var subject = payload.ssr_get_courses_resp.course_search_result.subjects.subject;
if (subject.subject_crs_count === '0') {
return [];
}
return payload.ssr_get_courses_resp.course_search_result
.subjects.subject.course_summaries.course_summary;
});
};
/**
* Returns the courses for the given subject code, but
* Converts crse_offer_nbr and crs_topic_id to numbers
* Strips whitespace from catalog_nbr
* @param {String} subject_code The subject code
* @return {Promise} The result of the API call
*/
Curriculum.prototype.courses = function(subject_code) {
return this.rawCourses(subject_code).then(function(courses) {
if (courses.length) {
return courses.map(function(course) {
return util.keyMap(course, ['crse_offer_nbr', 'crs_topic_id'], parseInt);
});
} else {
return [];
}
});
};
/**
* Returns the classes for the given term and course_id
* @param {String} term_id The term id
* @param {String} subject_id The suject id
* @param {String} course_id The course id
* @return {Promise} The result of the API call
*/
Curriculum.prototype.classes = function(term_id, subject_id, course_id) {
return this._rawClassInfo(term_id, course_id).then(function(payload) {
var subjects = payload.ssr_get_classes_resp.search_result.subjects;
if (subjects) {
var subject = subjects.subject;
if (Array.isArray(subject)) {
subject = subject.filter(function(subject_el) {
return subject_el.subject === subject_id })[0];
}
if (subject) {
var classes = subject.classes_summary.class_summary;
if (!Array.isArray(classes)){
classes = [classes];
}
return classes;
}
}
return [];
});
};
/**
* Returns true if the course if offered during the term
* @param {String} term_id The term id
* @param {String} subject_id The suject id
* @param {String} course_id The course id
* @return {Promise} The result of the API call
*/
Curriculum.prototype.isCourseOffered = function(term_id, subject_id, course_id) {
return this._rawClassInfo(term_id, course_id).then(function(payload) {
var search_result = payload.ssr_get_classes_resp.search_result;
if(search_result.ssr_course_count == '0') {
return false;
}
var subjects = search_result.subjects;
var subject = subjects.subject;
if (Array.isArray(subject)) {
subject = subject.filter(function(subject_el) {
return subject_el.subject === subject_id })[0];
}
return {
course: course_id,
is_offered: subject.subject === subject_id
};
});
};
module.exports = Curriculum;