Skip to content

Commit

Permalink
Improve clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
lwrubel committed Jun 7, 2024
1 parent 8596c3e commit ae974e7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 74 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,7 @@ RSpec/ExpectInLet: # new in 2.30
Enabled: true
Rails/WhereRange: # new in 2.25
Enabled: true
RSpec/InstanceVariable:
Enabled: false
RSpec/BeforeAfterAll:
Enabled: false
22 changes: 0 additions & 22 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,17 @@ RSpec/AnyInstance:
- 'spec/services/preservation_ingest_service_spec.rb'
- 'spec/services/publish/metadata_transfer_service_spec.rb'
- 'spec/services/publish/public_xml_service_spec.rb'
- 'spec/services/was_shelving_service_spec.rb'

# Offense count: 11
RSpec/Be:
Exclude:
- 'spec/services/publish/public_xml_service_spec.rb'

# Offense count: 2
RSpec/BeforeAfterAll:
Exclude:
- 'spec/services/digital_stacks_service_spec.rb'
- 'spec/services/was_shelving_service_spec.rb'

# Offense count: 6
# Configuration parameters: Prefixes, AllowedPatterns.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
- 'spec/services/digital_stacks_service_spec.rb'
- 'spec/services/publish/public_desc_metadata_service_spec.rb'
- 'spec/services/publish/public_xml_service_spec.rb'
- 'spec/services/thumbnail_service_spec.rb'
Expand All @@ -87,19 +79,11 @@ RSpec/ExpectInHook:
- 'spec/services/preservation_metadata_extractor_spec.rb'
- 'spec/services/publish/metadata_transfer_service_spec.rb'

# Offense count: 19
# Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable:
Exclude:
- 'spec/services/digital_stacks_service_spec.rb'
- 'spec/services/was_shelving_service_spec.rb'

# Offense count: 14
# Configuration parameters: EnforcedStyle.
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Exclude:
- 'spec/services/digital_stacks_service_spec.rb'
- 'spec/services/preservation_ingest_service_spec.rb'
- 'spec/services/preservation_metadata_extractor_spec.rb'
- 'spec/services/version_service_spec.rb'
Expand Down Expand Up @@ -139,12 +123,6 @@ Rails/TimeZone:
Exclude:
- 'spec/services/version_service_spec.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/CombinableLoops:
Exclude:
- 'app/services/digital_stacks_service.rb'

# Offense count: 15
# Configuration parameters: AllowedConstants.
Style/Documentation:
Expand Down
38 changes: 19 additions & 19 deletions app/services/was_shelving_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

# Copy shelve-able web archives files into web archiving stacks
class WasShelvingService
class ConfigurationError < RuntimeError; end
class WasShelvingError < StandardError; end

def self.shelve(cocina_object)
new(cocina_object).shelve
end

def initialize(cocina_object)
raise ConfigurationError, 'Missing configuration Settings.stacks.local_workspace_root' if Settings.stacks.local_workspace_root.nil?
raise WasShelvingService::WasShelvingError, 'Missing structural' if cocina_object.structural.nil?
raise StandardError, 'Missing configuration Settings.stacks.local_workspace_root' if Settings.stacks.local_workspace_root.nil?

@cocina_object = cocina_object
@druid = cocina_object.externalIdentifier
Expand All @@ -20,31 +18,35 @@ def initialize(cocina_object)
attr_reader :cocina_object, :druid

def shelve
# determine destination web archiving stacks location
stacks_druid = DruidTools::StacksDruid.new(druid, was_stacks_location)
stacks_object_pathname = Pathname(stacks_druid.path)

# determine current workspace location of object's content file
workspace_druid = DruidTools::Druid.new(druid, Settings.stacks.local_workspace_root)
workspace_content_pathname = Pathname(workspace_druid.content_dir(true))

# get the list of shelvable files from cocina and shelve those that are available in the workspace
files_for(cocina_object).each do |file|
workspace_pathname = workspace_content_pathname.join(file)
stacks_pathname = stacks_object_pathname.join(file)
copy_file(workspace_pathname, stacks_pathname) if File.exist?(workspace_pathname)
files_for.each do |file|
workspace_pathname = workspace_content_dir.join(file)
was_stacks_pathname = was_stacks_dir.join(file)
copy_file(workspace_pathname, was_stacks_pathname) if File.exist?(workspace_pathname)
end
end

