-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExchangeRateService.rb
38 lines (28 loc) · 965 Bytes
/
ExchangeRateService.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require "open-uri"
require "nokogiri"
require "date"
require_relative "ExchangeRatesCache"
require_relative "ExchangeRateProvider"
class ExchangeRateService
def initialize(exchangeRateProvider)
@rateProvider = exchangeRateProvider
@rates = @rateProvider.loadExchangeRates()
end
def getAvailableCurrencies
# todo: return list of supported currencies from rates cache.
end
def getRateAt(date, currency1, currency2)
if date > (Time.now.utc.to_date + 1)
raise RuntimeError, "Error: can not fetch exchange rate for future date."
end
if @rates.empty || @rates.isOld
@rates = @rateProvider.loadExchangeRates()
end
# Get exchange rates relative to base currency
currency1Rate = @rates.get(date, currency1)
currency2Rate = @rates.get(date, currency2)
# Calculate rate from currency1 to currency2
currency1RateCurrency2 = currency2Rate / currency1Rate
return currency1RateCurrency2
end
end