Skip to content

Commit

Permalink
[1] Capture and raise errors from Xurrency API
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep Mª Bach committed Aug 31, 2010
1 parent 224fa77 commit 6ff823a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
5 changes: 0 additions & 5 deletions .document

This file was deleted.

1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm --create use ruby-1.8.7@simple_currency > /dev/null
8 changes: 5 additions & 3 deletions lib/simple_currency/currency_convertible.rb
Expand Up @@ -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)
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions spec/simple_currency_spec.rb
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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!
Expand Down

0 comments on commit 6ff823a

Please sign in to comment.