Skip to content

Instantly share code, notes, and snippets.

Created February 15, 2018 01:43
#!/usr/bin/env ruby
require 'sequel'
require 'json'
DB = Sequel.connect('sqlite://db/wallets.db')
class WalletCentral
def self.output(name=nil)
if name.nil?
DB[:wallets].select_hash_groups(:name, [:currency, :amount]).to_json
else
DB[:wallets].where(name: name).select_hash_groups(:name, [:currency, :amount]).to_json
end
end
def self.transfer(from, to, currency, amount)
from_amount = DB[:wallets].where(name: from, currency: currency).get(:amount)
if from_amount >= 0
DB[:wallets].where(name: from, currency: currency).update(amount: Sequel[:amount]-amount)
DB[:wallets].where(name: to, currency: currency).update(amount: Sequel[:amount]+amount)
else
raise "Amount insufficient"
end
end
end
puts WalletCentral.output
WalletCentral.transfer('jon', 'jaime', 'USD', 100.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment