require File.dirname(__FILE__) + '/../spec_helper.rb' describe Money::ExchangeBank do before :each do @bank = Money::ExchangeBank.new end it "returns the previously specified conversion rate" do @bank.add_rate("USD", "EUR", 0.788332676) @bank.add_rate("EUR", "YEN", 122.631477) @bank.get_rate("USD", "EUR").should == 0.788332676 @bank.get_rate("EUR", "YEN").should == 122.631477 end it "returns the previously specified conversion rate" do @bank.add_rate("USD", "EUR", 0.788332676) @bank.add_rate("EUR", "YEN", 122.631477) @bank.get_rate("USD", "EUR").should == 0.788332676 @bank.get_rate("EUR", "YEN").should == 122.631477 end it "treats currency names case-insensitively" do @bank.add_rate("usd", "eur", 1) @bank.get_rate("USD", "EUR").should == 1 @bank.same_currency?("USD", "usd").should be_true @bank.same_currency?("EUR", "usd").should be_false end it "returns nil if the conversion rate is unknown" do @bank.get_rate("American Pesos", "EUR").should be_nil end it "exchanges money from one currency to another according to the specified conversion rates" do @bank.add_rate("USD", "EUR", 0.5) @bank.add_rate("EUR", "YEN", 10) @bank.exchange(10_00, "USD", "EUR").should == 5_00 @bank.exchange(500_00, "EUR", "YEN").should == 5000_00 end it "rounds the exchanged result down" do @bank.add_rate("USD", "EUR", 0.788332676) @bank.add_rate("EUR", "YEN", 122.631477) @bank.exchange(10_00, "USD", "EUR").should == 7_88 @bank.exchange(500_00, "EUR", "YEN").should == 6131573 end it "returns the previously specified conversion rate" do @bank.add_rate("EUR", 122.631477) @bank.get_rate("EUR").should == 122.631477 end it "treats currency names case-insensitively" do @bank.add_rate("yen", 1) @bank.get_rate("YEN").should == 1 @bank.same_currency?("USD", "usd").should be_true @bank.same_currency?("EUR", "usd").should be_false end it "should work with a different default currency" do Money.default_currency = "BRL" @bank.add_rate("yen", 10) @bank.exchange(5_00, "brl", "yen").should == 50_00 Money.default_currency = "USD" end it "returns nil if the conversion rate is unknown" do @bank.get_rate("American Pesos").should be_nil end it "exchanges money from one currency to another according to the specified conversion rates" do @bank.add_rate("EUR", 0.5) @bank.add_rate("YEN", 5) @bank.exchange(10_00, "USD", "EUR").should == 5_00 @bank.exchange(500_00, "USD", "YEN").should == 2500_00 @bank.exchange(2500_00, "YEN", "USD").should == 500_00 end it "should deal fine with FOO_TO_FOO" do @bank.add_rate("USD", 2.0) @bank.get_rate("USD").should be_nil end it "should test some more" do @bank.add_rate("EUR", 0.788332676) @bank.add_rate("YEN", 122.631477) @bank.exchange(10_00, "USD", "EUR").should == 788 @bank.exchange(500_00, "USD", "YEN").should == 6131573 end it "raises Money::UnknownRate upon conversion if the conversion rate is unknown" do lambda { @bank.exchange(10, "USD", "ABC") }.should raise_error(Money::UnknownRate) end describe "Fetching Data" do before(:each) do URI.should_receive(:parse).with("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml").and_return(:uri) Net::HTTP.should_receive(:get).with(:uri).and_return("\n\n\tReference rates\n\t\n\t\tEuropean Central Bank\n\t\n\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\n\t\n") end it "should fetch data" do Money.stub!(:default_currency).and_return("EUR") @bank.fetch_rates @bank.get_rate("DKK").should be_close(7.4453, 0.0001) @bank.get_rate("BRL").should be_close(2.832, 0.001) @bank.exchange(10_00, "EUR", "DKK").should == 74_45 end it "should fetch diff than eur" do Money.stub!(:default_currency).and_return("BRL") @bank.fetch_rates @bank.get_rate("DKK").should be_close(2.6289, 0.0001) @bank.get_rate("EEK").should be_close(5.5249, 0.0001) @bank.get_rate("EUR").should be_close(2.832, 0.001) end it "should fetch for an unknown one" do Money.stub!(:default_currency).and_return("XXX") @bank.fetch_rates @bank.get_rate("DKK").should be_nil @bank.get_rate("EUR", "USD").should be_close(1.4098, 0.001) end it "should fetch only what I want" do Money.stub!(:default_currency).and_return("BRL") @bank.default_rates = ["BRL", "EUR"] @bank.fetch_rates @bank.get_rate("DKK").should be_nil @bank.get_rate("USD").should be_nil @bank.get_rate("EUR").should be_close(2.832, 0.001) end it "should be convert EUR to BRL with default USD" do Money.stub!(:default_currency).and_return("USD") @bank.default_rates = ["BRL", "EUR", "USD"] @bank.fetch_rates @bank.exchange(100, "BRL", "EUR").should be_close(2,2) @bank.exchange(100, "EUR", "BRL") end end describe "Live Fetching" do if ENV["LIVE"] it "should fetch data live" do @bank.fetch_rates @bank.get_rate("DKK").should_not be_nil @bank.get_rate("EUR").should_not be_nil end end end end