Class: Moneylovercli::Commands::AddTransaction
- Inherits:
-
Moneylovercli::Command
- Object
- Moneylovercli::Command
- Moneylovercli::Commands::AddTransaction
- Defined in:
- lib/moneylovercli/commands/add_transaction.rb
Instance Method Summary collapse
- #execute(input: $stdin, output: $stdout) ⇒ Object
-
#initialize(options) ⇒ AddTransaction
constructor
A new instance of AddTransaction.
Methods inherited from Moneylovercli::Command
#command, #config, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which
Constructor Details
#initialize(options) ⇒ AddTransaction
Returns a new instance of AddTransaction.
13 14 15 16 |
# File 'lib/moneylovercli/commands/add_transaction.rb', line 13 def initialize() @options = @wallet_id = [:wallet_id] end |
Instance Method Details
#execute(input: $stdin, output: $stdout) ⇒ Object
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 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/moneylovercli/commands/add_transaction.rb', line 18 def execute(input: $stdin, output: $stdout) config.read current_wallet = @wallet_id || config.fetch(:current_wallet) access_token = config.fetch(:access_token) unless current_wallet request = Moneylovercli::Api::Wallet.new(token: access_token).list response = request.parsed_response if !response['data'].nil? wallet_id = prompt.select('Choose your wallet', response['data'].map { |a| [a['name'], a['_id']] }.to_h) config.set_if_empty(:wallet_id, value: wallet_id) config.write(force: true) amount = prompt.ask('Ammount', convert: :int, default: 10) categories = config.fetch(:categories, wallet_id) unless categories prompt.ok('categories are not exist, fetching...') categories_response = Moneylovercli::Api::Category .new(token: access_token) .list(wallet_id: wallet_id) .parsed_response if !categories_response['data'].nil? config.set(:categories, wallet_id, value: categories_response['data'].map { |a| a.slice('_id', 'name')}) config.write(force: true) prompt.ok('fetched categories succesfully') else prompt.error('Failed to fetch categories') return end end categories = config.fetch(:categories, wallet_id) cats = categories.each_with_object({}) { |a, hash| hash[a['name']] = a['_id']; } category_id = prompt.select('Category', cats, filter: true, show_help: :always) display_date = prompt.ask('Date (Eg: YYYY-mm-dd)', default: DateTime.now.strftime('%F')) puts "#{amount} - #{category_id} - #{display_date}" transaction_request = Moneylovercli::Api::Transaction.new(token: access_token) .add(account: wallet_id, category_id: category_id, amount: amount, display_date: display_date) if transaction_request.parsed_response['msg'] == 'transaction_add_success' prompt.ok('Great! Your transaction has been created') else prompt.error("Failed to add transaction: #{transaction_request.parsed_response['msg'] }") end elsif response['msg'] != 'get_list_account_success' prompt.error('Failed to fetch wallet') return end end end |