-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1778 from sul-dlss/resource-deserializer
Extract a resource deserializer
- Loading branch information
Showing
5 changed files
with
92 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Embed | ||
class Purl | ||
class ResourceXmlDeserializer | ||
# @param [String] druid the identifier of the resource | ||
# @param [Nokogiri::XML::Element] resource | ||
# @param [Dor::RightsAuth] rights | ||
def initialize(druid, resource, rights) | ||
@druid = druid | ||
@resource = resource | ||
@rights = rights | ||
end | ||
|
||
def deserialize | ||
description = @resource.xpath('./label').text | ||
Resource.new( | ||
druid: @druid, | ||
type: @resource.attributes['type'].value, | ||
description:, | ||
files: build_files(description) | ||
) | ||
end | ||
|
||
def build_files(description) | ||
@resource.xpath('./file').map do |file| | ||
FileXmlDeserializer.new(@druid, description, file, @rights).deserialize | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Embed::Purl::ResourceXmlDeserializer do | ||
describe '#deserialize' do | ||
let(:resource_element) do | ||
Nokogiri::XML( | ||
<<~XML | ||
<resource sequence="1" type="image"> | ||
<label>What a great image</label> | ||
<file id="Non thumb" mimetype="image/tiff" size="2799535" /> | ||
<file id="The Thumb" mimetype="image/jp2" size="2799535" /> | ||
</resource> | ||
XML | ||
).xpath('//resource').first | ||
end | ||
|
||
let(:resource) { described_class.new('abc123', resource_element, nil).deserialize } | ||
|
||
it 'creates a resource' do | ||
expect(resource.druid).to eq 'abc123' | ||
expect(resource.description).to eq 'What a great image' | ||
expect(resource.type).to eq 'image' | ||
expect(resource.files).to all(be_a Embed::Purl::ResourceFile) | ||
end | ||
end | ||
end |