Cosmetic clean-up

This commit is contained in:
hakanensari 2013-04-29 23:56:40 +01:00
parent 804572f0b8
commit 2cf3113568
6 changed files with 30 additions and 45 deletions

View File

@ -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

View File

@ -1,3 +1,3 @@
require './lib/app' $: << 'lib'
require 'app'
run Sinatra::Application run Sinatra::Application

View File

@ -9,4 +9,3 @@ Sequel.migration do
end end
end end
end end

View File

@ -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

View File

@ -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

View File

@ -1,43 +1,33 @@
require_relative 'db'
require 'virtus' require 'virtus'
require 'db'
class Snapshot class Snapshot
include Virtus include Virtus
attribute :base, String, default: 'EUR' attribute :base, String, default: 'EUR'
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 else
.where("date <= '#{date}'") Currency.last_date
.order(:date) end
.last
.date
else
Currency.last_date
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| end
rates.update currency.to_hash
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