mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-10-29 23:32:35 +01:00
Refactor Api
This commit is contained in:
parent
41c585ec3c
commit
c1f2ed25e7
28
lib/api.rb
28
lib/api.rb
@ -18,20 +18,28 @@ end
|
||||
|
||||
helpers do
|
||||
def quote
|
||||
quotes = Quote.new(params).to_h
|
||||
@quote ||= begin
|
||||
ret = Quote.new(params).to_h
|
||||
ret[:rates].keep_if { |k, _| symbols.include?(k) } if symbols
|
||||
|
||||
symbols = params.delete('symbols') || params.delete('currencies')
|
||||
if symbols
|
||||
symbols = symbols.split(',')
|
||||
quotes[:rates].keep_if { |k, _| symbols.include?(k) }
|
||||
ret
|
||||
end
|
||||
|
||||
quotes
|
||||
end
|
||||
|
||||
def process_date
|
||||
def symbols
|
||||
return @symbols if defined?(@symbols)
|
||||
|
||||
@symbols = begin
|
||||
ret = params.delete('symbols') || params.delete('currencies')
|
||||
ret.split(',') if ret
|
||||
end
|
||||
end
|
||||
|
||||
def transform_date
|
||||
params[:date] = Date.new(
|
||||
params[:year].to_i, params[:month].to_i, params[:day].to_i
|
||||
params.delete('year').to_i,
|
||||
params.delete('month').to_i,
|
||||
params.delete('day').to_i
|
||||
)
|
||||
end
|
||||
|
||||
@ -49,7 +57,7 @@ get '/latest' do
|
||||
end
|
||||
|
||||
get(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/) do
|
||||
process_date
|
||||
transform_date
|
||||
jsonp quote
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user