Permalink
Browse files

Merge pull request #3 from rafaelp/master

Fix of import and export and use app_id if environment configured
  • Loading branch information...
2 parents fc3fe5c + de96f1b commit c318a930f74a000ee020bab194805865b2fb83df @atwam committed Aug 18, 2012
Showing with 42 additions and 5 deletions.
  1. +1 −0 .rvmrc
  2. +3 −3 lib/money/bank/historical_bank.rb
  3. +2 −1 lib/money/bank/open_exchange_rates_loader.rb
  4. +36 −1 test/historical_bank_test.rb
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm use ruby-1.9.2-p290@money-historical-bank
View
6 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
View
3 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'
View
37 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

0 comments on commit c318a93

Please sign in to comment.