Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix of import and export and use app_id if environment configured #3

Merged
merged 6 commits into from

3 participants

@rafaelp

You were using Date.today as hash key to store rates for a specific date.
The problem is that after exporting to a string and importing again the key never returns as a Date object.
So I changed the code to use string as hash key.

@rates[date.to_s]

I've written tests to all implementations.

@travisbot

This pull request passes (merged 0989da9 into fc3fe5c).

@travisbot

This pull request passes (merged de96f1b into fc3fe5c).

@atwam
Owner

Sorry, just got back from holidays.
Sounds good to me, will release a new gem version asap.

@atwam atwam merged commit c318a93 into atwam:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 10, 2012
  1. @rafaelp

    fix import and export

    rafaelp authored
  2. @rafaelp
Commits on Aug 11, 2012
  1. @rafaelp

    fix last commit

    rafaelp authored
  2. @rafaelp

    remove whitespaces

    rafaelp authored
  3. @rafaelp

    add .rvmrc

    rafaelp authored
Commits on Aug 12, 2012
  1. @rafaelp

    fix tests

    rafaelp authored
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.