Skip to content

Commit

Permalink
Merge pull request #2136 from sul-dlss/find-constituents
Browse files Browse the repository at this point in the history
Find constituent records for virtual objects when loading from JSON
  • Loading branch information
jcoyne committed Apr 18, 2024
2 parents 87ac19a + 7d3bc7c commit 56b3013
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
17 changes: 16 additions & 1 deletion app/models/embed/purl_json_loader.rb
Expand Up @@ -90,9 +90,24 @@ def embargo_release_date
end

def contents
# NOTE: collections don't have structural.contains
# NOTE: collections don't have structural
return [] unless json['structural']

Array(json.dig('structural', 'contains')).map do |file_set_json|
Purl::ResourceJsonDeserializer.new(@druid, file_set_json).deserialize
end + external_resources(Array(json.dig('structural', 'hasMemberOrders', 0, 'members')))
end

def external_resources(identifiers)
identifiers.map do |identifier|
druid = identifier.delete_prefix('druid:')
component = Embed::Purl.find(druid)
Purl::Resource.new(
druid:,
type: component.type,
description: component.title,
files: []
)
end
end

Expand Down
1 change: 1 addition & 0 deletions app/viewers/embed/viewer_factory.rb
Expand Up @@ -2,6 +2,7 @@

module Embed
class ViewerFactory
# @param [Embed::Request] request
def initialize(embed_request)
@embed_request = embed_request
raise Embed::Purl::ResourceNotEmbeddable unless embed_request.purl_object.valid?
Expand Down
1 change: 1 addition & 0 deletions lib/embed/request.rb
Expand Up @@ -58,6 +58,7 @@ def object_druid
url[/\w*$/]
end

# @return [Embed::Purl]
def purl_object
@purl_object ||= Purl.find(object_druid)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/embed/response.rb
Expand Up @@ -5,6 +5,8 @@ class Response
attr_reader :request

delegate :height, :width, to: :viewer

# @param [Embed::Request] request
def initialize(request)
@request = request
end
Expand Down
31 changes: 31 additions & 0 deletions spec/fixtures/purl_fixtures.rb
Expand Up @@ -82,6 +82,37 @@ def file_purl_json
JSON
end

def virtual_object_purl_json
<<~JSON
{
"type": "https://cocina.sul.stanford.edu/models/object",
"label": "File Title",
"description": {
"title": [
{
"value": "File Title"
}
]
},
"access": {
"view": "world",
"download": "world",
"license": "https://creativecommons.org/publicdomain/mark/1.0/"
},
"structural": {
"contains": [],
"hasMemberOrders": [
{
"members": [
"druid:kq126jw7402"
]
}
]
}
}
JSON
end

def collection_purl_json
<<~JSON
{
Expand Down
13 changes: 13 additions & 0 deletions spec/models/embed/purl_json_loader_spec.rb
Expand Up @@ -63,6 +63,19 @@
end
end

context 'with a virtual object' do
let(:json) { virtual_object_purl_json }
let(:associate) { Embed::Purl.new }

before do
allow(Embed::Purl).to receive(:find).with('kq126jw7402').and_return(associate)
end

it 'has contents' do
expect(data[:contents].first.druid).to eq 'kq126jw7402'
end
end

describe 'license' do
context 'with a creative commons license' do
let(:json) { file_purl_json }
Expand Down

0 comments on commit 56b3013

Please sign in to comment.