From 3c146f643f0e0568753eba8dd5a1f176ff756f8f Mon Sep 17 00:00:00 2001 From: hakanensari Date: Fri, 16 May 2014 20:35:16 +0100 Subject: [PATCH] Handle more edge cases --- lib/snapshot.rb | 5 ++++- spec/api_spec.rb | 11 ----------- spec/edge_cases_spec.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 spec/edge_cases_spec.rb diff --git a/lib/snapshot.rb b/lib/snapshot.rb index 42a4e3d..2a23227 100644 --- a/lib/snapshot.rb +++ b/lib/snapshot.rb @@ -9,7 +9,9 @@ class Snapshot def quote self.date = if date - Currency.where("date <= '#{date}'").order(:date).last.date + last_date = Currency.where("date <= '#{date}'").order(:date).last + raise ArgumentError.new('Date too old') unless last_date + last_date.date else Currency.last_date end @@ -28,6 +30,7 @@ class Snapshot def rebase(rates) if base.upcase! != 'EUR' denominator = rates.update('EUR' => 1.0).delete(base) + raise ArgumentError.new('Invalid base') unless denominator rates.each do |iso_code, rate| rates[iso_code] = round(rate / denominator) diff --git a/spec/api_spec.rb b/spec/api_spec.rb index a6de438..992f91e 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -7,7 +7,6 @@ describe 'the API' do let(:app) { Sinatra::Application } let(:json) { Yajl::Parser.new.parse last_response.body } - let(:status) { last_response.status } it 'returns latest snapshot' do get '/latest' @@ -34,14 +33,4 @@ describe 'the API' do get '/2010-01-01' json['rates'].wont_be :empty? end - - it 'considers an invalid date unprocessable' do - get '/2010-31-01' - last_response.must_be :unprocessable? - end - - it 'handles unfound pages' do - get '/' - last_response.status.must_equal 404 - end end diff --git a/spec/edge_cases_spec.rb b/spec/edge_cases_spec.rb new file mode 100644 index 0000000..d84b9ea --- /dev/null +++ b/spec/edge_cases_spec.rb @@ -0,0 +1,29 @@ +require_relative 'helper' +require 'rack/test' +require 'api' + +describe 'the API' do + include Rack::Test::Methods + + let(:app) { Sinatra::Application } + + it 'handles unfound pages' do + get '/' + last_response.status.must_equal 404 + end + + it 'will not process an invalid date' do + get '/2010-31-01' + last_response.must_be :unprocessable? + end + + it 'will not process a date before 2000' do + get '/1999-01-01' + last_response.must_be :unprocessable? + end + + it 'will not process an invalid base' do + get '/latest?base=UAH' + last_response.must_be :unprocessable? + end +end