Sha256: 14000d3158e40f4b917512e090b71381c6c69c6b31fcba5abe2ccd761ddc96f7

Contents?: true

Size: 942 Bytes

Versions: 3

Compression:

Stored size: 942 Bytes

Contents

require 'danconia/errors/api_error'
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
      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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
danconia-0.4.0 lib/danconia/exchanges/currency_layer.rb
danconia-0.3.1 lib/danconia/exchanges/currency_layer.rb
danconia-0.3.0 lib/danconia/exchanges/currency_layer.rb