frankfurter/lib/api.rb
2014-03-17 10:10:39 +00:00

45 lines
940 B
Ruby

require 'sinatra'
require 'sinatra/jsonp'
require 'yajl'
require 'snapshot'
set :root, File.expand_path('..', File.dirname(__FILE__))
helpers do
def snapshot
quotes = Snapshot.new(params).quote
if symbols = params.delete('symbols') || params.delete('currencies')
symbols = symbols.split(',')
quotes[:rates].keep_if { |k, _| symbols.include?(k) }
end
quotes
end
def process_date
params[:date] = Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
end
def halt_with_meaningful_response(status, message)
halt status, "#{message}. Please read http://fixer.io."
end
end
get '/latest' do
jsonp snapshot
end
get %r((?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})) do
process_date
jsonp snapshot
end
not_found do
halt_with_meaningful_response 404, 'Not found'
end
error ArgumentError do
halt_with_meaningful_response 422, env['sinatra.error'].message.capitalize
end