frankfurter/lib/app.rb

47 lines
832 B
Ruby
Raw Normal View History

2012-11-20 17:36:12 +01:00
require_relative 'snapshot'
require 'sinatra'
require 'sinatra/jsonp'
2012-11-21 13:44:33 +01:00
require 'yajl'
2012-11-20 17:36:12 +01:00
2012-11-21 14:18:16 +01:00
set :root, File.expand_path('..', File.dirname(__FILE__))
2012-11-21 12:31:50 +01:00
helpers do
2012-11-26 20:36:37 +01:00
def redirect_to_api
2012-11-28 03:16:33 +01:00
if request.url.include? 'https://f'
redirect request.url.sub %r{//}, '//api.'
2012-11-26 20:36:37 +01:00
end
end
2012-11-23 15:27:09 +01:00
# Ugly as fuck.
2012-11-23 15:13:49 +01:00
def snapshot
2012-11-28 03:16:33 +01:00
quotes = Snapshot
2012-11-23 15:13:49 +01:00
.new(params)
.quote
2012-11-23 15:27:09 +01:00
2012-11-28 03:16:33 +01:00
if symbols = params.delete('symbols') || params.delete('currencies')
2012-11-23 15:27:09 +01:00
symbols = symbols.split ','
2012-11-28 03:16:33 +01:00
quotes[:rates].keep_if { |k, _| symbols.include? k }
2012-11-23 15:27:09 +01:00
end
2012-11-28 03:16:33 +01:00
quotes
2012-11-21 12:31:50 +01:00
end
end
2012-11-20 17:36:12 +01:00
get '/' do
File.read File.join 'public', 'index.html'
end
get '/latest' do
2012-11-26 20:36:37 +01:00
redirect_to_api if production?
2012-11-23 15:13:49 +01:00
jsonp snapshot
2012-11-20 17:36:12 +01:00
end
2012-11-20 20:32:26 +01:00
2012-11-21 14:18:24 +01:00
get '/:date' do
2012-11-26 20:36:37 +01:00
redirect_to_api if production?
2012-11-23 15:13:49 +01:00
jsonp snapshot
end
error do
"Something is rotten in the state of Denmark."
2012-11-20 20:32:26 +01:00
end