Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #3 from rafaelp/master
Browse files Browse the repository at this point in the history
Fix of import and export and use app_id if environment configured
  • Loading branch information
atwam committed Aug 18, 2012
2 parents fc3fe5c + de96f1b commit c318a93
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm use ruby-1.9.2-p290@money-historical-bank
6 changes: 3 additions & 3 deletions lib/money/bank/historical_bank.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/money/bank/open_exchange_rates_loader.rb
Expand Up @@ -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'
Expand Down
37 changes: 36 additions & 1 deletion test/historical_bank_test.rb
Expand Up @@ -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.