-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (73 loc) · 2.11 KB
/
index.js
File metadata and controls
87 lines (73 loc) · 2.11 KB
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
'use strict';
var rest = require('restler');
var validUrl = require('valid-url');
var parseString = require('xml2js').parseString;
var achecker = function(options, callback) {
if (!options.uri) {
return callback(new Error('Missing required param: uri'), null);
}
if (options.uri && !validUrl.isWebUri(options.uri)) {
return callback(new Error('Invalid url'), null);
}
if (!options.key) {
return callback(new Error('Web Service ID is required'), null);
}
var params = {
query: {
uri: options.uri,
id: options.key,
output: 'rest',
guide: options.guide || 'WCAG2-AA',
offset: options.offset || 0
},
headers: {
'Content-Type': 'application/json',
'Host': 'achecker.ca',
'X-Target-URI': 'http://achecker.ca/'
}
};
rest.get('http://achecker.ca/checkacc.php', params)
.on('401', function(){
callback('Empty URI', null);
})
.on('402', function(){
callback('Invalid URI', null);
})
.on('403', function(){
callback('Empty Web Service ID', null);
})
.on('404', function(){
callback('Invalid Web Service ID', null);
})
.on('405', function(){
callback('No sequence id is given', null);
})
.on('complete', function(result) {
parseString(result,{
trim: true,
normalize: true,
explicitArray: false
},
function (err, json) {
var summary = {
sessionID: json.resultset.summary.sessionID,
errors: parseInt(json.resultset.summary.NumOfErrors),
likelyProblems: parseInt(json.resultset.summary.NumOfLikelyProblems),
potentialProblems: parseInt(json.resultset.summary.NumOfPotentialProblems)
};
var results = json.resultset.results.result || {};
results.forEach(function(item, i, r){
if(item.errorMsg){
var url = item.errorMsg.match(/href="([^"]*)"/);
var text = item.errorMsg.match(/">([^"]*)<\/a>/);
r[i].errorMsg = {url: url[1], message: text[1]};
}
});
callback( {
summary : summary,
results: json.resultset.results.result
});
});
});
};
module.exports = achecker;