Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added /road endpoint for vegagerdin #483

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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))
olafur164 marked this conversation as resolved.
Show resolved Hide resolved
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
olafur164 marked this conversation as resolved.
Show resolved Hide resolved
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)
olafur164 marked this conversation as resolved.
Show resolved Hide resolved
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) => {
olafur164 marked this conversation as resolved.
Show resolved Hide resolved
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) |
olafur164 marked this conversation as resolved.
Show resolved Hide resolved

---
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',
olafur164 marked this conversation as resolved.
Show resolved Hide resolved
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)
})
})