Refactor quote validation

This commit is contained in:
Hakan Ensari 2016-04-11 14:26:49 +01:00
parent 0471499846
commit 9d0ed94abe
3 changed files with 26 additions and 23 deletions

View File

@ -26,6 +26,8 @@ helpers do
ret ret
end end
rescue Quote::Invalid => ex
halt_with_message 422, ex.message
end end
def symbols def symbols
@ -37,14 +39,6 @@ helpers do
end end
end end
def transform_date
params[:date] = Date.new(
params.delete('year').to_i,
params.delete('month').to_i,
params.delete('day').to_i
)
end
def halt_with_message(status, message) def halt_with_message(status, message)
halt status, Yajl::Encoder.encode(error: message) halt status, Yajl::Encoder.encode(error: message)
end end
@ -59,17 +53,11 @@ get '/latest' do
jsonp quote jsonp quote
end end
get(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/) do get(/(?<date>\d{4}-\d{2}-\d{2})/) do
transform_date
last_modified quote[:date] last_modified quote[:date]
jsonp quote jsonp quote
end end
not_found do not_found do
halt_with_message 404, 'Not found' halt_with_message 404, 'Not found'
end end
error ArgumentError do
halt_with_message 422, env['sinatra.error'].message.capitalize
end

View File

@ -6,6 +6,8 @@ require 'currency'
class Quote class Quote
include Virtus.value_object include Virtus.value_object
Invalid = Class.new(StandardError)
DEFAULT_BASE = 'EUR'.freeze DEFAULT_BASE = 'EUR'.freeze
values do values do
@ -23,9 +25,14 @@ class Quote
def date=(date) def date=(date)
current_date = Currency.current_date_before(date) current_date = Currency.current_date_before(date)
fail ArgumentError, 'Date too old' unless current_date raise Invalid, 'Date too old' unless current_date
super current_date super current_date
rescue Sequel::DatabaseError => ex
if ex.wrapped_exception.is_a?(PG::DatetimeFieldOverflow)
raise Invalid, 'Invalid date'
else
raise
end
end end
def find_rates def find_rates
@ -45,7 +52,7 @@ class Quote
def find_rebased_rates def find_rebased_rates
rates = find_default_rates rates = find_default_rates
denominator = rates.update(DEFAULT_BASE => 1.0).delete(base) denominator = rates.update(DEFAULT_BASE => 1.0).delete(base)
fail ArgumentError, 'Invalid base' unless denominator raise Invalid, 'Invalid base' unless denominator
rates.each do |iso_code, rate| rates.each do |iso_code, rate|
rates[iso_code] = round_rate(rate / denominator) rates[iso_code] = round_rate(rate / denominator)
end end

View File

@ -59,18 +59,26 @@ describe Quote do
describe 'when given an invalid base' do describe 'when given an invalid base' do
let(:quote) { Quote.new(base: 'FOO') } let(:quote) { Quote.new(base: 'FOO') }
it 'fails' do it 'raises validation error' do
proc { quote.to_h }.must_raise ArgumentError proc { quote.to_h }.must_raise Quote::Invalid
end end
end end
describe 'when given a date that is too old' do describe 'when given a date that is too old' do
let(:quote) { Quote.new(date: Date.new(1900)) } let(:quote) { Quote.new(date: '1900-01-01') }
it 'fails' do it 'raises validation error' do
Currency.stub :current_date_before, nil do Currency.stub :current_date_before, nil do
proc { quote }.must_raise ArgumentError proc { quote }.must_raise Quote::Invalid
end end
end end
end end
describe 'when given a bad date' do
let(:quote) { Quote.new(date: '2000-31-01') }
it 'raises validation error' do
proc { quote }.must_raise Quote::Invalid
end
end
end end