-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpensable_cli.rb
103 lines (94 loc) · 3.23 KB
/
expensable_cli.rb
1
2
3
4
5
6
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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require_relative "modules/helpers"
require_relative "modules/sessions"
require_relative "account"
require_relative "category"
require "colorize"
class ExpensableCLI
include Helpers
include Modules
def initialize
@user = nil
@account = nil
@notes = []
end
def menu_user
intro
action = ""
until action == "exit"
action = login_menu
begin
case action
when "login" then login
when "create_user" then create_user
when "exit"then exit
else
puts "Invalid options"
end
rescue HTTParty::ResponseError => e
parsed_error = JSON.parse(e.message, symbolize_names: true)
puts parsed_error
end
end
end
# ---- Methods for user ----
def create_user
credentials = create_form
@user = Sessions.#(credentials)
end
def login
credentials = login_form
@user = Sessions.login(credentials)
puts "Welcome back #{@user[:first_name]} User".colorize(:light_green)
menu_account
end
def logout
@user = Sessions.logout(@user[:token])
end
# ---- End Methods for user ----
# ---- View ----
def menu_account
@account = Account.new(@user[:token])
loop do
print_table("#{parse_name(@account.name)}\n#{parse_date(@account.date)}",
["ID", "Category", "Total"], @account.view_month)
# print table for Expenses and Incomes
action, id = account_menu
id = id.to_i
validation = @account.find_category(id).nil?
case action
when "create" then @account.create_category
when "show" then validation ? (puts "Not Found".colorize(:red)) : menu_category(id)
when "update" then validation ? (puts "Not Found".colorize(:red)) : @account.update_category(id)
when "delete" then validation ? (puts "Not Found".colorize(:red)) : @account.delete_category(id)
when "add-to" then validation ? (puts "Not Found".colorize(:red)) : @account.add_to_transaction(id)
when "toggle" then @account.toggle
when "next" then @account.change_week("next")
when "prev" then @account.change_week("prev")
when "logout"
break
end
end
# print_table("#{parse_name(@account.name)}\n#{parse_date(@account.date)}",
# ["ID", "Category", "Total"], @account.view_month)
end
def menu_category(id_category)
category = Category.new(@user[:token], @account.date, @account.find_category(id_category))
loop do
print_table("#{parse_name(category.name)}\n#{parse_date(category.date)}",
["ID", "Date", "Amount", "Notes"], category.show_category)
action, id = category_menu
id = id.to_i
validation = category.find_transaction(id).nil?
case action
when "add" then validation ? (puts "Not Found".colorize(:red)) : @account.add_to_transaction(id_category)
when "update" then validation ? (puts "Not Found".colorize(:red)) : category.update_transaction(id, id_category)
when "delete" then validation ? (puts "Not Found".colorize(:red)) : category.delete_transaction(id, id_category)
when "next" then category.change_week("next")
when "prev" then category.change_week("prev")
when "back" then break
end
end
end
end
prueba = ExpensableCLI.new
prueba.menu_user