mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-25 12:32:30 +01:00
Cosmetic clean-up
This commit is contained in:
parent
804572f0b8
commit
2cf3113568
15
Rakefile
15
Rakefile
@ -1,22 +1,23 @@
|
|||||||
require_relative 'lib/db'
|
$: << 'lib'
|
||||||
|
|
||||||
require 'fixer'
|
require 'fixer'
|
||||||
require 'rake/testtask'
|
require 'rake/testtask'
|
||||||
|
require 'db'
|
||||||
|
|
||||||
task :reset do
|
task :load do
|
||||||
data = Fixer::Feed.new(:historical).to_a
|
|
||||||
|
|
||||||
Currency.delete
|
Currency.delete
|
||||||
Currency.multi_insert data
|
data = Fixer::Feed.new(:historical)
|
||||||
|
Currency.multi_insert(data.to_a)
|
||||||
end
|
end
|
||||||
|
|
||||||
task :update do
|
task :update do
|
||||||
Fixer::Feed.new.each do |hsh|
|
Fixer::Feed.new.each do |hsh|
|
||||||
Currency.find_or_create hsh
|
Currency.find_or_create(hsh)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rake::TestTask.new do |t|
|
Rake::TestTask.new do |t|
|
||||||
t.libs.push 'lib'
|
t.libs.push('lib')
|
||||||
t.test_files = FileList['spec/*_spec.rb']
|
t.test_files = FileList['spec/*_spec.rb']
|
||||||
t.verbose = true
|
t.verbose = true
|
||||||
end
|
end
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
require './lib/app'
|
$: << 'lib'
|
||||||
|
require 'app'
|
||||||
run Sinatra::Application
|
run Sinatra::Application
|
||||||
|
@ -9,4 +9,3 @@ Sequel.migration do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
11
lib/app.rb
11
lib/app.rb
@ -1,20 +1,17 @@
|
|||||||
require_relative 'snapshot'
|
|
||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
require 'sinatra/jsonp'
|
require 'sinatra/jsonp'
|
||||||
require 'yajl'
|
require 'yajl'
|
||||||
|
require 'snapshot'
|
||||||
|
|
||||||
set :root, File.expand_path('..', File.dirname(__FILE__))
|
set :root, File.expand_path('..', File.dirname(__FILE__))
|
||||||
|
|
||||||
helpers do
|
helpers do
|
||||||
# Ugly as fuck.
|
|
||||||
def snapshot
|
def snapshot
|
||||||
quotes = Snapshot
|
quotes = Snapshot.new(params).quote
|
||||||
.new(params)
|
|
||||||
.quote
|
|
||||||
|
|
||||||
if symbols = params.delete('symbols') || params.delete('currencies')
|
if symbols = params.delete('symbols') || params.delete('currencies')
|
||||||
symbols = symbols.split ','
|
symbols = symbols.split(',')
|
||||||
quotes[:rates].keep_if { |k, _| symbols.include? k }
|
quotes[:rates].keep_if { |k, _| symbols.include?(k) }
|
||||||
end
|
end
|
||||||
|
|
||||||
quotes
|
quotes
|
||||||
|
@ -2,8 +2,6 @@ require 'sequel'
|
|||||||
|
|
||||||
Sequel.connect ENV['DATABASE_URL'] || 'postgres://localhost/fixer'
|
Sequel.connect ENV['DATABASE_URL'] || 'postgres://localhost/fixer'
|
||||||
|
|
||||||
#Sequel.single_threaded = true
|
|
||||||
|
|
||||||
class Currency < Sequel::Model
|
class Currency < Sequel::Model
|
||||||
def self.last_date
|
def self.last_date
|
||||||
order(:date).last.date
|
order(:date).last.date
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
require_relative 'db'
|
|
||||||
require 'virtus'
|
require 'virtus'
|
||||||
|
require 'db'
|
||||||
|
|
||||||
class Snapshot
|
class Snapshot
|
||||||
include Virtus
|
include Virtus
|
||||||
@ -8,36 +8,26 @@ class Snapshot
|
|||||||
attribute :date, Date
|
attribute :date, Date
|
||||||
|
|
||||||
def quote
|
def quote
|
||||||
self.date =
|
self.date = if date
|
||||||
if date
|
Currency.where("date <= '#{date}'").order(:date).last.date
|
||||||
Currency
|
|
||||||
.where("date <= '#{date}'")
|
|
||||||
.order(:date)
|
|
||||||
.last
|
|
||||||
.date
|
|
||||||
else
|
else
|
||||||
Currency.last_date
|
Currency.last_date
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes.merge rates: rebase(rates)
|
attributes.merge(rates: rebase(rates))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def rates
|
def rates
|
||||||
Currency
|
Currency.where(date: date).reduce({}) do |rates, currency|
|
||||||
.where(date: date)
|
rates.update(currency.to_hash)
|
||||||
.reduce({}) do |rates, currency|
|
|
||||||
rates.update currency.to_hash
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ugly as fuck.
|
|
||||||
def rebase(rates)
|
def rebase(rates)
|
||||||
if base.upcase! != 'EUR'
|
if base.upcase! != 'EUR'
|
||||||
denominator = rates
|
denominator = rates.update('EUR' => 1.0).delete(base)
|
||||||
.update('EUR' => 1.0)
|
|
||||||
.delete base
|
|
||||||
|
|
||||||
rates.each do |iso_code, rate|
|
rates.each do |iso_code, rate|
|
||||||
rates[iso_code] = round(rate / denominator)
|
rates[iso_code] = round(rate / denominator)
|
||||||
@ -49,11 +39,11 @@ class Snapshot
|
|||||||
|
|
||||||
def round(rate)
|
def round(rate)
|
||||||
if rate > 100
|
if rate > 100
|
||||||
rate.round 2
|
rate.round(2)
|
||||||
elsif rate > 10
|
elsif rate > 10
|
||||||
rate.round 3
|
rate.round(3)
|
||||||
else
|
else
|
||||||
rate.round 4
|
rate.round(4)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user