Class: Gtmtech::Crypto::Data
- Inherits:
-
Object
- Object
- Gtmtech::Crypto::Data
- Defined in:
- lib/gtmtech/crypto/data.rb
Class Method Summary collapse
- .account_exists?(name, currency) ⇒ Boolean
- .add_account(name, currency, type, url) ⇒ Object
- .add_transaction(date, source_account, source_currency, source_amount, dest_account, dest_currency, dest_amount, fees_account, fees_currency, fees_amount) ⇒ Object
- .delete_transaction(id) ⇒ Object
- .list_accounts ⇒ Object
- .list_transactions ⇒ Object
- .load ⇒ Object
- .reconcile_transactions ⇒ Object
- .save ⇒ Object
Class Method Details
.account_exists?(name, currency) ⇒ Boolean
43 44 45 46 47 |
# File 'lib/gtmtech/crypto/data.rb', line 43 def self.account_exists? name, currency return false unless @@document[ "accounts" ].key? name return false unless @@document[ "accounts" ][ name ][ "currencies" ].include? currency true end |
.add_account(name, currency, type, url) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gtmtech/crypto/data.rb', line 22 def self.add_account name, currency, type, url if @@document[ "accounts" ].key? name currencies = @@document[ "accounts" ][ name ][ "currencies" ] currencies << currency @@document[ "accounts" ][ name ][ "currencies" ] = currencies.uniq.sort @@document[ "accounts" ][ name ][ "type" ] = type if type @@document[ "accounts" ][ name ][ "url" ] = url if url else @@document[ "accounts" ][ name ] = { "currencies" => [ currency ], "type" => type, "url" => url } end end |
.add_transaction(date, source_account, source_currency, source_amount, dest_account, dest_currency, dest_amount, fees_account, fees_currency, fees_amount) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gtmtech/crypto/data.rb', line 49 def self.add_transaction date, source_account, source_currency, source_amount, dest_account, dest_currency, dest_amount, fees_account, fees_currency, fees_amount if date.downcase == "now" date = Time.now.to_s.split(" ").take(2).join(",") end source_amount = Utils.make_decimal source_amount dest_amount = Utils.make_decimal dest_amount id = SecureRandom.uuid.to_s @@document[ "transactions" ][ id ] = { "date" => date, "source_account" => source_account, "source_currency" => source_currency, "source_amount" => source_amount, "dest_account" => dest_account, "dest_currency" => dest_currency, "dest_amount" => dest_amount, "fees_account" => fees_account, "fees_currency" => fees_currency, "fees_amount" => fees_amount } end |
.delete_transaction(id) ⇒ Object
82 83 84 |
# File 'lib/gtmtech/crypto/data.rb', line 82 def self.delete_transaction id @@document[ "transactions" ].delete( id ) end |
.list_accounts ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/gtmtech/crypto/data.rb', line 34 def self.list_accounts puts "Accounts for profile \"#{ENV['CRYPTO_PROFILE'] || 'main'}\":" printf("%-10s %-10s %-60s\n", "name", "type", "currencies") puts "-" * 80 @@document[ "accounts" ].each do |name, account_info| printf("%-10s %-10s %-60s\n", name, account_info[ "type" ], account_info[ "currencies" ].join(",")) end end |
.list_transactions ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gtmtech/crypto/data.rb', line 70 def self.list_transactions puts "Transactions for profile \"#{ENV['CRYPTO_PROFILE'] || 'main'}\":" printf("%-36s %-20s %-15s %-15s %-15s %-15s\n", "id", "date", "src account", "src amount", "dest account", "dest amount") puts "-" * 120 @@document[ "transactions" ].each do |id, txn| printf("%-36s %-20s %-15s %-15s %-15s %-15s\n", id, txn["date"], "#{txn["source_account"]}.#{txn["source_currency"]}", txn["source_amount"], "#{txn["dest_account"]}.#{txn["dest_currency"]}", txn["dest_amount"]) if txn["fees_account"] printf("%-36s %-20s %-15s %-15s %-15s %-15s\n", id, txn["date"], "#{txn["fees_account"]}.#{txn["fees_currency"]}", txn["fees_amount"], "fees.#{txn["fees_currency"]}", txn["fees_amount"]) end end end |
.load ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/gtmtech/crypto/data.rb', line 8 def self.load if ENV['CRYPTO_PROFILE'] @@path = "#{ENV['HOME']}/.crypto.#{ENV['CRYPTO_PROFILE']}.yaml" else @@path = "#{ENV['HOME']}/.crypto.yaml" end unless File.exist? @@path File.open(@@path, 'w') do |file| file.write("---\naccounts: {}\ntransactions: {}\n") end end @@document = YAML.load(File.read(@@path)) end |
.reconcile_transactions ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/gtmtech/crypto/data.rb', line 86 def self.reconcile_transactions self.list_transactions puts "" printf("%-20s %-10s %-20s\n", "account", "currency", "amount") puts "-" * 120 fees = {} currencies = {} @@document[ "accounts" ].each do | name, account_info | account_info[ "currencies" ].sort.each do | currency | decimal = "0.0" @@document[ "transactions" ].each do |id, txn| # explicit fees if txn[ "fees_account" ] == name and txn[ "fees_currency" ] == currency decimal = Utils.decimal_minus( decimal, txn[ "fees_amount" ] ) unless fees.key? currency fees[ currency ] = "0.0" end fees[ currency ] = Utils.decimal_add( fees[ currency ], txn[ "fees_amount" ]) end # account reconciliation if txn[ "source_account" ] == name and txn[ "source_currency" ] == currency decimal = Utils.decimal_minus( decimal, txn[ "source_amount" ] ) # implicit fees if txn[ "source_currency" ] == txn[ "dest_currency" ] and txn[ "source_amount" ] != txn[ "dest_amount" ] implicit_fees = Utils.decimal_minus( txn[ "source_amount" ], txn[ "dest_amount" ] ) unless fees.key? currency fees[ currency ] = "0.0" end fees[ currency ] = Utils.decimal_add( fees[ currency ], implicit_fees ) end end if txn[ "dest_account" ] == name and txn[ "dest_currency" ] == currency decimal = Utils.decimal_add( decimal, txn[ "dest_amount" ] ) end end unless currencies.key? currency currencies[ currency ] = "0.0" end currencies[ currency ] = Utils.decimal_add( currencies[ currency ], decimal ) printf( "%-20s %-10s %-20s\n", name, currency, decimal ) end end fees.keys.sort.each do |currency| unless currencies.key? currency currencies[ currency ] = "0.0" end printf( "%-20s %-10s %-20s\n", "**FEES**", currency, fees[ currency ] ) end puts "" printf( "%-10s %-20s\n", "currency", "reconciliation" ) puts "-" * 120 currencies.keys.sort.each do |currency| currency_fees = fees[ currency ] || "0.0" printf( "%-10s %-20s\n", currency, Utils.decimal_add( currencies[ currency ], currency_fees ) ) end puts "" printf( "%-10s %-20s\n", "currency", "totals" ) puts "-" * 120 currencies.keys.sort.each do |currency| printf( "%-10s %-20s\n", currency, currencies[ currency ] ) end end |
.save ⇒ Object
154 155 156 157 158 |
# File 'lib/gtmtech/crypto/data.rb', line 154 def self.save File.open(@@path, 'w') do |file| file.write( @@document.to_yaml ) end end |