diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..75dadd4 --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm use ruby-1.9.2-p290@money-historical-bank \ No newline at end of file diff --git a/lib/money/bank/historical_bank.rb b/lib/money/bank/historical_bank.rb index 5601b95..df76d9d 100644 --- a/lib/money/bank/historical_bank.rb +++ b/lib/money/bank/historical_bank.rb @@ -60,9 +60,9 @@ def set_rate(date, from, to, rate) # bank.get_rate(d2, "CAD", "USD") #=> 0.803115 def get_rate(date, from, to) @mutex.synchronize do - unless existing_rates = @rates[date] + unless existing_rates = @rates[date.to_s] load_data(date) - existing_rates = @rates[date] + existing_rates = @rates[date.to_s] end rate = nil if existing_rates @@ -257,7 +257,7 @@ def rate_key_for(from, to) # @return [Numeric] def internal_set_rate(date, from, to, rate) if Money::Currency.find(from) && Money::Currency.find(to) - date_rates = @rates[date] ||= {} + date_rates = @rates[date.to_s] ||= {} date_rates[rate_key_for(from, to)] = rate end end diff --git a/lib/money/bank/open_exchange_rates_loader.rb b/lib/money/bank/open_exchange_rates_loader.rb index baaa55a..bf93d7f 100644 --- a/lib/money/bank/open_exchange_rates_loader.rb +++ b/lib/money/bank/open_exchange_rates_loader.rb @@ -15,11 +15,12 @@ module OpenExchangeRatesLoader # in OpenExchangeRates (short) history. def load_data(date) rates_source = if date == Date.today - OER_URL + OER_URL.dup else # Should we use strftime, does to_s have better performance ? Or is it localized accross systems ? HIST_URL + date.to_s + '.json' end + rates_source << "?app_id=#{ENV['OPENEXCHANGERATES_APP_ID']}" if ENV['OPENEXCHANGERATES_APP_ID'] doc = Yajl::Parser.parse(open(rates_source).read) base_currency = doc['base'] || 'USD' diff --git a/test/historical_bank_test.rb b/test/historical_bank_test.rb index f70a863..3038cdc 100644 --- a/test/historical_bank_test.rb +++ b/test/historical_bank_test.rb @@ -54,16 +54,51 @@ before do @bank = Money::Bank::HistoricalBank.new @cache_path = "#{File.dirname(__FILE__)}/test.json" + ENV['OPENEXCHANGERATES_APP_ID'] = nil end it 'should download new rates from url' do source = Money::Bank::OpenExchangeRatesLoader::HIST_URL + '2009-09-09.json' stub(@bank).open(source) { File.open @cache_path } d1 = Date.new(2009,9,9) - + rate = @bank.get_rate(d1, 'USD', 'EUR') rate.must_equal 0.73062465 end + + describe 'environment variable set with api id' do + before do + ENV['OPENEXCHANGERATES_APP_ID'] = 'example-of-app-id' + end + it 'should download new rates from url' do + source = Money::Bank::OpenExchangeRatesLoader::HIST_URL + '2009-09-09.json' + '?app_id=example-of-app-id' + stub(@bank).open(source) { File.open @cache_path } + d1 = Date.new(2009,9,9) + + rate = @bank.get_rate(d1, 'USD', 'EUR') + rate.must_equal 0.73062465 + end + end + + + end + + describe 'export/import' do + before do + @bank = Money::Bank::HistoricalBank.new + end + it "should store any rate stored for a date, and retrieve it after importing exported json" do + d1 = Date.new(2001,1,1) + d2 = Date.new(2002,1,1) + @bank.set_rate(d1, "USD", "EUR", 1.234) + @bank.set_rate(d2, "GBP", "USD", 1.456) + + json = @bank.export_rates(:json) + @bank.import_rates(:json, json) + + @bank.get_rate(d1, "USD", "EUR").must_equal 1.234 + @bank.get_rate(d2, "GBP", "USD").must_equal 1.456 + end end end