Skip to content

Commit

Permalink
use nokogiri to build goobi xml tags
Browse files Browse the repository at this point in the history
  • Loading branch information
peetucket committed Oct 3, 2018
1 parent 275569a commit a3b578c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
5 changes: 4 additions & 1 deletion app/models/dor/goobi_tag.rb
Expand Up @@ -13,7 +13,10 @@ def initialize(p_hash)
end

def to_xml
"<tag name=\"#{name.encode(xml: :text)}\" value=\"#{value.encode(xml: :text)}\"></tag>"
Nokogiri::XML::Node.new('tag', Nokogiri::XML::Document.new).tap do |node|
node['name'] = name
node['value'] = value
end.to_xml
end
end
end
14 changes: 7 additions & 7 deletions spec/goobi_spec.rb
Expand Up @@ -23,7 +23,7 @@

it 'creates the correct xml request without ocr tag present' do
allow(item).to receive(:tags).and_return(['DPG : Workflow : book_workflow & stuff', 'Process : Content Type : Book', 'LAB : MAPS'])
expect(@goobi.goobi_xml_tags).to eq('<tag name="DPG" value="Workflow : book_workflow &amp; stuff"></tag><tag name="Process" value="Content Type : Book"></tag><tag name="LAB" value="MAPS"></tag>')
expect(@goobi.goobi_xml_tags).to eq('<tag name="DPG" value="Workflow : book_workflow &amp; stuff"/><tag name="Process" value="Content Type : Book"/><tag name="LAB" value="MAPS"/>')
expect(@goobi.xml_request).to be_equivalent_to <<-END
<stanfordCreationRequest>
<objectId>#{pid}</objectId>
Expand All @@ -40,17 +40,17 @@
<goobiWorkflow>goobi_workflow</goobiWorkflow>
<ocr>false</ocr>
<tags>
<tag name="DPG" value="Workflow : book_workflow &amp; stuff"></tag>
<tag name="Process" value="Content Type : Book"></tag>
<tag name="LAB" value="MAPS"></tag>
<tag name="DPG" value="Workflow : book_workflow &amp; stuff"/>
<tag name="Process" value="Content Type : Book"/>
<tag name="LAB" value="MAPS"/>
</tags>
</stanfordCreationRequest>
END
end

it 'creates the correct xml request with ocr tag present' do
allow(item).to receive(:tags).and_return(['DPG : Workflow : book_workflow', 'DPG : OCR : TRUE'])
expect(@goobi.goobi_xml_tags).to eq('<tag name="DPG" value="Workflow : book_workflow"></tag><tag name="DPG" value="OCR : TRUE"></tag>')
expect(@goobi.goobi_xml_tags).to eq('<tag name="DPG" value="Workflow : book_workflow"/><tag name="DPG" value="OCR : TRUE"/>')
expect(@goobi.xml_request).to be_equivalent_to <<-END
<stanfordCreationRequest>
<objectId>#{pid}</objectId>
Expand All @@ -67,8 +67,8 @@
<goobiWorkflow>goobi_workflow</goobiWorkflow>
<ocr>true</ocr>
<tags>
<tag name="DPG" value="Workflow : book_workflow"></tag>
<tag name="DPG" value="OCR : TRUE"></tag>
<tag name="DPG" value="Workflow : book_workflow"/>
<tag name="DPG" value="OCR : TRUE"/>
</tags>
</stanfordCreationRequest>
END
Expand Down
4 changes: 2 additions & 2 deletions spec/goobi_tag_spec.rb
Expand Up @@ -3,11 +3,11 @@
RSpec.describe Dor::GoobiTag do
it 'encodes a tag as XML' do
goobi_tag = described_class.new(name: 'tag_name', value: 'tag value')
expect(goobi_tag.to_xml).to eq('<tag name="tag_name" value="tag value"></tag>')
expect(goobi_tag.to_xml).to eq('<tag name="tag_name" value="tag value"/>')
end

it 'encodes a tag as XML with special characters' do
goobi_tag = described_class.new(name: 'tag_name > and odd characters', value: 'tag value & other things')
expect(goobi_tag.to_xml).to eq('<tag name="tag_name &gt; and odd characters" value="tag value &amp; other things"></tag>')
expect(goobi_tag.to_xml).to eq('<tag name="tag_name &gt; and odd characters" value="tag value &amp; other things"/>')
end
end

0 comments on commit a3b578c

Please sign in to comment.