Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 6 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
View
20 lib/money/bank/open_exchange_rates_bank.rb
@@ -23,10 +23,25 @@ def update_rates
end
end
+ def read_from_url
+ open(OER_URL).read
+ end
+
+ def has_valid_rates?(text)
+ parsed = Yajl::Parser.parse(text)
+ parsed && parsed.has_key?('rates')
+ rescue Yajl::ParseError
+ false
+ end
+
+
def save_rates
raise InvalidCache unless cache
- open(cache, 'w') do |f|
- f.write(open(OER_URL).read)
+ new_text = read_from_url
+ if has_valid_rates?(new_text)
+ open(cache, 'w') do |f|
+ f.write(new_text)
+ end
end
rescue Errno::ENOENT
raise InvalidCache
@@ -37,6 +52,7 @@ def exchange(cents, from_currency, to_currency)
end
def exchange_with(from, to_currency)
+ return from if same_currency?(from.currency, to_currency)
rate = get_rate(from.currency, to_currency)
unless rate
from_base_rate = get_rate("USD", from.currency)
View
48 test/open_exchange_rates_bank_test.rb
@@ -4,6 +4,23 @@
describe Money::Bank::OpenExchangeRatesBank do
+ describe 'exchange' do
+ include RR::Adapters::TestUnit
+
+ before do
+ @bank = Money::Bank::OpenExchangeRatesBank.new
+ @temp_cache_path = File.expand_path(File.join(File.dirname(__FILE__), 'tmp.json'))
+ @bank.cache = @temp_cache_path
+ stub(OpenURI::OpenRead).open(Money::Bank::OpenExchangeRatesBank::OER_URL) { File.read @cache_path }
+ @bank.save_rates
+ end
+
+ it "should be able to exchange a money to its own currency even without rates" do
+ money = Money.new(0, "USD");
+ @bank.exchange_with(money, "USD").must_equal money
+ end
+ end
+
describe 'update_rates' do
before do
@cache_path = File.expand_path(File.join(File.dirname(__FILE__), 'latest.json'))
@@ -20,7 +37,6 @@
end
it "should return the correct oer rates using oer" do
- @bank.update_rates
@bank.oer_rates.keys.each do |currency|
next unless Money::Currency.find(currency)
subunit = Money::Currency.wrap(currency).subunit_to_unit
@@ -29,7 +45,6 @@
end
it "should return the correct oer rates using exchange_with" do
- @bank.update_rates
@bank.oer_rates.keys.each do |currency|
next unless Money::Currency.find(currency)
subunit = Money::Currency.wrap(currency).subunit_to_unit
@@ -40,7 +55,7 @@
end
it "should not return 0 with integer rate" do
- Money::Currency::TABLE[:wtf] = {
+ wtf = {
:priority => 1,
:iso_code => "WTF",
:name => "WTF",
@@ -50,14 +65,14 @@
:separator => ".",
:delimiter => ","
}
- Money::Currency::STRINGIFIED_KEYS << 'wtf'
+ Money::Currency.register(wtf)
@bank.add_rate("USD", "WTF", 2)
@bank.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
end
# in response to #4
it "should exchange btc" do
- Money::Currency::TABLE[:btc] = {
+ btc = {
:priority => 1,
:iso_code => "BTC",
:name => "Bitcoin",
@@ -67,7 +82,7 @@
:separator => ".",
:delimiter => ","
}
- Money::Currency::STRINGIFIED_KEYS << 'btc'
+ Money::Currency.register(btc)
@bank.add_rate("USD", "BTC", 1 / 13.7603)
@bank.add_rate("BTC", "USD", 13.7603)
@bank.exchange(100, "BTC", "USD").cents.must_equal 138
@@ -130,6 +145,27 @@
end
end
+ it "should not break an existing file if save fails to read" do
+ initial_size = File.read(@temp_cache_path).size
+ stub(@bank).read_from_url {""}
+ @bank.save_rates
+ File.open(@temp_cache_path).read.size.must_equal initial_size
+ end
+
+ it "should not break an existing file if save returns json without rates" do
+ initial_size = File.read(@temp_cache_path).size
+ stub(@bank).read_from_url { %Q({"error": "An error"}) }
+ @bank.save_rates
+ File.open(@temp_cache_path).read.size.must_equal initial_size
+ end
+
+ it "should not break an existing file if save returns a invalid json" do
+ initial_size = File.read(@temp_cache_path).size
+ stub(@bank).read_from_url { %Q({invalid_json: "An error"}) }
+ @bank.save_rates
+ File.open(@temp_cache_path).read.size.must_equal initial_size
+ end
+
after do
File.delete @temp_cache_path
end

No commit comments for this range

Something went wrong with that request. Please try again.