Skip to content

Commit

Permalink
Merge 330420a into 8e938d1
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Apr 26, 2019
2 parents 8e938d1 + 330420a commit 1acadeb
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
13 changes: 7 additions & 6 deletions .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
Expand Down Expand Up @@ -31,7 +31,7 @@ Lint/UriEscapeUnescape:

# Offense count: 19
Metrics/AbcSize:
Max: 119
Max: 118

# Offense count: 2
# Configuration parameters: CountComments.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions 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
32 changes: 32 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions 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
19 changes: 19 additions & 0 deletions 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('<xml/>')
end

it 'gets the data an puts it in descMetadata' do
refresh
expect(item.descMetadata.content).to eq '<xml/>'
end
end

0 comments on commit 1acadeb

Please sign in to comment.