Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('<xml/>') | ||
end | ||
|
||
it 'gets the data an puts it in descMetadata' do | ||
refresh | ||
expect(item.descMetadata.content).to eq '<xml/>' | ||
end | ||
end |