Skip to content

Commit

Permalink
Merge pull request #1778 from sul-dlss/resource-deserializer
Browse files Browse the repository at this point in the history
Extract a resource deserializer
  • Loading branch information
jcoyne authored Nov 17, 2023
2 parents ab119ea + 58df083 commit 1b9f640
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/models/embed/purl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def valid?

def contents
@contents ||= ng_xml.xpath('//contentMetadata/resource').map do |resource|
Purl::Resource.new(@druid, resource, rights)
ResourceXmlDeserializer.new(druid, resource, rights).deserialize
end
end

Expand Down
31 changes: 9 additions & 22 deletions app/models/embed/purl/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,20 @@ module Embed
class Purl
class Resource
# @param [String] druid identifier without a namespace
# @param [Nokogiri::XML::Element] resource
# @param [Dor::RightsAuth] rights
def initialize(druid, resource, rights)
# @param [String] type the resource type
# @param [String] description the resource description
# @param [Array<ResourceFile>] files
def initialize(druid:, type:, description:, files: [])
@druid = druid
@resource = resource
@rights = rights
@type = type
@description = description
@files = files
end

attr_reader :druid

def type
@resource.attributes['type'].value
end

def description
@description ||= @resource.xpath('./label').text
end
attr_reader :druid, :type, :description, :files

def three_dimensional?
@resource.attributes['type']&.value == '3d'
end

# @return [Array<ResourceFile>]
def files
@files ||= @resource.xpath('./file').map do |file|
FileXmlDeserializer.new(druid, description, file, @rights).deserialize
end
type == '3d'
end

def size
Expand Down
32 changes: 32 additions & 0 deletions app/models/embed/purl/resource_xml_deserializer.rb
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
44 changes: 22 additions & 22 deletions spec/models/embed/purl/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,35 @@
end

describe '#thumbnail' do
let(:resource) { described_class.new('12345', resource_element, instance_double(Dor::RightsAuth)) }
let(:resource) do
described_class.new(
druid: '12345',
type: 'file',
description: 'great',
files: [
Embed::Purl::ResourceFile.new(filename: 'Non thumb', mimetype: 'image/tiff'),
Embed::Purl::ResourceFile.new(filename: 'The thumb', mimetype: 'image/jp2')
]
)
end

context 'when the resource has a thumbnail' do
let(:resource_element) do
Nokogiri::XML(
<<~XML
<resource sequence="1" type="file">
<file id="Non thumb" mimetype="image/tiff" size="2799535" />
<file id="The Thumb" mimetype="image/jp2" size="2799535" />
</resource>
XML
).xpath('//resource').first
end

it 'is the thumbnail' do
expect(resource.thumbnail.title).to eq 'The Thumb'
expect(resource.thumbnail.title).to eq 'The thumb'
end
end

context 'when the resource does not have a file specific thumbnail' do
let(:resource_element) do
Nokogiri::XML(
<<~XML
<resource sequence="1" type="file">
<file id="Non thumb" mimetype="image/tiff" size="2799535" />
<file id="Another non Thumb" mimetype="audio/aiff" size="2799535" />
</resource>
XML
).xpath('//resource').first
let(:resource) do
described_class.new(
druid: '12345',
type: 'file',
description: 'bland',
files: [
Embed::Purl::ResourceFile.new(filename: 'Non thumb', mimetype: 'image/tiff'),
Embed::Purl::ResourceFile.new(filename: 'Another non Thumb', mimetype: 'audio/aiff')
]
)
end

it 'is nil' do
Expand Down
28 changes: 28 additions & 0 deletions spec/models/embed/purl/resource_xml_deserializer_spec.rb
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

0 comments on commit 1b9f640

Please sign in to comment.