Sha256: e23d412bed7f9aff3eb693bdfa1f14ae7ade5661644164edd8132b0c9ad649da

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Tannenbaum
  class Quote
    # The [Quote] class defines our medium for getting informal exchange rate information
    # for a given currency on a given [Provider].
    def initialize(currency = :USD)
      raise Tannenbaum::Exceptions::InvalidCurrencyException unless Tannenbaum::Providers.constants.include?(currency)
      @provider = get_provider(currency)
    end

    # Retrieves the latest exchange rate prices according to our [Provider].
    #
    # @param ensure_fresh [Boolean] Ensures that we get the latest rates.
    # @return [ExchangeRate] Struct with a sell and buy prices and the query timestamp.
    def ask(ensure_fresh = false)
      if ensure_fresh
        @exchange_rates = fetch_current_exchange_rates
      else
        @exchange_rates ||= fetch_current_exchange_rates
      end
    end

    private

    def get_provider(currency)
      Tannenbaum::Providers.const_get(currency)
    end

    def fetch_current_exchange_rates
      raw_exchange_data = Tannenbaum::Scraper.new(@provider).process
      Tannenbaum::ExchangeRate.new(raw_exchange_data)
    end
  end

  class ExchangeRate < Struct.new(:sell, :buy, :timestamp, :provider)
    def initialize(hash)
      super(*hash.values_at(:sell, :buy, :timestamp, :provider))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tannenbaum-0.0.1 lib/tannenbaum/quote.rb