From 0862dba4f4469586560ceb09dec5b2703aec5a96 Mon Sep 17 00:00:00 2001 From: Alex Legler Date: Thu, 26 Mar 2015 18:51:08 +0100 Subject: Make parsing more resilient to weird advisories --- lib/glsav1.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/glsav1.rb b/lib/glsav1.rb index 0ecea87..22ec1f1 100644 --- a/lib/glsav1.rb +++ b/lib/glsav1.rb @@ -7,9 +7,9 @@ class GLSAv1 def parse(xml) @id = xml.root['id'] - @title = xml.xpath('/glsa/title/text()').first.content - @synopsis = xml.xpath('/glsa/synopsis/text()').first.content - @product = xml.xpath('/glsa/product/text()').first.content + @title = text_content xml, '/glsa/title/text()' + @synopsis = text_content xml, '/glsa/synopsis/text()' + @product = text_content xml, '/glsa/product/text()' @date = DateTime.parse(xml.xpath('/glsa/announced/text()').first.content) @revised, @revision = xml.xpath('/glsa/revised/text()').first.content.split(': ') @@ -27,14 +27,27 @@ class GLSAv1 } end - @background = xml.xpath('/glsa/background').first.children.to_xml.strip - @description = xml.xpath('/glsa/description').first.children.to_xml.strip + @background = xml_content xml, '/glsa/background' + @description = xml_content xml, '/glsa/description' @severity = xml.xpath('/glsa/impact').first['type'] - @impact = xml.xpath('/glsa/impact').first.children.to_xml.strip - @workaround = xml.xpath('/glsa/workaround').first.children.to_xml.strip - @resolution = xml.xpath('/glsa/resolution').first.children.to_xml.strip + @impact = xml_content xml, '/glsa/impact' + @workaround = xml_content xml, '/glsa/workaround' + @resolution = xml_content xml, '/glsa/resolution' @references = xml.xpath('/glsa/references/uri').map {|uri| [uri.content, uri['link']] } self end + + private + def xml_content(xml, xpath) + xml.xpath(xpath).first.children.to_xml.strip + rescue + '' + end + + def text_content(xml, xpath) + xml.xpath(xpath).first.content + rescue + '' + end end \ No newline at end of file -- cgit v1.2.3