From d5a2ced5a67dd099f77bd6b0cd9f0af7a325d340 Mon Sep 17 00:00:00 2001 From: Yemi Aina Date: Mon, 4 Mar 2024 13:25:27 -0500 Subject: [PATCH 1/2] including helper method to mitigate nil to string issue for export controller --- app/models/inst_book.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/app/models/inst_book.rb b/app/models/inst_book.rb index cd41e180..97746919 100644 --- a/app/models/inst_book.rb +++ b/app/models/inst_book.rb @@ -135,14 +135,19 @@ def validate_configuration def extract_av_data_from_rst av_data = {} - lang = JSON.parse(self.options)['lang'] || 'en' + ç + parsed_options = parse_json_options(self.options) + lang = parsed_options['lang'] || 'en' rst_folder = File.join('public', 'OpenDSA', 'RST', lang) - + + # Check if rst_folder to prevent Dir.glob from failing + return av_data unless Dir.exist?(rst_folder) + Dir.glob("#{rst_folder}/**/*.rst").each do |rst_file_path| module_name = File.basename(rst_file_path, ".rst") av_data[module_name] = { avmetadata: {}, inlineav: [], avembed: [] } in_metadata_block = false - + File.foreach(rst_file_path) do |line| if line.strip == '.. avmetadata::' in_metadata_block = true @@ -160,9 +165,9 @@ def extract_av_data_from_rst end end end - + av_data - end + end private @@ -178,7 +183,14 @@ def extract_inlineav_name_from_line(line) def extract_avembed_data_from_line(line) match = line.match(/\.\. avembed:: Exercises\/\w+\/(\w+)\.html/) - match[1] if match # Returns the 3rd level attribbutee or nil if no match + match[1] if match # Returns the 3rd level attribute or nil if no match + end + + # helper method for extract_av_data_from_rst(), safely attempts to parse a JSON string, returning an empty hash as fallback + def parse_json_options(json_str) + JSON.parse(json_str) + rescue JSON::ParserError + {} # Return an empty hash as a fallback end # -------------------------------------------------------------------------------- From 1a9f17f991dc46c3e5a43aae70ce850e35f223bd Mon Sep 17 00:00:00 2001 From: Yemi Aina Date: Mon, 4 Mar 2024 14:20:53 -0500 Subject: [PATCH 2/2] including helper method to mitigate nil to string issue for export controller --- app/models/inst_book.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/inst_book.rb b/app/models/inst_book.rb index 97746919..b5e75332 100644 --- a/app/models/inst_book.rb +++ b/app/models/inst_book.rb @@ -135,7 +135,6 @@ def validate_configuration def extract_av_data_from_rst av_data = {} - ç parsed_options = parse_json_options(self.options) lang = parsed_options['lang'] || 'en' rst_folder = File.join('public', 'OpenDSA', 'RST', lang)