def was_stacks_dir
# determine destination web archiving stacks directory
stacks_druid = DruidTools::StacksDruid.new(druid, was_stacks_location)
Pathname(stacks_druid.path)
end

def was_stacks_location
collection_druid = cocina_object.structural&.isMemberOf&.first

raise WasShelvingService::WasShelvingError, 'Web archive object missing collection' unless collection_druid

"/web-archiving-stacks/data/collections/#{collection_druid.delete_prefix('druid:')}"
"#{Settings.stacks.web_archiving_stacks}/#{collection_druid.delete_prefix('druid:')}"
end

def workspace_content_dir
# determine current workspace location of object's content file
workspace_druid = DruidTools::Druid.new(druid, Settings.stacks.local_workspace_root)
Pathname(workspace_druid.content_dir(true))
end

def files_for(cocina_object)
def files_for
[].tap do |files_list|
cocina_object.structural.contains.each do |file_set|
file_set.structural.contains.each do |file|
Expand All @@ -59,7 +61,5 @@ def copy_file(workspace_pathname, stacks_pathname)

Rails.logger.debug("[Was Shelve] Copying #{workspace_pathname} to #{stacks_pathname}")
FileUtils.cp workspace_pathname.to_s, stacks_pathname.to_s
# Change permissions
FileUtils.chmod 'u=rw,go=r', stacks_pathname.to_s
end
end
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ stacks:
local_workspace_root: '/dor/workspace'
local_stacks_root: '/stacks'
local_document_cache_root: '/purl/document_cache'
web_archiving_stacks: '/web-archiving-stacks/data/collections'

# Suri
suri:
Expand Down
22 changes: 0 additions & 22 deletions spec/jobs/shelve_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,5 @@
workflow_process: 'shelve')
end
end

context 'with errors returned by WasShelvingService' do
let(:error_message) { 'missing structural' }

before do
allow(WasService).to receive(:crawl?).with(druid:).and_return(true)
allow(WasShelvingService).to receive(:shelve).and_raise(WasShelvingService::WasShelvingError, error_message)
allow(LogFailureJob).to receive(:perform_later)
end

it 'marks the job as errored' do
perform
expect(result).to have_received(:processing!).once
expect(WasShelvingService).to have_received(:shelve).with(cocina_object).once
expect(LogFailureJob).to have_received(:perform_later)
.with(druid:,
background_job_result: result,
workflow: 'accessionWF',
workflow_process: 'shelve',
output: { errors: [{ title: 'Unable to shelve web archive files', detail: error_message }] })
end
end
end
end
13 changes: 2 additions & 11 deletions spec/services/was_shelving_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
let(:workspace_filename) { workspace.join('bc', '123', 'df', '4567', 'bc123df4567', 'content', filename) }

before do
allow_any_instance_of(described_class).to receive(:was_stacks_location).and_return(tmp_was_stacks_location)
allow_any_instance_of(described_class).to receive(:was_stacks_location).and_return(tmp_was_stacks_location) # rubocop:disable RSpec/AnyInstance
end

it 'copies the files to stacks' do
Expand All @@ -88,22 +88,13 @@

before do
was_stacks_dir.rmtree if was_stacks_dir.exist?
allow_any_instance_of(described_class).to receive(:was_stacks_location).and_return(tmp_was_stacks_location)
allow_any_instance_of(described_class).to receive(:was_stacks_location).and_return(tmp_was_stacks_location) # rubocop:disable RSpec/AnyInstance
end

it 'creates the directory' do
expect(was_stacks_dir).not_to exist
described_class.shelve(cocina_object)
expect(was_stacks_dir).to exist
expect(File::Stat.new(was_stacks_destination.to_s).mode.to_fs(8)).to eq('100644')
end
end

context 'when no cocina structural' do
let(:structural) { nil }

it 'raises' do
expect { described_class.shelve(cocina_object) }.to raise_error(WasShelvingService::WasShelvingError)
end
end

Expand Down

0 comments on commit ae974e7

Please sign in to comment.