Skip to content

Commit

Permalink
Merge pull request #1779 from sul-dlss/rights
Browse files Browse the repository at this point in the history
Resolve rights values early
  • Loading branch information
jcoyne authored Nov 18, 2023
2 parents 1b9f640 + 64b2e9b commit 7ed0d0b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
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

0 comments on commit 7ed0d0b

Please sign in to comment.