-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.coffee
96 lines (86 loc) · 3.09 KB
/
main.coffee
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
91
92
93
94
95
96
fs = require 'fs'
path = require 'path'
io = require('socket.io').listen(4563)
request = require 'request'
_ = require 'underscore'
languages = require 'languages'
getLanguage = (languageCode) ->
if languageCode == 'en-US'
'English'
else if languages.isValid(languageCode)
languages.getLanguageInfo(languageCode).name
else
'<em>Unknown language</em>'
requestUrl = (webPageUrl) -> 'http://accessibility.egovmon.no/en/pagecheck2.0/?url=' + encodeURIComponent(webPageUrl) + '&export=json'
transformResults = (results) ->
_.reject(_.map(results, (result) -> transformResult(result)), (result) -> _.isEmpty(result))
transformResult = (checkerResult) ->
result = {
line: checkerResult.line
column: checkerResult.column
testId: checkerResult.testId
testResultId: checkerResult.testResultId.split('-')[2]
testTitle: checkerResult.testTitle
category: checkerResult.category
}
if result.category == 'verify'
transformVerifyResult checkerResult, result
else
result
transformVerifyResult = (checkerResult, result) ->
switch checkerResult.testResultId
when 'SC2.4.2-1-11'
_.extend result, {
questionValues: [checkerResult.details.pageTitle]
answers: ['yes', 'no']
}
when 'SC2.4.4-2-11'
_.extend result, {
questionValues: [checkerResult.details.codeExtract]
answers: ['yes', 'no', 'unsure']
}
when 'SC2.4.4-2-12'
_.extend result, {
questionValues: [
checkerResult.details.title,
checkerResult.details.codeExtract
]
answers: ['yes', 'no', 'unsure']
}
when 'SC2.4.6-1-11'
_.extend result, {
questionValues: [checkerResult.details.heading]
answers: ['yes', 'no', 'unsure']
}
when 'SC3.1.2-2-11'
languageCode = checkerResult.details.languageDefinition.languageCode
_.extend result, {
questionValues: [
checkerResult.details.checkedText,
getLanguage(languageCode),
languageCode
]
answers: ['yes', 'no', 'unsure', 'i_dont_speak_this_language']
}
else
console.log 'Not supported: ' + checkerResult.testResultId
{}
io.sockets.on 'connection', (socket) ->
socket.on 'get tests', (data) ->
request(requestUrl(data), (error, result, body) ->
console.log 'error=' + error
console.log 'result=' + result
console.log 'body=' + body
if result.body.substring(0, 19) == 'An error occurred: '
console.log 'Error when requesting ' + data + ': ' + result.body.substring(19)
socket.emit 'tests', null
else
socket.emit 'tests', transformResults(JSON.parse(body))
)
socket.on 'get locale', (locale) ->
if locale isnt 'no' and locale isnt 'en'
return
fs.readFile path.resolve(__dirname, 'locale/', locale + '.json'), 'utf8', (err, data) ->
if err
throw err
socket.emit 'locale', { locale: locale, data: JSON.parse(data)[locale] }