Module: Europe::Currency::ExchangeRates
- Defined in:
- lib/europe/currency/exchange_rates.rb
Overview
exchange rates
Constant Summary collapse
- EXCHANGE_URL =
'https://www.floatrates.com/daily/eur.json'
Class Method Summary collapse
Class Method Details
.extract_rates(doc) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/europe/currency/exchange_rates.rb', line 19 def self.extract_rates(doc) data = JSON.parse(doc) rates = { date: Date.parse(data['usd']['date']), rates: {} } filter_rates(data, rates) rescue JSON::ParserError :failed end |
.filter_rates(data, rates) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/europe/currency/exchange_rates.rb', line 30 def self.filter_rates(data, rates) data.each do |currency, object| rates[:rates][currency.upcase.to_sym] = object['rate'].to_f end rates end |
.retrieve ⇒ Object
14 15 16 17 |
# File 'lib/europe/currency/exchange_rates.rb', line 14 def self.retrieve resp = Net::HTTP.get_response(URI.parse(EXCHANGE_URL)) resp.code.to_i == 200 ? extract_rates(resp.body) : :failed end |