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