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

Adding currency rate for the central bank of Iceland #494

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions endpoints/currency/cb.js
Original file line number Diff line number Diff line change
@@ -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(),
Comment on lines +21 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the html has changed. The table only has 3 columns now so the indexes in the tds.eq calls are wrong.

Suggested change
const name = tds.eq(0).text()
if (name) {
currencies.push({
shortName: name,
longName: tds.eq(1).text(),
value: tds.eq(4).text(),
const name = tds.eq(1).text()
if (name) {
currencies.push({
shortName: name,
longName: tds.eq(0).text().trim(),
value: Number(tds.eq(2).text().replace(',', '.')),

I'm not sure if the if on line 23 is still valid now that name is from column index 1.

askValue: null,
bidValue: null,
changeCur: null,
changePer: null,
})
}
})

// cache for 15 minutes
return res.cache(900).json({ results: currencies })
})
})
4 changes: 2 additions & 2 deletions endpoints/currency/documentation.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# 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)

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) |


---
2 changes: 1 addition & 1 deletion endpoints/currency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down
8 changes: 8 additions & 0 deletions endpoints/currency/tests/integration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing mock data for this test.

Here's an example of how to do it: https://github.com/apis-is/apis/blob/1fb161a/endpoints/horses/tests/integration_test.js#L22-L4

})
})

describe('searching using provider "borgun"', () => {
it('should return an array of objects containing correct fields', (done) => {
const params = helpers.testRequestParams('/currency/borgun')
Expand Down