5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/converter/grand_trunk.rb', line 5
def self.get_rate_and_cache(source_currency, to_currency, date)
if source_currency.downcase == to_currency.downcase
rate = 1.0
else
exchange_rate = ExchangeRate.find_by_from_code_and_to_code_and_date(source_currency.downcase, to_currency.downcase, date)
rate = if exchange_rate.present?
exchange_rate.rate
else
response = Net::HTTP.get_response(URI("http://currencies.apps.grandtrunk.net/getrate/#{(date - 1).strftime("%Y-%m-%d")}/#{source_currency.downcase}/#{to_currency.downcase}"))
if response.is_a? Net::HTTPOK
ExchangeRate.create!(from_code: source_currency, to_code: to_currency, date: date, rate: response.body.to_f)
response.body.to_f
else
raise "#{response.code}: #{response.body}"
end
end
end
rate
end
|