-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (76 loc) · 2.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const querystring = require('querystring');
const http = require('http');
const BASE_URL = "http://evaluacion.ucuenca.edu.ec/ucuenca-rest-ws/api/v1/"
class Ucuenca {
careers(object, callback) {
const params = {
idEstudiante: object['studentId']
}
const response = this.get('registroacademico', params, callback);
}
schedule(object, callback) {
const params = {
idEstudiante: object['studentId']
}
const response = this.get('horarios', params, callback);
}
notes(object, callback){
const params = {
idEstudiante: object['studentId'],
idCarrera: object['careerId'],
idPerlec: object['periodId']
}
return this.get('registroacademico/notas', params, callback);
}
curriculum_progress(object, callback){
const params={
idEstudiante: object['studentId'],
idCarrera: object['careerId'],
idMalla: object['curriculumId'],
idPlacar: object['careerPlan']
}
return this.get('registroacademico/malla', params, callback);
}
authentication(object, callback){
const params={
usuario: object['user'],
pass: object['passw']
}
return this.get('acceso', params, callback);
}
get(service_name, params, callback){
const url = this.get_url(service_name, params);
const response = this.request(url, callback);
}
get_url(service_name, params){
return `${BASE_URL}${service_name}?${querystring.stringify(params)}`;
}
request(url, callback){
callback = callback || function(){};
http.get(url, (res) => {
const { statusCode } = res;
let error;
if(statusCode !== 200){
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
callback(error, null)
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chuck) => { rawData += chuck; });
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData);
callback(error, parsedData);
} catch (e) {
callback(e, null);
}
}).on('error', (e) => {
callback(e, null);
});
});
}
}
module.exports = Ucuenca;