2015-12-30 20:52:02 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-11-20 17:36:12 +01:00
|
|
|
require 'sinatra'
|
2014-10-07 13:08:28 +02:00
|
|
|
require 'sinatra/cross_origin'
|
2012-11-20 17:36:12 +01:00
|
|
|
require 'sinatra/jsonp'
|
2012-11-21 13:44:33 +01:00
|
|
|
require 'yajl'
|
2015-08-30 12:54:13 +02:00
|
|
|
require 'quote'
|
2012-11-20 17:36:12 +01:00
|
|
|
|
2014-10-07 13:08:28 +02:00
|
|
|
configure do
|
|
|
|
enable :cross_origin
|
|
|
|
end
|
2012-11-21 14:18:16 +01:00
|
|
|
|
2015-05-25 13:12:35 +02:00
|
|
|
configure :development do
|
|
|
|
set :show_exceptions, :after_handler
|
|
|
|
end
|
|
|
|
|
2014-05-01 12:56:28 +02:00
|
|
|
configure :production do
|
|
|
|
require 'newrelic_rpm'
|
|
|
|
end
|
|
|
|
|
2012-11-21 12:31:50 +01:00
|
|
|
helpers do
|
2015-08-30 12:54:13 +02:00
|
|
|
def quote
|
2015-09-15 13:00:57 +02:00
|
|
|
@quote ||= begin
|
2015-09-16 11:33:22 +02:00
|
|
|
ret = Quote.new(params).attributes
|
2015-09-15 13:00:57 +02:00
|
|
|
ret[:rates].keep_if { |k, _| symbols.include?(k) } if symbols
|
2012-11-23 15:27:09 +01:00
|
|
|
|
2015-09-15 13:00:57 +02:00
|
|
|
ret
|
2012-11-23 15:27:09 +01:00
|
|
|
end
|
2015-09-15 13:00:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def symbols
|
|
|
|
return @symbols if defined?(@symbols)
|
2012-11-23 15:27:09 +01:00
|
|
|
|
2015-09-15 13:00:57 +02:00
|
|
|
@symbols = begin
|
|
|
|
ret = params.delete('symbols') || params.delete('currencies')
|
|
|
|
ret.split(',') if ret
|
|
|
|
end
|
2012-11-21 12:31:50 +01:00
|
|
|
end
|
|
|
|
|
2015-09-15 13:00:57 +02:00
|
|
|
def transform_date
|
2015-05-25 13:13:05 +02:00
|
|
|
params[:date] = Date.new(
|
2015-09-15 13:00:57 +02:00
|
|
|
params.delete('year').to_i,
|
|
|
|
params.delete('month').to_i,
|
|
|
|
params.delete('day').to_i
|
2015-05-25 13:13:05 +02:00
|
|
|
)
|
2014-03-17 11:10:39 +01:00
|
|
|
end
|
|
|
|
|
2015-03-09 12:25:11 +01:00
|
|
|
def halt_with_message(status, message)
|
|
|
|
halt status, Yajl::Encoder.encode(error: message)
|
2014-03-17 11:10:39 +01:00
|
|
|
end
|
2012-11-20 17:36:12 +01:00
|
|
|
end
|
|
|
|
|
2014-06-19 13:31:35 +02:00
|
|
|
get '/' do
|
2015-05-25 13:12:35 +02:00
|
|
|
jsonp details: 'http://fixer.io', version: App.version
|
2014-06-19 13:31:35 +02:00
|
|
|
end
|
|
|
|
|
2012-11-20 17:36:12 +01:00
|
|
|
get '/latest' do
|
2015-09-15 13:01:16 +02:00
|
|
|
last_modified quote[:date]
|
2015-08-30 12:54:13 +02:00
|
|
|
jsonp quote
|
2012-11-20 17:36:12 +01:00
|
|
|
end
|
2012-11-20 20:32:26 +01:00
|
|
|
|
2015-05-25 13:12:35 +02:00
|
|
|
get(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/) do
|
2015-09-15 13:00:57 +02:00
|
|
|
transform_date
|
2015-09-15 13:01:16 +02:00
|
|
|
last_modified quote[:date]
|
|
|
|
|
2015-08-30 12:54:13 +02:00
|
|
|
jsonp quote
|
2012-11-23 15:13:49 +01:00
|
|
|
end
|
2014-03-17 11:10:39 +01:00
|
|
|
|
|
|
|
not_found do
|
2015-03-09 12:25:11 +01:00
|
|
|
halt_with_message 404, 'Not found'
|
2014-03-17 11:10:39 +01:00
|
|
|
end
|
|
|
|
|
2015-09-02 23:11:57 +02:00
|
|
|
error ArgumentError do
|
2015-03-09 12:25:11 +01:00
|
|
|
halt_with_message 422, env['sinatra.error'].message.capitalize
|
2014-03-17 11:10:39 +01:00
|
|
|
end
|