diff --git a/lib/collectionspace/mapper/response.rb b/lib/collectionspace/mapper/response.rb index 93d256f0..435eec26 100644 --- a/lib/collectionspace/mapper/response.rb +++ b/lib/collectionspace/mapper/response.rb @@ -6,7 +6,7 @@ class Response attr_reader :orig_data, :doc, :record_status, :csid, :uri, :refname, :terms, :errors, :warnings, - :identifier, :merged_data, :xpaths, + :merged_data, :xpaths, :split_data, :transformed_data attr_accessor :combined_data @@ -46,6 +46,15 @@ def add_identifier(id) @identifier = id end + def identifier + return @identifier unless @identifier.empty? + + id_field = handler.record.identifier_field.downcase + return @identifier unless transformed_data.key?(id_field) + + transformed_data[id_field][0] + end + def add_error(error) errors << error @errors = errors.flatten.compact diff --git a/spec/collectionspace/mapper/response_spec.rb b/spec/collectionspace/mapper/response_spec.rb index d8a89374..3c2d122b 100644 --- a/spec/collectionspace/mapper/response_spec.rb +++ b/spec/collectionspace/mapper/response_spec.rb @@ -192,8 +192,8 @@ let(:customcfg) { {delimiter: "|", response_mode: "verbose"} } let(:processed) { response.map } - context "with some terms found and some terms not found" do - let(:result) { processed.terms.reject { |t| t.found? } } + context "with terms found in cache and instance" do + let(:result) { processed.terms.select { |t| t.missing? } } vcr_found_opts = { cassette_name: "datahandler_uncached_found_terms", @@ -211,8 +211,9 @@ } end - it "returns expected found values" do + it "returns no missing terms" do expect(result.length).to eq(0) + expect(processed.identifier).to eq("20CS.001.0002") end end @@ -220,7 +221,7 @@ cassette_name: "datahandler_uncached_unfound_terms", record: :new_episodes } - context "with terms in instance but not in cache, and not in instance", + context "with found term and missing term", vcr: vcr_unfound_opts do let(:data) do { @@ -232,8 +233,9 @@ } end - it "returns expected found values" do + it "returns 1 missing term" do expect(result.length).to eq(1) + expect(processed.identifier).to eq("20CS.001.0001") end end end @@ -250,6 +252,9 @@ # authority - in instance, not in cache "namedCollection" => "QA TARGET Work" } + resp1 = CollectionSpace::Mapper::Response.new(data1, handler) + handler.process(resp1) + data2 = { "objectNumber" => "2", # vocabulary - now in cache @@ -259,16 +264,11 @@ # authority - not in instance, not in cache "contentConceptAssociated" => "Birbs" } - - resp1 = CollectionSpace::Mapper::Response.new(data1, handler) resp2 = CollectionSpace::Mapper::Response.new(data2, handler) - - handler.process(resp1) - result = handler.process(resp2) - .terms - .select { |t| !t.found? } - expect(result.length).to eq(1) + + expect(result.terms.count(&:missing?)).to eq(1) + expect(result.identifier).to eq("2") end end