Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve rights values early #1779

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions app/models/embed/purl/file_xml_deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,40 @@ def initialize(druid, description, file, rights)
@rights = rights
end

def filename
@filename ||= @file.attributes['id'].value
end

def stanford_only
@rights.stanford_only_rights_for_file(filename).first
end

def location_restricted
@rights.restricted_by_location?(filename)
end

def world_downloadable
@rights.world_downloadable_file?(filename)
end

def duration
return if @file.xpath('./*/@duration').blank?

Embed::MediaDuration.new(@file.xpath('./*[@duration]').first).to_s
end

def deserialize
ResourceFile.new(
druid: @druid,
label: @description,
filename: @file.attributes['id'].value,
mimetype: @file.attributes['mimetype']&.value,
size: @file.attributes['size']&.value.to_i,
rights: @rights
filename:,
stanford_only:,
location_restricted:,
world_downloadable:
) do |file|
if @file.xpath('./*/@duration').present?
file.duration = Embed::MediaDuration.new(@file.xpath('./*[@duration]').first).to_s
end
file.duration = duration
end
end
end
Expand Down
20 changes: 5 additions & 15 deletions app/models/embed/purl/resource_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ def attributes=(hash)
end
end

attr_accessor :druid, :label, :filename, :mimetype, :size, :duration, :rights
attr_accessor :druid, :label, :filename, :mimetype, :size, :duration,
:world_downloadable, :stanford_only, :location_restricted

alias title filename
alias world_downloadable? world_downloadable
alias stanford_only? stanford_only
alias location_restricted? location_restricted

##
# Creates a file url for stacks
Expand Down Expand Up @@ -49,20 +53,6 @@ def pdf?
def image?
mimetype =~ %r{image/jp2}i
end

def stanford_only?
value, _rule = @rights.stanford_only_rights_for_file(title)

value
end

def location_restricted?
@rights.restricted_by_location?(title)
end

def world_downloadable?
@rights.world_downloadable_file?(title)
end
end
end
end
7 changes: 6 additions & 1 deletion spec/models/embed/purl/file_xml_deserializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
XML
).root
end
let(:rights) do
instance_double(Dor::RightsAuth, stanford_only_rights_for_file: [false, nil],
restricted_by_location?: false,
world_downloadable_file?: true)
end

let(:resource_file) { described_class.new('abc123', 'desc', file_node, nil).deserialize }
let(:resource_file) { described_class.new('abc123', 'desc', file_node, rights).deserialize }

it 'creates a resource file' do
expect(resource_file.druid).to eq 'abc123'
Expand Down
8 changes: 7 additions & 1 deletion spec/models/embed/purl/resource_xml_deserializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
).xpath('//resource').first
end

let(:resource) { described_class.new('abc123', resource_element, nil).deserialize }
let(:rights) do
instance_double(Dor::RightsAuth, stanford_only_rights_for_file: [false, nil],
restricted_by_location?: false,
world_downloadable_file?: true)
end

let(:resource) { described_class.new('abc123', resource_element, rights).deserialize }

it 'creates a resource' do
expect(resource.druid).to eq 'abc123'
Expand Down