frankfurter/lib/api.rb
2015-12-30 19:52:02 +00:00

76 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require 'sinatra'
require 'sinatra/cross_origin'
require 'sinatra/jsonp'
require 'yajl'
require 'quote'
configure do
enable :cross_origin
end
configure :development do
set :show_exceptions, :after_handler
end
configure :production do
require 'newrelic_rpm'
end
helpers do
def quote
@quote ||= begin
ret = Quote.new(params).attributes
ret[:rates].keep_if { |k, _| symbols.include?(k) } if symbols
ret
end
end
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.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
end
get '/' do
jsonp details: 'http://fixer.io', version: App.version
end
get '/latest' do
last_modified quote[:date]
jsonp quote
end
get(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/) do
transform_date
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