Skip to content

Commit

Permalink
Merge pull request #167 from OpenDSA/splice_embed
Browse files Browse the repository at this point in the history
update to parse_json_options method to safely handle nil extract
  • Loading branch information
babz007 authored Mar 5, 2024
2 parents ec41c5f + 7b61bf8 commit 8f400a6
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions app/models/inst_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,20 @@ def extract_metadata_from_line(line)

def extract_inlineav_name_from_line(line)
match = line.match(/\.\. inlineav:: (\w+)/)
match[1] if match # Returns the inlineav short name or nil if there is no match
match[1] if match
end

def extract_avembed_data_from_line(line)
match = line.match(/\.\. avembed:: Exercises\/\w+\/(\w+)\.html/)
match[1] if match # Returns the 3rd level attribute or nil if no match
match[1] if 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)
return {} if json_str.nil? # Ensures nil input returns an empty hash
JSON.parse(json_str || '{}')
rescue JSON::ParserError
{} # Return an empty hash as a fallback
{}
end

# --------------------------------------------------------------------------------
Expand Down

0 comments on commit 8f400a6

Please sign in to comment.