From 31a7df435dbde22eeb76e50284bac406b52380a9 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Fri, 10 Aug 2012 18:21:34 -0300 Subject: [PATCH 1/6] fix import and export --- lib/money/bank/historical_bank.rb | 6 +++--- test/historical_bank_test.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) 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/test/historical_bank_test.rb b/test/historical_bank_test.rb index f70a863..17dc3b2 100644 --- a/test/historical_bank_test.rb +++ b/test/historical_bank_test.rb @@ -66,4 +66,22 @@ 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 From d4d2706c3455cb48dae1259bb3f994243efff9f6 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Fri, 10 Aug 2012 20:15:42 -0300 Subject: [PATCH 2/6] use app id if environemnt variable set ENV['OPENEXCHANGERATES_APP_ID'] --- lib/money/bank/open_exchange_rates_loader.rb | 1 + test/historical_bank_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/money/bank/open_exchange_rates_loader.rb b/lib/money/bank/open_exchange_rates_loader.rb index baaa55a..537dddf 100644 --- a/lib/money/bank/open_exchange_rates_loader.rb +++ b/lib/money/bank/open_exchange_rates_loader.rb @@ -20,6 +20,7 @@ def load_data(date) # 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 17dc3b2..9c0cf78 100644 --- a/test/historical_bank_test.rb +++ b/test/historical_bank_test.rb @@ -64,6 +64,20 @@ 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 From 203efabb5988fe6461a50ca3a318c3927d54a87c Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sat, 11 Aug 2012 20:11:12 -0300 Subject: [PATCH 3/6] fix last commit --- lib/money/bank/open_exchange_rates_loader.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/money/bank/open_exchange_rates_loader.rb b/lib/money/bank/open_exchange_rates_loader.rb index 537dddf..bf93d7f 100644 --- a/lib/money/bank/open_exchange_rates_loader.rb +++ b/lib/money/bank/open_exchange_rates_loader.rb @@ -15,7 +15,7 @@ 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' From ca779f32ca35bdc93998af526d7334f5ac8d43ff Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sat, 11 Aug 2012 20:11:46 -0300 Subject: [PATCH 4/6] remove whitespaces --- test/historical_bank_test.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/historical_bank_test.rb b/test/historical_bank_test.rb index 9c0cf78..f96657e 100644 --- a/test/historical_bank_test.rb +++ b/test/historical_bank_test.rb @@ -60,11 +60,11 @@ 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' @@ -76,8 +76,10 @@ rate = @bank.get_rate(d1, 'USD', 'EUR') rate.must_equal 0.73062465 - end + end end + + end describe 'export/import' do From 0989da93cbb90671dd2ebe68398730e88a7565f6 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sat, 11 Aug 2012 20:11:56 -0300 Subject: [PATCH 5/6] add .rvmrc --- .rvmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .rvmrc 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 From de96f1b181df9e60598238fecf5e10da7172757a Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Sun, 12 Aug 2012 18:04:20 -0300 Subject: [PATCH 6/6] fix tests --- test/historical_bank_test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/historical_bank_test.rb b/test/historical_bank_test.rb index f96657e..3038cdc 100644 --- a/test/historical_bank_test.rb +++ b/test/historical_bank_test.rb @@ -54,6 +54,7 @@ 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