Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Added final data set, subject-area, to 'gymsar' level, completing it'…
Browse files Browse the repository at this point in the history
…s implementation! 🎉
  • Loading branch information
themaxsandelin committed Oct 15, 2018
1 parent 80742c9 commit faf9cab
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/controllers/gymsar/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fs = require('fs')
const { parseString } = require('xml2js')

// Helpers
const formatter = require('../../helpers/gym/formatter')
const formatter = require('../../helpers/gymsar/formatter')
const latinize = require('../../helpers/latinize')

const rootPath = path.resolve(__dirname + '/../../../files/gys')
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/gymsar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const router = require('express').Router()
router.use('/programmes', require('./programmes'))
router.use('/subjects', require('./subjects'))
router.use('/courses', require('./courses'))
router.use('/subject-areas', require('./subjectAreas'))

router.get('/', (req, res) => {
res.json({
level: 'Gymnasiesärskolan och särvux gymnasial',
routes: ['/programmes', '/programmes/[code]', '/subjects', '/subjects/[code]', '/courses', '/courses/[code]']
routes: ['/programmes', '/programmes/[code]', '/subjects', '/subjects/[code]', '/courses', '/courses/[code]', '/subject-areas', '/subject-areas/[code]']
})
})

Expand Down
60 changes: 60 additions & 0 deletions src/controllers/gymsar/subjectAreas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Route manager for: /gymsar/subject-areas
// Data folder: gys

// Dependencies
const router = require('express').Router()
const path = require('path')
const fs = require('fs')
const { parseString } = require('xml2js')
const async = require('async')

// Helpers
const formatter = require('../../helpers/gymsar/formatter')
const latinize = require('../../helpers/latinize')

const rootPath = path.resolve(__dirname + '/../../../files/gys')

router.get('/', (req, res) => {
const folder = rootPath + '/subjectArea'
const files = fs.readdirSync(folder)

let areas = []
async.each(files, (file, callback) => {
parseString(fs.readFileSync(folder + '/' + file), (err, result) => {
areas.push({
name: result.subject.name[0],
code: result.subject.code[0]
})

callback()
})
}, (error) => {
res.json(areas)
})
})

router.get('/:code', (req, res) => {
const code = req.params.code.toLowerCase()
const folder = rootPath + '/subjectArea'
const files = fs.readdirSync(folder)

let area
async.each(files, (file, callback) => {
parseString(fs.readFileSync(folder + '/' + file), (err, result) => {
if (result.subject.code[0].toLowerCase() === code) {
const data = result.subject
area = formatter.formatSubjectData(result.subject)
}

callback()
})
}, (error) => {
if (area) {
res.json(area)
} else {
res.status(404).send('Not found')
}
})
})

module.exports = router
8 changes: 4 additions & 4 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ router.get('/', (req, res) => {
'<a href="/gym">/gym</a><br><br>' +

'Obligatoriska skolan<br>' +
'<a href="/gru">/gru</a><br><br><br>' +
'<a href="/gru">/gru</a><br><br>' +

'<b>Work-in-progress routes:</b><br><br>' +
'Gymnasiesärskolan och särvux gymnasial<br>' +
'<a href="/gymsar">/gymsar</a><br><br><br>' +

'Gymnasiesärskolan och särvux gymnasial (WIP)<br>' +
'<a href="/gymsar">/gymsar</a><br><br>' +
'<b>Work-in-progress routes:</b><br><br>' +

'Kommunal vuxenutbildning grundläggande (WIP)<br>' +
'<a href="/vuxgru">/vuxgru</a><br><br>' +
Expand Down
10 changes: 6 additions & 4 deletions src/helpers/gym/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ function Formatter() {
function formatKnowledgeRequirements(requirementsList) {
const requirements = []
requirementsList.forEach(reqData => {
requirements.push({
grade: reqData.gradeStep[0],
description: turndownService.turndown(reqData.text[0])
})
const req = { description: turndownService.turndown(reqData.text[0]) }

if (reqData.gradeStep) req.grade = reqData.gradeStep[0]
if (reqData.year && typeof reqData.year[0] === 'string') req.year = reqData.year[0]

requirements.push(req)
})

return requirements
Expand Down
37 changes: 29 additions & 8 deletions src/helpers/gymsar/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,21 @@ function Formatter() {
}

function formatSubjectData(data) {
return {
const subject = {
name: data.name[0],
code: data.code[0],
skolfs: data.skolfsId[0],
applianceDate: data.applianceDate[0],
typeOfSchooling: data.typeOfSchooling[0],
category: data.category[0],
description: turndownService.turndown(data.description[0]),
purpose: turndownService.turndown(data.purpose[0]),
courses: formatCourses(data.courses)
purpose: turndownService.turndown(data.purpose[0])
}
if (data.courses) subject.courses = formatCourses(data.courses)
if (data.centralContents) subject.centralContent = formatCentralContent(data.centralContents)
if (data.knowledgeRequirements) subject.knowledgeRequirements = formatKnowledgeRequirements(data.knowledgeRequirements)

return subject
}

function formatCourses(courseList) {
Expand All @@ -205,13 +209,29 @@ function Formatter() {
return course
}

function formatCentralContent(blocks) {
const content = []
blocks.forEach(block => {
const row = {
text: turndownService.turndown(block.text[0])
}
if (block.year && typeof block.year[0] === 'string') row.year = block.year[0]

content.push(row)
})

return content.length > 1 ? content : content[0]
}

function formatKnowledgeRequirements(requirementsList) {
const requirements = []
requirementsList.forEach(reqData => {
requirements.push({
grade: reqData.gradeStep[0],
description: turndownService.turndown(reqData.text[0])
})
const req = { description: turndownService.turndown(reqData.text[0]) }

if (reqData.gradeStep) req.grade = reqData.gradeStep[0]
if (reqData.year && typeof reqData.year[0] === 'string') req.year = reqData.year[0]

requirements.push(req)
})

return requirements
Expand All @@ -220,7 +240,8 @@ function Formatter() {
return {
formatProgramme,
formatSubjects,
formatSubjectData
formatSubjectData,
formatCourseData
}
}

Expand Down

0 comments on commit faf9cab

Please sign in to comment.