mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-22 02:52:49 +01:00
Refactor quote validation
This commit is contained in:
parent
0471499846
commit
9d0ed94abe
18
lib/api.rb
18
lib/api.rb
@ -26,6 +26,8 @@ helpers do
|
||||
|
||||
ret
|
||||
end
|
||||
rescue Quote::Invalid => ex
|
||||
halt_with_message 422, ex.message
|
||||
end
|
||||
|
||||
def symbols
|
||||
@ -37,14 +39,6 @@ helpers do
|
||||
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)
|
||||
halt status, Yajl::Encoder.encode(error: message)
|
||||
end
|
||||
@ -59,17 +53,11 @@ get '/latest' do
|
||||
jsonp quote
|
||||
end
|
||||
|
||||
get(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/) do
|
||||
transform_date
|
||||
get(/(?<date>\d{4}-\d{2}-\d{2})/) do
|
||||
last_modified quote[:date]
|
||||
|
||||
jsonp quote
|
||||
end
|
||||
|
||||
not_found do
|
||||
halt_with_message 404, 'Not found'
|
||||
end
|
||||
|
||||
error ArgumentError do
|
||||
halt_with_message 422, env['sinatra.error'].message.capitalize
|
||||
end
|
||||
|
13
lib/quote.rb
13
lib/quote.rb
@ -6,6 +6,8 @@ require 'currency'
|
||||
class Quote
|
||||
include Virtus.value_object
|
||||
|
||||
Invalid = Class.new(StandardError)
|
||||
|
||||
DEFAULT_BASE = 'EUR'.freeze
|
||||
|
||||
values do
|
||||
@ -23,9 +25,14 @@ class Quote
|
||||
|
||||
def date=(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
|
||||
rescue Sequel::DatabaseError => ex
|
||||
if ex.wrapped_exception.is_a?(PG::DatetimeFieldOverflow)
|
||||
raise Invalid, 'Invalid date'
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
def find_rates
|
||||
@ -45,7 +52,7 @@ class Quote
|
||||
def find_rebased_rates
|
||||
rates = find_default_rates
|
||||
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[iso_code] = round_rate(rate / denominator)
|
||||
end
|
||||
|
@ -59,18 +59,26 @@ describe Quote do
|
||||
describe 'when given an invalid base' do
|
||||
let(:quote) { Quote.new(base: 'FOO') }
|
||||
|
||||
it 'fails' do
|
||||
proc { quote.to_h }.must_raise ArgumentError
|
||||
it 'raises validation error' do
|
||||
proc { quote.to_h }.must_raise Quote::Invalid
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
proc { quote }.must_raise ArgumentError
|
||||
proc { quote }.must_raise Quote::Invalid
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user