Skip to content

Commit

Permalink
'cache pbcore xsl for json api, re-escape double quotes in xml before…
Browse files Browse the repository at this point in the history
… json transformation'
  • Loading branch information
foglabs committed Dec 11, 2023
1 parent 955cb4a commit 5ab46fb
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ def show
end

format.json do
pbcore_xml_to_json_xsl_doc = Nokogiri::XSLT(File.read('./lib/pbcore_xml_to_json.xsl'))
# escape double quotes (because they may appear in node values)
xml = xml.gsub(%(\"),%(\\\"))

json = pbcore_xml_to_json_xsl_doc.transform(Nokogiri::XML(xml))
render json: JSON.pretty_generate(
JSON.parse(
pbcore_xml_to_json_xsl_doc.transform(
Nokogiri::XML(xml)
)
)
JSON.parse(json)
)
end
end
Expand All @@ -74,4 +73,11 @@ def transcript
def render_no_transcript_content
render json: { status: '404 Not Found', code: '404', message: 'This transcript is not currently available' }, status: :not_found
end

private
def pbcore_xml_to_json_xsl_doc
Rails.cache.fetch("pbcore_xml_to_json_xsl_doc") do
return Nokogiri::XSLT(File.read('./lib/pbcore_xml_to_json.xsl'))
end
end
end

0 comments on commit 5ab46fb

Please sign in to comment.