diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3df1861055..b931ca3bb4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2019-04-24 09:41:45 -0500 using RuboCop version 0.65.0. +# on 2019-04-26 16:20:32 -0500 using RuboCop version 0.65.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -31,7 +31,7 @@ Lint/UriEscapeUnescape: # Offense count: 19 Metrics/AbcSize: - Max: 119 + Max: 118 # Offense count: 2 # Configuration parameters: CountComments. @@ -113,10 +113,11 @@ RSpec/ContextWording: - 'spec/services/registration_service_spec.rb' - 'spec/services/version_service_spec.rb' -# Offense count: 1 +# Offense count: 2 RSpec/DescribeClass: Exclude: - 'spec/requests/about_spec.rb' + - 'spec/requests/metadata_refresh.rb' # Offense count: 32 # Cop supports --auto-correct. @@ -167,7 +168,7 @@ RSpec/ExpectInHook: - 'spec/services/publish_metadata_service_spec.rb' - 'spec/services/registration_service_spec.rb' -# Offense count: 139 +# Offense count: 123 # Configuration parameters: AssignmentOnly. RSpec/InstanceVariable: Exclude: @@ -189,7 +190,7 @@ RSpec/MessageSpies: - 'spec/services/registration_service_spec.rb' - 'spec/services/version_service_spec.rb' -# Offense count: 75 +# Offense count: 76 # Configuration parameters: AggregateFailuresByDefault. RSpec/MultipleExpectations: Max: 8 @@ -232,7 +233,7 @@ RSpec/SubjectStub: Exclude: - 'spec/dor/update_marc_record_service_spec.rb' -# Offense count: 12 +# Offense count: 11 # Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. RSpec/VerifiedDoubles: Exclude: diff --git a/app/controllers/metadata_refresh_controller.rb b/app/controllers/metadata_refresh_controller.rb new file mode 100644 index 0000000000..70bdb560b9 --- /dev/null +++ b/app/controllers/metadata_refresh_controller.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# Gets the catkey or barcode from identityMetadata and returns the catalog info +class MetadataRefreshController < ApplicationController + before_action :load_item + + def refresh + status = RefreshMetadataAction.run(@item) + @item.save if status + end +end diff --git a/app/services/refresh_metadata_action.rb b/app/services/refresh_metadata_action.rb new file mode 100644 index 0000000000..4ccb994ad3 --- /dev/null +++ b/app/services/refresh_metadata_action.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +# Look into identityMetadata for compliant ids and use them to fetch +# descriptive metadata from Symphony. Put the fetched value in the descMetadata +class RefreshMetadataAction + def self.run(object) + new(object).run(object.descMetadata) + end + + def initialize(object) + @object = object + end + + # Returns nil if it didn't retrieve anything + def run(datastream) + content = fetch_datastream + return nil if content.nil? + + datastream.dsLabel = 'Descriptive Metadata' + datastream.ng_xml = Nokogiri::XML(content) + datastream.ng_xml.normalize_text! + datastream.content = datastream.ng_xml.to_xml + end + + private + + def fetch_datastream + candidates = @object.identityMetadata.otherId.collect(&:to_s) + metadata_id = Dor::MetadataService.resolvable(candidates).first + metadata_id.nil? ? nil : Dor::MetadataService.fetch(metadata_id.to_s) + end +end diff --git a/config/routes.rb b/config/routes.rb index 06287db996..873ceca02b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,8 @@ post 'release_tags' post 'apo_workflows/:wf_name', action: 'apo_workflows' + post 'refresh_metadata', to: 'metadata_refresh#refresh' + get 'contents', to: 'content#list' get 'contents/*path', to: 'content#read', format: false, as: :read_content end diff --git a/spec/requests/metadata_refresh.rb b/spec/requests/metadata_refresh.rb new file mode 100644 index 0000000000..987f860d95 --- /dev/null +++ b/spec/requests/metadata_refresh.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Refresh metadata' do + let(:user) { Settings.DOR.SERVICE_USER } + let(:password) { Settings.DOR.SERVICE_PASSWORD } + let(:basic_auth) { ActionController::HttpAuthentication::Basic.encode_credentials(user, password) } + let(:object) { Dor::Item.new(pid: 'druid:1234') } + + before do + allow(Dor).to receive(:find).and_return(object) + allow(RefreshMetadataAction).to receive(:run) + allow(object).to receive(:save) + end + + it 'updates the metadata and saves the changes' do + post '/v1/objects/druid:mk420bs7601/refresh_metadata', + headers: { 'Authorization' => basic_auth } + expect(response).to be_successful + expect(RefreshMetadataAction).to have_received(:run).with(object) + expect(object).to have_received(:save) + end +end diff --git a/spec/services/refresh_metadata_action_spec.rb b/spec/services/refresh_metadata_action_spec.rb new file mode 100644 index 0000000000..853c99b18d --- /dev/null +++ b/spec/services/refresh_metadata_action_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe RefreshMetadataAction do + subject(:refresh) { described_class.run(item) } + + let(:item) { Dor::Item.new } + + before do + allow(item.identityMetadata).to receive(:otherId).and_return(['catkey:123']) + allow(Dor::MetadataService).to receive(:fetch).and_return('') + end + + it 'gets the data an puts it in descMetadata' do + refresh + expect(item.descMetadata.content).to eq '' + end +end