diff --git a/Rakefile b/Rakefile index 8f123d7..d5d140b 100644 --- a/Rakefile +++ b/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 diff --git a/config.ru b/config.ru index e47fcde..9caa472 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,3 @@ -require './lib/app' - +$: << 'lib' +require 'app' run Sinatra::Application diff --git a/db/migrations/001_create_currencies.rb b/db/migrations/001_create_currencies.rb index c723eb5..d94a01b 100644 --- a/db/migrations/001_create_currencies.rb +++ b/db/migrations/001_create_currencies.rb @@ -9,4 +9,3 @@ Sequel.migration do end end end - diff --git a/lib/app.rb b/lib/app.rb index 1dcf502..86767d4 100644 --- a/lib/app.rb +++ b/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 diff --git a/lib/db.rb b/lib/db.rb index 7bb6349..8976761 100644 --- a/lib/db.rb +++ b/lib/db.rb @@ -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 diff --git a/lib/snapshot.rb b/lib/snapshot.rb index 3dcf03d..3dc7303 100644 --- a/lib/snapshot.rb +++ b/lib/snapshot.rb @@ -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