lib/danconia/exchanges/currency_layer.rb in danconia-0.2.9 vs lib/danconia/exchanges/currency_layer.rb in danconia-0.3.0

- old
+ new

@@ -2,22 +2,34 @@ require 'net/http' require 'json' module Danconia module Exchanges + # Fetches the rates from https://currencylayer.com/. The `access_key` must be provided. + # See `examples/currency_layer.rb` for a complete usage example. class CurrencyLayer < Exchange - def initialize access_key:, **args - super args + attr_reader :store + + def initialize access_key:, store: Stores::InMemory.new @access_key = access_key + @store = store end def fetch_rates response = JSON.parse Net::HTTP.get URI "http://www.apilayer.net/api/live?access_key=#{@access_key}" if response['success'] response['quotes'] else raise Errors::APIError, response end + end + + def update_rates! + @store.save_rates fetch_rates.map { |pair, rate| {pair: pair, rate: rate} } + end + + def rates **_opts + array_of_rates_to_hash @store.rates end end end end