Module: MercuryBanking::CLI::Accounts
- Included in:
- Main
- Defined in:
- lib/mercury_banking/cli/accounts.rb
Overview
Module for account-related commands
Class Method Summary collapse
-
.included(base) ⇒ Object
Add account-related commands to the CLI class.
Class Method Details
.included(base) ⇒ Object
Add account-related commands to the CLI class
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mercury_banking/cli/accounts.rb', line 8 def self.included(base) base.class_eval do desc 'accounts', 'Display all accounts' method_option :format, type: :string, default: 'table', enum: %w[table json], desc: 'Output format (table or json)' def accounts with_api_client do |client| accounts = client.accounts if [:json] || [:format] == 'json' puts JSON.pretty_generate(accounts) else display_accounts_table(accounts) end puts "You have #{accounts.count} account/s" unless [:json] end end desc 'balance ACCOUNT_ID_OR_NUMBER', "Display the current balance of an account (using account number from accounts table)" def balance(account_identifier) with_api_client do |client| # Determine if we're dealing with an account ID or account number account_id = nil begin account = client.find_account_by_number(account_identifier) account_id = account['id'] rescue StandardError # If not found by number, assume it's an ID account_id = account_identifier end account = client.get_account(account_id) balance = client.balance(account_id) if [:json] puts JSON.pretty_generate({ 'account_id' => account_id, 'account_number' => account['accountNumber'], 'name' => account['name'], 'balance' => balance }) else puts "#{account['name']} (#{account['accountNumber']}): $#{format('%.2f', balance)}" end end end end end |