Ruby exercise regarding currency

Viewed 50

I would be thankful for any help as I'm really stuck at the moment. I'm new to Ruby.

Just for some background, I've been given a method which is: CurrencyExchange.rate(date, from_currency, to_currency)

I have to use the above method and I can't change the given method. The purpose is to convert one currency to another currency within a JSON file.

I also have a JSON file that contains dates, currencies (eg CZK, SGD...) and values. Here's a section of the JSON file:

"2018-11-22": {
"USD": 1.1403,
"JPY": 128.8,
"BGN": 1.9558,
"CZK": 25.99,
"DKK": 7.4616,
"GBP": 0.88598,
"HUF": 321.52,
"PLN": 4.3004,
"RON": 4.6593,
"SEK": 10.3035,
"CHF": 1.1351,
"ISK": 141.2,
"NOK": 9.7398,

...

Each value shown above is that currency in Euros. I have already done the next part which was two form two formulae:

Convert a given value to Euros (1 Euro)

I have a second formula to convert between any two currencies. eg PLN to RON - I have a way to calculate the exchange rate and I've been working on that.

I have read and parsed through the JSON file. I have a variable called currency_data that I've stored the key value pairs in.

What I need to do is come up with a way to ensure that if I enter a date (key) and two currencies a floating point number is returned, which matches my two formulae.

This is an example of the test:

def test_non_base_currency_exchange_is_correct
correct_rate = 1.2870493690602498
assert_equal correct_rate, CurrencyExchange.rate(Date.new(2018,11,22), "GBP", "USD")
end

What's the best way to approach this? Should I create methods for both formulae?

I would be very grateful for any help at all. Thanksss a million.

0 Answers
Related