Permalink
Browse files

Move symbol filtering to Quote

1 parent e5cb17d commit b616346701d5bff79692feeb2a53de3d639fc9ed @hakanensari committed Jan 10, 2017
Showing with 20 additions and 11 deletions.
  1. +2 −9 lib/api.rb
  2. +6 −2 lib/quote.rb
  3. +12 −0 spec/quote_spec.rb
View
@@ -25,13 +25,6 @@ def quote
@quote ||= Quote.new(params)
end
- def quote_attributes
- @quote_attributes ||= quote.attributes.tap do |data|
- symbols = params.values_at('symbols', 'to').compact.first
- data[:rates].keep_if { |k, _| symbols.include?(k) } if symbols
- end
- end
-
def jsonp(data)
json = encode_json(data)
callback = params.delete('callback')
@@ -65,12 +58,12 @@ def encode_json(data)
get '/latest' do
last_modified quote.date
- jsonp quote_attributes
+ jsonp quote.to_h
end
get(/(?<date>\d{4}-\d{2}-\d{2})/) do
last_modified quote.date
- jsonp quote_attributes
+ jsonp quote.to_h
end
not_found do
View
@@ -7,11 +7,12 @@ class Quote
DEFAULT_AMOUNT = 1
DEFAULT_BASE = 'EUR'
- attr_reader :amount, :base, :date
+ attr_reader :amount, :base, :date, :symbols
def initialize(params = {})
self.amount = params['amount']
self.base = params.values_at(:base, :from).compact.first
+ self.symbols = params.values_at(:symbols, :to).compact.first
self.date = params[:date]
end
@@ -27,6 +28,8 @@ def attributes
private
+ attr_writer :symbols
+
def amount=(value)
@amount = (value || DEFAULT_AMOUNT).to_f
raise Invalid, 'Invalid amount' if @amount.zero?
@@ -45,7 +48,8 @@ def date=(value)
end
def find_rates
- quoted_against_default_base? ? find_default_rates : find_rebased_rates
+ rates = quoted_against_default_base? ? find_default_rates : find_rebased_rates
+ symbols ? rates.keep_if { |k, _| symbols.include?(k) } : rates
end
def quoted_against_default_base?
View
@@ -82,4 +82,16 @@ def stub_rates(default_rates = {})
proc { quote }.must_raise Quote::Invalid
end
end
+
+ describe 'when given custom symbols' do
+ let(:quote) { Quote.new(symbols: 'FOO,BAR') }
+
+ it 'quotes rates only for given symbols' do
+ stub_rates 'FOO' => 1, 'BAR' => 2, 'BAZ' => 3 do |quote|
+ quote.rates.keys.must_include 'FOO'
+ quote.rates.keys.must_include 'BAR'
+ quote.rates.keys.wont_include 'BAZ'
+ end
+ end
+ end
end

0 comments on commit b616346

Please sign in to comment.