Sha256: fee46a6b1a1f288b139c01a13bdae7bdfaf5eaa58201182ec96bb0b37d32da3b

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# encoding: UTF-8
require 'open-uri'
require 'yajl'
require 'money'

class Money
  module Bank
    class InvalidCache < StandardError ; end

    class OpenExchangeRatesBank < Money::Bank::VariableExchange

      OER_URL = 'http://openexchangerates.org/latest.php'

      attr_accessor :cache
      attr_reader :doc, :oer_rates, :rates_source

      def update_rates
        exchange_rates.each do |exchange_rate|
          rate = exchange_rate.last
          currency = exchange_rate.first
          set_rate('USD', currency, rate)
        end
      end

      def save_rates
        raise InvalidCache unless cache
        open(cache, 'w').write(open(OER_URL).read)
      rescue Errno::ENOENT
        raise InvalidCache
      end

      def exchange(cents, from_currency, to_currency)
        exchange_with(Money.new(cents, from_currency), to_currency)
      end

      def exchange_with(from, to_currency)
        rate = get_rate(from.currency, to_currency)
        unless rate
          from_base_rate = get_rate("USD", from.currency)
          to_base_rate = get_rate("USD", to_currency)
          rate = to_base_rate / from_base_rate
        end
        Money.new(((Money::Currency.wrap(to_currency).subunit_to_unit.to_f / from.currency.subunit_to_unit.to_f) * from.cents * rate).round, to_currency)
      end

      protected

      def find_rates_source
        if !!cache && File.exist?(cache)
          @rates_source = cache
        else
          OER_URL
        end
      end

      def exchange_rates
        @rates_source = find_rates_source
        @doc = Yajl::Parser.parse(open(rates_source).read)
        @oer_rates = @doc['rates']
        @doc['rates']
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
money-open-exchange-rates-0.0.3 lib/money/bank/open_exchange_rates_bank.rb