diff --git a/endpoints/currency/cb.js b/endpoints/currency/cb.js new file mode 100644 index 00000000..7239bbfc --- /dev/null +++ b/endpoints/currency/cb.js @@ -0,0 +1,39 @@ +/* eslint-disable import/first */ +const request = require('request') +const h = require('apis-helpers') +const cheerio = require('cheerio') +const app = require('../../server') + +app.get('/currency/cb', (req, res) => { + request.get({ + headers: { 'User-Agent': h.browser() }, + url: 'https://www.sedlabanki.is/hagtolur/opinber-gengisskraning/', + }, (err, response, body) => { + if (err || response.statusCode !== 200) { + return res.status(500).json({ error: 'www.sedlabanki.is refuses to respond or give back data' }) + } + + const $ = cheerio.load(body) + const currencies = [] + + $('.Gengistafla').first().find('tr').each(function () { + const tds = $(this).find('td') + const name = tds.eq(0).text() + + if (name) { + currencies.push({ + shortName: name, + longName: tds.eq(1).text(), + value: tds.eq(4).text(), + askValue: null, + bidValue: null, + changeCur: null, + changePer: null, + }) + } + }) + + // cache for 15 minutes + return res.cache(900).json({ results: currencies }) + }) +}) diff --git a/endpoints/currency/documentation.md b/endpoints/currency/documentation.md index 9a0f392e..d4493d73 100644 --- a/endpoints/currency/documentation.md +++ b/endpoints/currency/documentation.md @@ -1,6 +1,6 @@ # Currency in relation to ISK -Source: [m5.is](http://m5.is/), [Arion banki](https://arionbanki.is/) and [Landsbankinn](https://landsbankinn.is/) +Source: [m5.is](http://m5.is/), [Arion banki](https://arionbanki.is/), [Landsbankinn](https://landsbankinn.is/) and [Central Bank of Iceland](https://www.sedlabanki.is) - GET [/currency/:source](https://apis.is/currency/:source) @@ -8,7 +8,7 @@ Get currency data in relation to ISK | Parameters | Description | Example | |--------------------|---------------------|-----------------------| -| :source (required) | Which source to use | [m5](https://apis.is/currency/m5), [arion](https://apis.is/currency/arion) or [lb](https://apis.is/currency/lb) | +| :source (required) | Which source to use | [m5](https://apis.is/currency/m5), [arion](https://apis.is/currency/arion), [lb](https://apis.is/currency/lb), [cb](https://https://apis.is/currency/cb) | --- diff --git a/endpoints/currency/index.js b/endpoints/currency/index.js index 4565a688..4f52d8ca 100644 --- a/endpoints/currency/index.js +++ b/endpoints/currency/index.js @@ -2,7 +2,7 @@ const app = require('../../server') app.get('/currency', (req, res) => { const provider = req.query.provider || 'arion' - const providers = ['m5', 'arion', 'lb', 'borgun'] + const providers = ['m5', 'arion', 'lb', 'borgun', 'cb'] if (providers.indexOf(provider) >= 0) { return res.redirect(301, `/currency/${provider}`) diff --git a/endpoints/currency/tests/integration_test.js b/endpoints/currency/tests/integration_test.js index 6f947a9a..f6940daf 100644 --- a/endpoints/currency/tests/integration_test.js +++ b/endpoints/currency/tests/integration_test.js @@ -45,6 +45,14 @@ describe('currency', () => { }) }) + describe('searching using provider "cb"', () => { + it('should return an array of objects containing correct fields', (done) => { + const params = helpers.testRequestParams('/currency/cb') + const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor) + request(params, resultHandler) + }) + }) + describe('searching using provider "borgun"', () => { it('should return an array of objects containing correct fields', (done) => { const params = helpers.testRequestParams('/currency/borgun')