Sha256: c4a384f34e7e2b15b5eda8f56c9a8ee78ac3b8de74cc8c1018ea3ffd02bc17d2

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

module CurrencyRate
  class Synchronizer
    attr_accessor :storage

    def initialize(storage: nil)
      @storage = storage || FileStorage.new
    end

    def sync_fiat!
      _sync CurrencyRate.configuration.fiat_adapters
    end

    def sync_crypto!
      _sync CurrencyRate.configuration.crypto_adapters
    end

    def sync!
      fiat = sync_fiat!
      crypto = sync_crypto!
      [fiat[0] | crypto[0], fiat[1] | crypto[1]]
    end

    private

    def _sync(adapters)
      successfull = []
      failed = []
      adapters.each do |provider|
        adapter_name = "#{provider}Adapter"
        begin
          adapter = CurrencyRate::const_get(adapter_name).instance
          rates = adapter.fetch_rates
          unless rates
            CurrencyRate.logger.warn("Synchronizer#sync!: rates for #{provider} not found")
            failed.push(provider)
            next
          end
          exchange_name = provider.downcase
          @storage.write(exchange_name, rates)
          successfull.push(provider)
        rescue StandardError => e
          failed.push({ provider: e })
          CurrencyRate.logger.error(e)
          next
        end
      end
      [successfull, failed]
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
currency-rate-1.6.1 lib/synchronizer.rb
currency-rate-1.6.0 lib/synchronizer.rb
currency-rate-1.5.4 lib/synchronizer.rb
currency-rate-1.5.3 lib/synchronizer.rb
currency-rate-1.5.2 lib/synchronizer.rb
currency-rate-1.5.1 lib/synchronizer.rb