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 Bitcoin to ISK endpoint #485

Open
wants to merge 5 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
9 changes: 9 additions & 0 deletions endpoints/btc/documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Bitcoin to ISK price

Source [Myntkaup.is](https://myntkaup.is)

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

Current Bitcoin to ISK exchange rate.

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

// set cache time to 10 secs
const cacheTime = 10

function queryGzipJson(url, callback) {
const headers = {
'user-agent': 'apis.is',
}
request.get({
headers,
url,
gzip: true,
json: true,
}, (error, response, data) => {
if (error || response.statusCode !== 200) {
callback(error, response, null)
}
callback(error, response, data)
})
}

function queryRate(callback) {
queryGzipJson('https://myntkaup.is/api/assets/bitcoin', callback)
}

function standardErrorResponse(res) {
return res.status(500).json({
error: 'myntkaup.is refuses to respond or give back data',
})
}

app.get('/btc', (req, res) => {
queryRate((error, response, data) => {
if (error || response.statusCode !== 200) {
return standardErrorResponse(res)
}
return res.cache(cacheTime).json({results: [data]})
})
})
32 changes: 32 additions & 0 deletions endpoints/btc/tests/integration_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable no-bitwise */
/* eslint-disable import/first */
/* eslint-disable import/extensions */
const fs = require('fs')
const request = require('request')
const nock = require('nock')
const helpers = require('../../../lib/test_helpers.js')

describe('btc', () => {
const rateTypes = {
id: String,
symbol: String,
currencySymbol: String,
rateUsd: Number,
rateIsk: Number,
rateIskFriendly: String,
timestamp: Number
}
const fieldsToCheckFor = Object.keys(rateTypes)

before(() => {
nock('https://myntkaup.is')
.get('/api/assets/bitcoin')
.reply(200, fs.readFileSync(`${__dirname}/myntkaup.fixture`))
})

it('should return object containing specific fields with correct types', (done) => {
const params = helpers.testRequestParams('/btc')
const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor)
request(params, resultHandler)
})
})
9 changes: 9 additions & 0 deletions endpoints/btc/tests/myntkaup.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "bitcoin",
"symbol": "BTC",
"currencySymbol": "₿",
"rateUsd": 4040.7933255406665,
"rateIsk": 472287.6491152461,
"rateIskFriendly": "472.287kr.",
"timestamp": 1553041842635
}
Loading