diff --git a/.document b/.document deleted file mode 100644 index ecf3673..0000000 --- a/.document +++ /dev/null @@ -1,5 +0,0 @@ -README.rdoc -lib/**/*.rb -bin/* -features/**/*.feature -LICENSE diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..e182e8a --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm --create use ruby-1.8.7@simple_currency > /dev/null diff --git a/lib/simple_currency/currency_convertible.rb b/lib/simple_currency/currency_convertible.rb index 0e49a2b..4fe2cff 100644 --- a/lib/simple_currency/currency_convertible.rb +++ b/lib/simple_currency/currency_convertible.rb @@ -35,8 +35,10 @@ def _to(target) # If not, perform a call to the api json_response = call_api(original, target, amount) - return nil unless json_response - result = sprintf("%.2f", JSON.parse(json_response)["result"]["value"]).to_f + return nil unless json_response && parsed_response = JSON.parse(json_response) + raise parsed_response["message"] if parsed_response["status"] != "ok" + + result = sprintf("%.2f", parsed_response["result"]["value"]).to_f # Cache exchange rate for today only Rails.cache.write("#{original}_#{target}_#{Time.now.to_a[3..5].join('-')}", calculate_rate(amount,result)) if defined?(Rails) @@ -55,7 +57,7 @@ def _to(target) end def check_cache(original, target, amount) - if rate = ::Rails.cache.read("#{original}_#{target}_#{Time.now.to_a[3..5].join('-')}") + if rate = Rails.cache.read("#{original}_#{target}_#{Time.now.to_a[3..5].join('-')}") result = (amount * rate).to_f return result = (result * 100).round.to_f / 100 end diff --git a/spec/simple_currency_spec.rb b/spec/simple_currency_spec.rb index 08289e2..702cd01 100644 --- a/spec/simple_currency_spec.rb +++ b/spec/simple_currency_spec.rb @@ -2,9 +2,13 @@ describe "SimpleCurrency" do - def mock_uri(from_currency, to_currency, amount, result) + def mock_uri(from_currency, to_currency, amount, result, options = {}) args = [from_currency, to_currency, amount] + response = "{\"result\":{\"value\":#{result},\"target\":\"#{to_currency}\",\"base\":\"#{from_currency}\"},\"status\":\"ok\"}" + + response = "{\"message\":\"#{options[:fail_with]}\", \"status\":\"fail\"\}" if options[:fail_with] + FakeWeb.register_uri(:get, "http://xurrency.com/api/#{args.join('/')}", :body => response) end @@ -29,6 +33,14 @@ def mock_uri(from_currency, to_currency, amount, result) 8.should respond_to(:to_eur) end + it "raises any error returned by the api call" do + mock_uri('usd', 'xxx', 1, 1.5, :fail_with => "The currencies are not valid") + mock_uri('usd', 'eur', 1_000_000_000, 1.5, :fail_with => "The amount should be between 0 and 999999999") + + expect {1.usd.to_xxx}.to raise_error("The currencies are not valid") + expect {1_000_000_000.usd.to_eur}.to raise_error("The amount should be between 0 and 999999999") + end + context "when Rails is present" do before(:all) do @@ -50,13 +62,15 @@ def mock_uri(from_currency, to_currency, amount, result) it "ensures the cache is valid only for today" do now = Time.parse('2010-08-30') + Time.stub(:now).and_return(now) + Rails.stub_chain("cache.write").and_return(true) Rails.stub_chain("cache.read").with('usd_eur_30-8-2010').and_return(1.5) mock_uri('usd', 'eur', 1, 1.5) 1.usd.to_eur.should == 1.5 - Time.stub(:now).and_return(now + 86400) # Tomorrow + Time.stub(:now).and_return(now + 86400) # One day later Rails.stub_chain("cache.read").with('usd_eur_31-8-2010').and_return(nil) # Exchange rate has changed next day, so forget cache rate!