-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path109_xml_write.rb
55 lines (42 loc) · 1.3 KB
/
109_xml_write.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
# frozen_string_literal: true
require 'rexml/document'
require 'date'
current_path = File.dirname(__FILE__)
file_name = "#{current_path}/modules/output/my_expenses.xml"
not_found_msg = 'File "/modules/output/my_expenses.xml" not found'
# Checking XML-file
abort not_found_msg unless File.exist?(file_name)
file = File.read(file_name)
# XML-file validation check
begin
doc = REXML::Document.new(file)
rescue REXML::ParseException => e
abort "\n\n\t\t*** FATAL ERROR!!!! ***\n\n" + e.message
end
puts "\n\t***Helper for writing your expenses to the XML-file***\n\n"
puts 'Reason for wasting money:'
reason = $stdin.gets.chomp
puts 'Amount spent:'
amount = $stdin.gets.chomp
puts 'Date (dd.mm.yyy) or blank line for today date:'
date_input = $stdin.gets.chomp
expense_date = if date_input == ''
Date.today
else
Date.parse(date_input)
end
puts 'Input an expense category:'
category = $stdin.gets.chomp
# Preparing data for writing the file
expenses = doc.elements.find('expenses').first
expense = expenses.add_element 'expense', {
'amount' => amount,
'category' => category,
'date' => expense_date.to_s
}
expense.text = reason
# Writing to the file
file = File.new(file_name, 'w')
doc.write(file, 2)
file.close
puts "\n\t***Recorded successfully***"