Permalink
Browse files

Drop rspec in favor of minitest

  • Loading branch information...
1 parent dd91732 commit 0d18f9c3c6cac2d0e04d1d9d9d721c1d7d62f112 @oriolgual oriolgual committed Jun 11, 2011
Showing with 148 additions and 149 deletions.
  1. +47 −51 Gemfile.lock
  2. +33 −30 Rakefile
  3. +22 −22 lib/simple_currency/currency_convertible.rb
  4. +1 −3 simple_currency.gemspec
  5. +36 −32 test/simple_currency_test.rb
  6. +9 −11 test/test_helper.rb
View
@@ -8,73 +8,70 @@ GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
- actionmailer (3.0.0)
- actionpack (= 3.0.0)
- mail (~> 2.2.5)
- actionpack (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
+ actionmailer (3.0.8)
+ actionpack (= 3.0.8)
+ mail (~> 2.2.19)
+ actionpack (3.0.8)
+ activemodel (= 3.0.8)
+ activesupport (= 3.0.8)
builder (~> 2.1.2)
erubis (~> 2.6.6)
- i18n (~> 0.4.1)
+ i18n (~> 0.5.0)
rack (~> 1.2.1)
- rack-mount (~> 0.6.12)
- rack-test (~> 0.5.4)
+ rack-mount (~> 0.6.14)
+ rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
- activemodel (3.0.0)
- activesupport (= 3.0.0)
+ activemodel (3.0.8)
+ activesupport (= 3.0.8)
builder (~> 2.1.2)
- i18n (~> 0.4.1)
- activerecord (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
- arel (~> 1.0.0)
+ i18n (~> 0.5.0)
+ activerecord (3.0.8)
+ activemodel (= 3.0.8)
+ activesupport (= 3.0.8)
+ arel (~> 2.0.10)
tzinfo (~> 0.3.23)
- activeresource (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
- activesupport (3.0.0)
- arel (1.0.1)
- activesupport (~> 3.0.0)
+ activeresource (3.0.8)
+ activemodel (= 3.0.8)
+ activesupport (= 3.0.8)
+ activesupport (3.0.8)
+ arel (2.0.10)
builder (2.1.2)
crack (0.1.8)
erubis (2.6.6)
abstract (>= 1.0.0)
fakeweb (1.3.0)
- i18n (0.4.1)
- mail (2.2.5)
+ i18n (0.5.0)
+ mail (2.2.19)
activesupport (>= 2.3.6)
- mime-types
- treetop (>= 1.4.5)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
mime-types (1.16)
minitest (2.2.2)
polyglot (0.3.1)
- rack (1.2.1)
- rack-mount (0.6.12)
+ rack (1.2.3)
+ rack-mount (0.6.14)
rack (>= 1.0.0)
- rack-test (0.5.4)
+ rack-test (0.5.7)
rack (>= 1.0)
- rails (3.0.0)
- actionmailer (= 3.0.0)
- actionpack (= 3.0.0)
- activerecord (= 3.0.0)
- activeresource (= 3.0.0)
- activesupport (= 3.0.0)
- bundler (~> 1.0.0)
- railties (= 3.0.0)
- railties (3.0.0)
- actionpack (= 3.0.0)
- activesupport (= 3.0.0)
- rake (>= 0.8.4)
- thor (~> 0.14.0)
- rake (0.8.7)
- simplecov (0.3.6)
- simplecov-html (>= 0.3.7)
- simplecov-html (0.3.8)
- thor (0.14.0)
- treetop (1.4.8)
+ rails (3.0.8)
+ actionmailer (= 3.0.8)
+ actionpack (= 3.0.8)
+ activerecord (= 3.0.8)
+ activeresource (= 3.0.8)
+ activesupport (= 3.0.8)
+ bundler (~> 1.0)
+ railties (= 3.0.8)
+ railties (3.0.8)
+ actionpack (= 3.0.8)
+ activesupport (= 3.0.8)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.9.2)
+ thor (0.14.6)
+ treetop (1.4.9)
polyglot (>= 0.3.1)
- tzinfo (0.3.23)
+ tzinfo (0.3.27)
PLATFORMS
java
@@ -83,6 +80,5 @@ PLATFORMS
DEPENDENCIES
fakeweb (>= 1.3.0)
minitest
- rails (>= 3.0)
+ rails (~> 3.0.0)
simple_currency!
- simplecov
View
@@ -8,38 +8,41 @@ Rake::TestTask.new do |t|
t.verbose = true
end
-require 'yard'
-YARD::Rake::YardocTask.new(:docs) do |t|
- t.files = ['lib/**/*.rb']
- t.options = ['-m', 'markdown', '--no-private', '-r', 'Readme.md', '--title', 'Simple Currency documentation']
-end
+begin
+ require 'yard'
+ YARD::Rake::YardocTask.new(:docs) do |t|
+ t.files = ['lib/**/*.rb']
+ t.options = ['-m', 'markdown', '--no-private', '-r', 'Readme.md', '--title', 'Simple Currency documentation']
+ end
-site = 'doc'
-source_branch = 'master'
-deploy_branch = 'gh-pages'
+ site = 'doc'
+ source_branch = 'master'
+ deploy_branch = 'gh-pages'
-desc "generate and deploy documentation website to github pages"
-multitask :pages do
- puts ">>> Deploying #{deploy_branch} branch to Github Pages <<<"
- require 'git'
- repo = Git.open('.')
- puts "\n>>> Checking out #{deploy_branch} branch <<<\n"
- repo.branch("#{deploy_branch}").checkout
- (Dir["*"] - [site]).each { |f| rm_rf(f) }
- Dir["#{site}/*"].each {|f| mv(f, "./")}
- rm_rf(site)
- puts "\n>>> Moving generated site files <<<\n"
- Dir["**/*"].each {|f| repo.add(f) }
- repo.status.deleted.each {|f, s| repo.remove(f)}
- puts "\n>>> Commiting: Site updated at #{Time.now.utc} <<<\n"
- message = ENV["MESSAGE"] || "Site updated at #{Time.now.utc}"
- repo.commit(message)
- puts "\n>>> Pushing generated site to #{deploy_branch} branch <<<\n"
- repo.push
- puts "\n>>> Github Pages deploy complete <<<\n"
- repo.branch("#{source_branch}").checkout
-end
+ desc "generate and deploy documentation website to github pages"
+ multitask :pages do
+ puts ">>> Deploying #{deploy_branch} branch to Github Pages <<<"
+ require 'git'
+ repo = Git.open('.')
+ puts "\n>>> Checking out #{deploy_branch} branch <<<\n"
+ repo.branch("#{deploy_branch}").checkout
+ (Dir["*"] - [site]).each { |f| rm_rf(f) }
+ Dir["#{site}/*"].each {|f| mv(f, "./")}
+ rm_rf(site)
+ puts "\n>>> Moving generated site files <<<\n"
+ Dir["**/*"].each {|f| repo.add(f) }
+ repo.status.deleted.each {|f, s| repo.remove(f)}
+ puts "\n>>> Commiting: Site updated at #{Time.now.utc} <<<\n"
+ message = ENV["MESSAGE"] || "Site updated at #{Time.now.utc}"
+ repo.commit(message)
+ puts "\n>>> Pushing generated site to #{deploy_branch} branch <<<\n"
+ repo.push
+ puts "\n>>> Github Pages deploy complete <<<\n"
+ repo.branch("#{source_branch}").checkout
+ end
-task :doc => [:docs]
+ task :doc => [:docs]
+rescue LoadError
+end
task :default => :test
@@ -54,7 +54,7 @@ def _to(target)
return 0.0 if @numeric == 0 # Obviously
- return self if @original == target
+ return @numeric if @currency == target
original = @currency
@@ -78,8 +78,10 @@ def _to(target)
def exchange(original, target, amount)
negative = (@numeric < 0)
+ rate = call_xavier_api(original, target)
+ #
# Get the result and round it to 2 decimals
- result = sprintf("%.2f", call_xavier_api(original, target, amount)).to_f
+ result = sprintf("%.2f", amount * rate).to_f
return -(result) if negative
result
@@ -90,39 +92,28 @@ def exchange(original, target, amount)
#
# 30.eur.at(1.year.ago).to_usd
- def call_xavier_api(original, target, amount)
+ def call_xavier_api(original, target)
# Check if there is any cached XML for the specified date
if defined?(Rails)
parsed_response = Rails.cache.read("xaviermedia_#{stringified_exchange_date}")
end
unless parsed_response # Unless there is a cached XML response, ask for it
- date = @exchange_date
- args = [date.year, date.month.to_s.rjust(2, '0'), date.day.to_s.rjust(2,'0')]
- api_url = "http://api.finance.xaviermedia.com/api/#{args.join('/')}.xml"
-
- uri = URI.parse(api_url)
-
retries = 10
not_found_retries=10
xml_response = nil
begin
- Timeout::timeout(1){
- # Returns the raw response or
- # raises OpenURI::HTTPError when no data available
- xml_response = uri.open.read || nil
- }
+ # Returns the raw response or
+ # raises OpenURI::HTTPError when no data available
+ Timeout::timeout(1) { xml_response = make_call(@exchange_date) || nil }
rescue Timeout::Error
retries -= 1
retries > 0 ? sleep(0.42) && retry : raise
rescue OpenURI::HTTPError # Try to fetch one day and 2 days earlier
not_found_retries -= 1
if not_found_retries >= 0
- date = (Time.parse(date.to_s) - 86400).send(:to_date)
- args = [date.year, date.month.to_s.rjust(2, '0'), date.day.to_s.rjust(2,'0')]
- api_url = "http://api.finance.xaviermedia.com/api/#{args.join('/')}.xml"
- uri = URI.parse(api_url)
+ @exchange_date = (Time.parse(@exchange_date.to_s) - 86400).send(:to_date)
retry
else
raise NotFoundError.new("404 Not Found")
@@ -131,15 +122,24 @@ def call_xavier_api(original, target, amount)
raise NotFoundError.new("Socket Error")
end
- return nil unless xml_response && parsed_response = Crack::XML.parse(xml_response)
- parsed_response = parsed_response["xavierresponse"]["exchange_rates"]["fx"]
+ return nil unless xml_response && parsed_response = Crack::XML.parse(xml_response)["xavierresponse"]["exchange_rates"]["fx"]
# Cache successful XML response for later reuse
if defined?(Rails)
Rails.cache.write("xaviermedia_#{stringified_exchange_date}", parsed_response)
end
end
+ calculate_rate(parsed_response, original, target)
+ end
+
+ def make_call(date)
+ args = [date.year, date.month.to_s.rjust(2, '0'), date.day.to_s.rjust(2,'0')]
+ api_url = "http://api.finance.xaviermedia.com/api/#{args.join('/')}.xml"
+ URI.parse(api_url).open.read
+ end
+
+ def calculate_rate(parsed_response, original, target)
# Calculate the exchange rate from the XML
if parsed_response.first['basecurrency'].downcase == original
rate = parse_rate(parsed_response, target)
@@ -150,11 +150,11 @@ def call_xavier_api(original, target, amount)
target_rate = parse_rate(parsed_response, target)
rate = target_rate / original_rate
end
-
cache_rate(original, target, rate)
- amount * rate
+ rate
end
+
def parse_rate(parsed_response, currency)
rate = parsed_response.select{|element| element["currency_code"].downcase == currency}
raise CurrencyNotFoundException, "#{currency} is not a valid currency" unless rate && rate.is_a?(Array) && rate.first
@@ -15,13 +15,11 @@ Gem::Specification.new do |s|
s.add_dependency 'crack', [">= 0.1.8"]
s.add_development_dependency 'minitest'
- s.add_development_dependency 'simplecov'
s.add_development_dependency 'fakeweb', [">= 1.3.0"]
- s.add_development_dependency 'rails', ">= 3.0"
+ s.add_development_dependency 'rails', "~> 3.0.0"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
end
-
Oops, something went wrong.

0 comments on commit 0d18f9c

Please sign in to comment.