Class: MercuryBanking::Multi
- Inherits:
-
Object
- Object
- MercuryBanking::Multi
- Defined in:
- lib/mercury_banking/multi.rb
Overview
Multi-account management class Handles operations across multiple Mercury accounts
Instance Method Summary collapse
- #add_target_account_to_recipient_list(mercury_source, to, email, address, city, region, postal_code, country) ⇒ Object
- #balances ⇒ Object
- #count_checking_accounts(accounts) ⇒ Object
- #ensure_account(account) ⇒ Object
- #ensure_recipient(from:, to:, email:, address:, city:, region:, postal_code:, country:) ⇒ Object
- #find_all_checking_accounts(accounts) ⇒ Object
-
#initialize(keys = []) ⇒ Multi
constructor
A new instance of Multi.
- #transfer(from:, to:, amount:, note:, email:, address:, city:, region:, postal_code:, country:, wait: false) ⇒ Object
- #write_log(from, to, amount, note, transfer) ⇒ Object
Constructor Details
#initialize(keys = []) ⇒ Multi
Returns a new instance of Multi.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mercury_banking/multi.rb', line 7 def initialize(keys = []) @banks = {} @apis = {} @keys = keys @keys.each do |api_key| next if api_key.empty? mercury = MercuryBanking::API.new(api_key) begin accounts = mercury.accounts rescue StandardError => e puts e next end if accounts.nil? puts "*" * 80 puts "* #{api_key} returned no accounts" puts "*" * 80 next end checking = find_all_checking_accounts(accounts) checking.each do |ac| identifier = ac['name'] @banks[identifier] = ac end @identifier = accounts.first["name"] count_checking_accounts(accounts) @apis[@identifier] = mercury end end |
Instance Method Details
#add_target_account_to_recipient_list(mercury_source, to, email, address, city, region, postal_code, country) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mercury_banking/multi.rb', line 73 def add_target_account_to_recipient_list(mercury_source, to, email, address, city, region, postal_code, country) puts "Adding recipient: #{@banks[to]['name']}" rec = MercuryBanking::Recipient.new( name: @banks[to]["name"], address: address, email: email, account_number: @banks[to]["accountNumber"], routing_number: @banks[to]["routingNumber"], city: city, region: region, postal_code: postal_code, country: country ) mercury_source.post(rec.json, "recipients") mercury_source.find_recipient(name: @banks[to]["name"]) end |
#balances ⇒ Object
53 54 55 56 57 58 |
# File 'lib/mercury_banking/multi.rb', line 53 def balances table = Terminal::Table.new rows: @banks.collect { |name, vals| [name, vals["availableBalance"], vals["status"]] } print "#{table}\n" end |
#count_checking_accounts(accounts) ⇒ Object
44 45 46 47 |
# File 'lib/mercury_banking/multi.rb', line 44 def count_checking_accounts(accounts) checking_count = accounts.count { |a| a["kind"] == "checking" } puts "More than one checking account for #{@identifier}: #{checking_count}" if checking_count > 1 end |
#ensure_account(account) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/mercury_banking/multi.rb', line 90 def ensure_account(account) found = @banks.keys.grep(/#{account}/i) raise "Account: #{account} not found." if found.empty? raise "Account: #{account} matched multiple #{found.join(',')}." if found.size > 1 found.first end |
#ensure_recipient(from:, to:, email:, address:, city:, region:, postal_code:, country:) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mercury_banking/multi.rb', line 60 def ensure_recipient(from:, to:, email:, address:, city:, region:, postal_code:, country:) raise "Target bank account not found: #{to}" unless @banks[to] mercury_source = @apis[from] target = mercury_source.find_recipient(name: @banks[to]["name"]) if target.nil? target = add_target_account_to_recipient_list(mercury_source, to, email, address, city, region, postal_code, country) end target end |
#find_all_checking_accounts(accounts) ⇒ Object
49 50 51 |
# File 'lib/mercury_banking/multi.rb', line 49 def find_all_checking_accounts(accounts) accounts.find_all { |a| a["kind"] == "checking" && a["status"] == "active" } end |
#transfer(from:, to:, amount:, note:, email:, address:, city:, region:, postal_code:, country:, wait: false) ⇒ Object
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 |
# File 'lib/mercury_banking/multi.rb', line 98 def transfer(from:, to:, amount:, note:, email:, address:, city:, region:, postal_code:, country:, wait: false) puts "#{from} -> #{to} (#{amount})" from_account = ensure_account(from) to_account = ensure_account(to) # Create a recipient with "to" information in "from"'s account: recipient = ensure_recipient( from: from_account, to: to_account, email: email, address: address, city: city, region: region, postal_code: postal_code, country: country ) puts "From account #{@banks[from_account]['id']} -> Recipient #{recipient['id']} (account: #{@banks[to_account]['id']})" transfer = @apis[from_account].transfer( recipient_id: recipient["id"], amount: amount, account_id: @banks[from_account]["id"], note: note, external: "Ex #{note}" ) if wait while transfer["status"] != "sent" puts "Waiting on #{transfer['id']} (#{transfer['status']}) to complete." sleep 10 transfer = @apis[from_account].transaction(@banks[from_account]["id"], transfer["id"]) puts transfer if transfer["status"] == "failed" end end write_log(from_account, to_account, amount, note, transfer) transfer end |
#write_log(from, to, amount, note, transfer) ⇒ Object
137 138 139 |
# File 'lib/mercury_banking/multi.rb', line 137 def write_log(from, to, amount, note, transfer) File.write("log", "\"#{from}\", \"#{to}\", \"#{amount}\", \"#{note}\", \"#{transfer['status']}\"\n") end |