lib/europe/currency/exchange_rates.rb in europe-0.0.13 vs lib/europe/currency/exchange_rates.rb in europe-0.0.14

- old
+ new

@@ -1,15 +1,17 @@ +# frozen_string_literal: true + require 'net/http' require 'date' # Europe Gem module Europe # currency module Currency # exchange rates module ExchangeRates - EXCHANGE_URL = 'https://www.floatrates.com/daily/eur.json'.freeze + EXCHANGE_URL = 'https://www.floatrates.com/daily/eur.json' def self.retrieve resp = Net::HTTP.get_response(URI.parse(EXCHANGE_URL)) resp.code.to_i == 200 ? extract_rates(resp.body) : :failed end @@ -19,9 +21,11 @@ rates = { date: Date.parse(data['usd']['date']), rates: {} } filter_rates(data, rates) + rescue JSON::ParserError + :failed end def self.filter_rates(data, rates) data.each do |currency, object| rates[:rates][currency.upcase.to_sym] = object['rate'].to_f