Skip to content

Commit

Permalink
Added rates caching for currency pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
olarotseyi committed Jun 26, 2022
1 parent c3d2314 commit 1265b16
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,15 @@ class CurrencyConverter {
this.currencyAmount = 1
this.convertedValue = 0
this.isDecimalComma = false
this.ratesCache = {};

if(params != undefined){
if(params["from"] !== undefined)
this.from(params["from"])

if(params["to"] !== undefined)
this.to(params["to"])

if(params["amount"] !== undefined)
this.amount(params["amount"])

Expand All @@ -178,7 +179,7 @@ class CurrencyConverter {
from (currencyFrom) {
if(typeof currencyFrom !== "string")
throw new TypeError("currency code should be a string")

if(!this.currencyCode.includes(currencyFrom.toUpperCase()))
throw new Error(`${currencyFrom} is not a valid currency code`)

Expand All @@ -201,15 +202,15 @@ class CurrencyConverter {

if(currencyAmount <= 0)
throw new Error("amount should be a positive number")

this.currencyAmount = currencyAmount
return this
}

setDecimalComma (isDecimalComma){
if(typeof isDecimalComma !== "boolean")
throw new TypeError("isDecimalComma should be a boolean")

this.isDecimalComma = isDecimalComma
return this
}
Expand All @@ -230,25 +231,30 @@ class CurrencyConverter {
rates(){
if(this.currencyFrom === this.currencyTo)
return new Promise((resolve, _) => {resolve(this.currencyAmount) })
else
return got(`https://www.google.co.in/search?q=${this.currencyAmount}+${this.currencyFrom}+to+${this.currencyTo}+&hl=en`)
.then((html) => {
return cheerio.load(html.body)})
.then(($) => {return $(".iBp4i").text().split(" ")[0]})
.then((rates) => {
if(this.isDecimalComma){
if(rates.includes("."))
rates = this.replaceAll(rates, ".", "")
if(rates.includes(","))
rates = this.replaceAll(rates, ",", ".")
}
else{
if(rates.includes(","))
rates = this.replaceAll(rates, ",", "")
}

return parseFloat(rates)
})
else
let currencyPair = this.currencyFrom.toUpperCase() + this.currencyTo.toUpperCase();
if (currencyPair in this.ratesCache)
return this.ratesCache[currencyPair];
else
return got(`https://www.google.co.in/search?q=${this.currencyFrom}+to+${this.currencyTo}+&hl=en`)
.then((html) => {
return cheerio.load(html.body)})
.then(($) => {return $(".iBp4i").text().split(" ")[0]})
.then((rates) => {
if(this.isDecimalComma){
if(rates.includes("."))
rates = this.replaceAll(rates, ".", "")
if(rates.includes(","))
rates = this.replaceAll(rates, ",", ".")
}
else{
if(rates.includes(","))
rates = this.replaceAll(rates, ",", "")
}
this.ratesCache[currencyPair] = parseFloat(rates);
this.removeCurrencyPairFromRatesCache(currencyPair);
return parseFloat(rates)
})
}

convert(currencyAmount){
Expand All @@ -266,25 +272,32 @@ class CurrencyConverter {
throw new Error("currency amount should be a positive value")

return this.rates().then((rates) =>{
// this.convertedValue = rates * this.currencyAmount
this.convertedValue = rates * this.currencyAmount

// as the google result now sends the exact converted
// currency, multiplying the rates with currencyAmount
// currency, multiplying the rates with currencyAmount
// makes it redundant.
this.convertedValue = rates * 1
// this.convertedValue = rates * 1
return this.convertedValue
})
}

currencyName(currencyCode_){
if(typeof currencyCode_ != "string")
throw new TypeError("currency code should be a string")

if(!this.currencyCode.includes(currencyCode_.toUpperCase()))
throw new Error(`${currencyCode_} is not a valid currency code`)

return this.currencies[currencyCode_]
}

removeCurrencyPairFromRatesCache(currencyPair) {
// Deletes cached currencyPair rate an hour later
setTimeout(function() {
delete this.ratesCache[currencyPair];
}, 3600000);
}
}

module.exports = CurrencyConverter

0 comments on commit 1265b16

Please sign in to comment.