Sha256: dcbc3d9bf928c725266aff43333ae7147e22b1d27264fe6c664662b1a15611cf

Contents?: true

Size: 781 Bytes

Versions: 1

Compression:

Stored size: 781 Bytes

Contents

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

      def self.retrieve
        resp = Net::HTTP.get_response(URI.parse(EXCHANGE_URL))
        resp.code.to_i == 200 ? extract_rates(resp.body) : :failed
      end

      def self.extract_rates(doc)
        data = JSON.parse(doc)

        rates = { date: Date.parse(data['usd']['date']),
                  rates: {} }

        filter_rates(data, rates)
      end

      def self.filter_rates(data, rates)
        data.each do |currency, object|
          rates[:rates][currency.upcase.to_sym] = object['rate'].to_f
        end
        rates
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
europe-0.0.13 lib/europe/currency/exchange_rates.rb