Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/score_parser.rb

Instance Method Summary collapse

Instance Method Details

#json_parse(string) ⇒ Object



6
7
8
# File 'lib/score_parser.rb', line 6

def json_parse(string)
	JSON.parse(string)
end

#score_parse(masterfolder) ⇒ Object

main parsing method



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
# File 'lib/score_parser.rb', line 10

def score_parse(masterfolder)	#main parsing method
	# Open master scoresheet file for writing
	ss = File.new(masterfolder + "/scoresheet.txt", "w")

	# Open each .rb file in directory and add username and failure count to scoresheet
	Dir.glob(masterfolder + '/*') do |student_result|
		if File.basename(student_result) != "scoresheet.txt"
			# Parse file into JSON and extract failure count
			file = File.open(student_result, "r")
			contents = file.read
			json = json_parse(contents)
			failure_count = json['summary']['failure_count']
			
			# Set filename to just username of student		
			filename = File.basename(student_result)
			filename.chomp(File.extname(filename))

			# Write username and failure count to score sheet
			ss << filename
			ss << ' '
			ss << failure_count
			ss << "\n"
			file.close
		end
	end

	ss.close
end