Permalink
Browse files

1.2.2 - Minor patches

 * Full rubinius support
 * Definitely removed references to the Xurrency API
 * 100% coverage
  • Loading branch information...
1 parent f9e35d5 commit 0e5c22ee30d364ec5a6a282f0f6893a0232ee983 @txus txus committed Oct 14, 2010
Showing with 56 additions and 62 deletions.
  1. +1 −1 .rvmrc
  2. +2 −0 Gemfile
  3. +14 −0 Gemfile.lock
  4. +1 −1 README.md
  5. +1 −1 VERSION
  6. +3 −44 lib/simple_currency/currency_convertible.rb
  7. +18 −4 spec/simple_currency_spec.rb
  8. +16 −11 spec/spec_helper.rb
View
2 .rvmrc
@@ -1 +1 @@
-rvm --create use ruby-1.8.7@simple_currency > /dev/null
+rvm --create use ruby-1.9.2@simple_currency > /dev/null
View
@@ -3,7 +3,9 @@ source :gemcutter
gem "crack"
group :test do
+ gem "jeweler"
gem "rspec", ">= 2.0.0.beta.22"
gem "fakeweb"
gem "rails"
+ gem "simplecov"
end
View
@@ -36,7 +36,14 @@ GEM
erubis (2.6.6)
abstract (>= 1.0.0)
fakeweb (1.3.0)
+ gemcutter (0.6.1)
+ git (1.2.5)
i18n (0.4.1)
+ jeweler (1.4.0)
+ gemcutter (>= 0.1.0)
+ git (>= 1.2.5)
+ rubyforge (>= 2.0.0)
+ json_pure (1.4.6)
mail (2.2.5)
activesupport (>= 2.3.6)
mime-types
@@ -72,6 +79,11 @@ GEM
rspec-mocks (2.0.0.beta.22)
rspec-core (= 2.0.0.beta.22)
rspec-expectations (= 2.0.0.beta.22)
+ rubyforge (2.0.4)
+ json_pure (>= 1.1.7)
+ simplecov (0.3.6)
+ simplecov-html (>= 0.3.7)
+ simplecov-html (0.3.8)
thor (0.14.0)
treetop (1.4.8)
polyglot (>= 0.3.1)
@@ -84,5 +96,7 @@ PLATFORMS
DEPENDENCIES
crack
fakeweb
+ jeweler
rails
rspec (>= 2.0.0.beta.22)
+ simplecov
View
@@ -1,7 +1,7 @@
#simple_currency
A really simple currency converter using XavierMedia API.
-Compatible with Ruby 1.8, 1.9 and JRuby.
+Compatible with Ruby 1.8, 1.9, JRuby 1.5.3 and Rubinius 1.1.
##Notes
View
@@ -1 +1 @@
-1.2.1
+1.2.2
@@ -70,59 +70,18 @@ def _to(target)
def exchange(original, target, amount)
negative = (self < 0)
- result = @exchange_date ? call_xavier_api(original, target, amount) : call_xurrency_api(original, target, amount)
-
- # Round result to 2 decimals
- result = sprintf("%.2f", result).to_f
+ # Get the result and round it to 2 decimals
+ result = sprintf("%.2f", call_xavier_api(original, target, amount)).to_f
return -(result) if negative
result
end
- ##
- # API call strategies
- ##
-
- # Calls Xurrency API to perform the exchange without a specific date (assuming today)
- #
- def call_xurrency_api(original, target, amount)
-
- if amount > 999_999_999 # Xurrency API does not support numbers bigger than this
- amount = 1
- multiplier = self.abs # Save a multiplier to apply it to the result later
- end
-
- api_url = "http://xurrency.com/api/#{[original, target, amount].join('/')}"
- uri = URI.parse(api_url)
-
-
- retries = 10
- json_response = nil
- begin
- Timeout::timeout(1){
- json_response = uri.open.read || nil # Returns the raw response
- }
- rescue Timeout::Error
- retries -= 1
- retries > 0 ? sleep(0.42) && retry : raise
- end
-
- return nil unless json_response && parsed_response = Crack::JSON.parse(json_response)
- if parsed_response['status'] != 'ok'
- raise CurrencyNotFoundException if parsed_response['message'] =~ /currencies/
- raise parsed_response['message']
- end
-
- result = parsed_response['result']['value'].to_f
- cache_rate(original, target, (result / amount))
- result * (multiplier || 1)
- end
-
# Calls Xavier API to perform the exchange with a specific date.
# This method is called when using :at method, like this:
#
# 30.eur.at(1.year.ago).to_usd
- #
+
def call_xavier_api(original, target, amount)
# Check if there is any cached XML for the specified date
@@ -51,6 +51,11 @@
2.gbp.at(the_past).to_usd.should == 3.07
end
+ it "retries in case of timeout" do
+ mock_xavier_api(the_past, :timeout => 2)
+ 2.eur.at(the_past).to_usd.should == 2.54
+ end
+
it "retries yesterday and two days ago if no data found (could be a holiday)" do
mock_xavier_api(the_past, :fail => true) # It's a holiday
mock_xavier_api(the_past - 86400, :fail => true) # It's a holiday
@@ -60,11 +65,20 @@
end
it "raises an error when no data available" do
- mock_xavier_api(the_past, :fail => true) # It's a holiday
- mock_xavier_api(the_past - 86400, :fail => true) # It's a holiday
- mock_xavier_api(the_past - 86400 - 86400, :fail => true) # It's a holiday
+ counter = 0
+ 10.times do
+ mock_xavier_api(the_past - counter, :fail => true) # It's a holiday
+ counter += 86400
+ end
+
+ expect {
+ begin
+ 1.usd.at(the_past).to_eur
+ rescue Timeout::Error
+ retry
+ end
+ }.to raise_error("404 Not Found")
- expect {1.usd.at(the_past).to_eur}.to raise_error("404 Not Found")
end
end
View
@@ -2,6 +2,11 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems'
+
+require 'simplecov'
+SimpleCov.start do
+ add_group "Lib", 'lib'
+end
require 'fakeweb'
require 'simple_currency'
@@ -13,24 +18,24 @@ def fixture(name)
File.read(File.dirname(__FILE__) + "/support/#{name}")
end
- def mock_xurrency_api(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
-
def mock_xavier_api(date, options = {})
date = date.send(:to_date)
args = [date.year, date.month.to_s.rjust(2, '0'), date.day.to_s.rjust(2,'0')]
response = {:body => fixture("xavier.xml")}
response = {:body => "No exchange rate available", :status => ["404", "Not Found"]} if options[:fail]
- FakeWeb.register_uri(:get, "http://api.finance.xaviermedia.com/api/#{args.join('/')}.xml", response)
+ url = "http://api.finance.xaviermedia.com/api/#{args.join('/')}.xml"
+ FakeWeb.register_uri(:get, url, response)
+
+ if t = options[:timeout]
+ uri = double('uri')
+ uri_open = double('uri.open')
+ URI.stub(:parse).with(url).and_return(uri)
+ uri.stub(:open).and_return(uri_open)
+ t.times { uri_open.should_receive(:read).once.ordered.and_raise(Timeout::Error) }
+ uri_open.should_receive(:read).once.ordered.and_return(response[:body])
+ end
end
end

0 comments on commit 0e5c22e

Please sign in to comment.