Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions endpoints/road/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-disable no-plusplus */
/* eslint-disable no-prototype-builtins */
/* eslint-disable prefer-destructuring */
/* eslint-disable no-unneeded-ternary */

const request = require('request')
const xml2js = require('xml2js')
const h = require('apis-helpers')
const app = require('../../server')

const parseString = xml2js.parseString
// return callback(null, JSON.parse(data))
Comment thread
olafur164 marked this conversation as resolved.
Outdated
const parseFeed = function (callback, data) {
parseString(data, { explicitRoot: false }, (err, result) => {
if (err) return callback(new Error(`Parsing of XML failed. Title ${err}`))
const roads = []

for (let i = 0; i < result.Faerd.length; ++i) {
const Road = result.Faerd[i]
roads.push({
routeId: Road.IdLeid[0].length > 0 ? Road.IdLeid[0] : null, // Can be null
Comment thread
olafur164 marked this conversation as resolved.
Outdated
routeName: Road.LeidNafn[0].length > 0 ? Road.LeidNafn[0] : null, // Can be null
segmentId: parseInt(Road.IdButur[0], 10),
segmentSerial: Road.Rodun[0],
segmentName: Road.LangtNafn[0],
segmentShortName: Road.StuttNafn[0],
segmentSignal: Road.Skilti[0].length > 0 ? Road.Skilti[0] : null,
conditionId: Road.IdAstand[0],
conditionDescription: Road.FulltAstand[0],
conditionShortDescription: Road.StuttAstand[0],
priority: parseInt(Road.Forgangur[0], 10),
comment: Road.Aths[0].length > 0 ? Road.Aths[0] : null,
date: Road.DagsKeyrtUt[0],
isHighlands: parseInt(Road.ErHalendi[0], 2) === 1 ? true : false,
colorCode: Road.Linulitur[0].length > 0 ? Road.Linulitur[0] : null,
conditionUpdated: Road.DagsSkrad[0],
surfaceCondition: Road.AstandYfirbords[0],
})
}
return callback(null, roads)
})
}

const getFeed = function (url, callback) {
request.get({
headers: { 'User-Agent': h.browser(), 'Content-Type': 'application/xml; charset=utf-8' },
encoding: 'utf-8',
url,
}, (error, response, body) => {
console.log(body)
Comment thread
olafur164 marked this conversation as resolved.
Outdated
if (error) return callback(new Error(`${url} did not respond ${JSON.stringify(error)}`))
parseFeed(callback, body)
})
}

const serve = function (url, res, next) {
getFeed(url, (err, data) => {
if (err) {
console.error(err)
return next(502)
}
res.cache(1800).json({ results: data })
})
}

app.get('/road/all', (req, res, next) => {
Comment thread
olafur164 marked this conversation as resolved.
Outdated
const url = 'http://gagnaveita.vegagerdin.is/api/faerd2014_1'
serve(url, res, next)
})
16 changes: 16 additions & 0 deletions endpoints/road/documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Road conditions in Iceland

Source: [Vegagerdin](http://gagnaveita.vegagerdin.is/api/faerd2014_1)

http://www.vegagerdin.is/upplysingar-og-utgafa/gagnaveita-vegagerdarinnar/

- GET [/road](https://apis.is/road)
- GET [/road/all](https://apis.is/road/all)

Lookup road conditions in Iceland

| Endpoints | Description | Example |
|------------|------------------------------------------------|---------------------------------|
| :endpoint | Which region in Iceland to get road conditions | [all](https://apis.is/road/all) |
Comment thread
olafur164 marked this conversation as resolved.
Outdated

---
19 changes: 19 additions & 0 deletions endpoints/road/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const app = require('../../server')

/* Root Vegagerdin */
app.get('/road', (req, res) => {
return res.json({
results: [
{
info: 'This is an Api for Iceland\'s roads conditions',
Comment thread
olafur164 marked this conversation as resolved.
Outdated
endpoints: {
all: '/road/all',
// sudurland: '/road/sudurland',
// nordurland: '/road/nordurland',
// austurland: '/road/austurland',
// vesturland: '/road/vesturland',
},
},
],
})
})
38 changes: 38 additions & 0 deletions endpoints/road/tests/integration_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const request = require('request')
const helpers = require('../../../lib/test_helpers')

describe('road root', () => {
it('should return info', (done) => {
const fieldsToCheckFor = ['info']
const params = helpers.testRequestParams('/road/')
const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor)
request.get(params, resultHandler)
})
})

describe('road - all', () => {
it('should return an array of objects containing correct fields', (done) => {
const fieldsToCheckFor = [
'routeId',
'routeName',
'segmentId',
'segmentSerial',
'segmentName',
'segmentShortName',
'segmentSignal',
'conditionId',
'conditionDescription',
'conditionShortDescription',
'priority',
'comment',
'date',
'isHighlands',
'colorCode',
'conditionUpdated',
'surfaceCondition',
]
const params = helpers.testRequestParams('/road/all')
const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor)
request.get(params, resultHandler)
})
})