Skip to content

Commit

Permalink
Move ExchangeRates to Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Calyhre committed May 15, 2017
1 parent 4b978b1 commit fc10eda
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'puma', '~> 3.7'

gem 'pg', '~> 0.18'
gem 'rails', '~> 5.1.1'
gem 'redis', '~>3.2'

gem 'bugsnag'
gem 'countries'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ GEM
rb-fsevent (0.9.8)
rb-inotify (0.9.8)
ffi (>= 0.5.0)
redis (3.3.3)
ruby_dep (1.5.0)
sixarm_ruby_unaccent (1.1.1)
slop (3.6.0)
Expand Down Expand Up @@ -178,6 +179,7 @@ DEPENDENCIES
pg (~> 0.18)
puma (~> 3.7)
rails (~> 5.1.1)
redis (~> 3.2)
spring
spring-watcher-listen (~> 2.0.0)

Expand Down
23 changes: 15 additions & 8 deletions app/models/exchange_rate.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
class ExchangeRate < ApplicationRecord
def self.get_rate(from_iso_code, to_iso_code)
rate = find_by(from: from_iso_code, to: to_iso_code)
rate.present? ? rate.rate : 0
class ExchangeRate
def initialize(opts)
@client = Redis.new(opts)
end

def self.add_rate(from_iso_code, to_iso_code, rate)
exrate = find_or_initialize_by(from: from_iso_code, to: to_iso_code)
exrate.rate = rate
exrate.save!
def add_rate(iso_from, iso_to, rate)
@client.set key(iso_from, iso_to), rate
end

def get_rate(iso_from, iso_to)
@client.get key(iso_from, iso_to)
end

private

def key(a, b)
"#{a}_TO_#{b}"
end
end
3 changes: 2 additions & 1 deletion config/initializers/money.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MoneyRails.configure do |config|
config.default_bank = Money::Bank::VariableExchange.new(ExchangeRate)
REDIS_URL = ENV.fetch('REDIS_URL', 'redis://localhost:6379')
config.default_bank = Money::Bank::VariableExchange.new(ExchangeRate.new(url: REDIS_URL))
end
5 changes: 5 additions & 0 deletions db/migrate/20170515170820_drop_exchange_rates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DropExchangeRates < ActiveRecord::Migration[5.1]
def change
drop_table :exchange_rates
end
end
10 changes: 1 addition & 9 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170514215902) do
ActiveRecord::Schema.define(version: 20170515170820) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "exchange_rates", force: :cascade do |t|
t.string "from", limit: 3
t.string "to", limit: 3
t.float "rate"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "games", force: :cascade do |t|
t.string "game_code"
t.string "parsed_game_code"
Expand Down
7 changes: 4 additions & 3 deletions lib/tasks/currencies.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ API_URL = 'http://rate-exchange-1.appspot.com/currency'.freeze
namespace :currencies do
desc 'Get all currencies exchange rates'
task retrieve_all: :environment do
currencies = Price.pluck(:currency).uniq
currencies = Price.pluck(:currency).uniq.sort

currencies.each do |from|
(currencies - [from]).each do |to|
puts "Retieving #{currencies.count} rates for #{from} currency..."
currencies.each do |to|
response = HTTParty.get(API_URL, query: { from: from, to: to})
rate = JSON.parse(response.body, symbolize_names: true)[:rate]
ExchangeRate.find_or_create_by(from: from, to: to).update_attribute(:rate, rate)
Money.add_rate(from, to, rate)
end
end
end
Expand Down

0 comments on commit fc10eda

Please sign in to comment.