lib/harvestdor/indexer/resource.rb in harvestdor-indexer-2.1.1 vs lib/harvestdor/indexer/resource.rb in harvestdor-indexer-2.2.0

- old
+ new

@@ -1,24 +1,25 @@ +# rubocop:disable Metrics/ClassLength require 'active_support/benchmarkable' module Harvestdor class Indexer::Resource include ActiveSupport::Benchmarkable attr_reader :indexer, :druid, :options # @param [Harvestdor::Indexer] indexer an instance of Harvestdor::Indexer - # @param [String] coll_druid a collection druid of the form 'druid:oo123oo1234' - def initialize indexer, coll_druid, options = {} + # @param [String] druid a druid of the form 'druid:oo123oo1234' + def initialize(indexer, druid, options = {}) @indexer = indexer - @druid = coll_druid + @druid = druid @options = options end # @return [String] string of form oo123oo1234 def bare_druid - @bare_druid ||= druid.gsub("druid:", "") + @bare_druid ||= druid.gsub('druid:', '') end ## # The harvestdor client used for retrieving resources def harvestdor_client @@ -36,49 +37,54 @@ end ## # Is this resource a collection? def collection? - identity_metadata.xpath("/identityMetadata/objectType").any? { |x| x.text == "collection" } + identity_metadata.xpath('/identityMetadata/objectType').any? { |x| x.text == 'collection' } end # get the druids from isMemberOfCollection relationships in rels-ext from public_xml # @return [Array<String>] the druids (e.g. ww123yy1234) this object has isMemberOfColletion relationship with, or nil if none def collections @collections ||= begin - ns_hash = {'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'fedora' => "info:fedora/fedora-system:def/relations-external#", '' => ''} + ns_hash = { 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'fedora' => 'info:fedora/fedora-system:def/relations-external#', '' => '' } is_member_of_nodes ||= public_xml.xpath('/publicObject/rdf:RDF/rdf:Description/fedora:isMemberOfCollection/@rdf:resource', ns_hash) is_member_of_nodes.reject { |n| n.value.empty? }.map do |n| - Harvestdor::Indexer::Resource.new(indexer, n.value.gsub("info:fedora/", "")) + Harvestdor::Indexer::Resource.new(indexer, n.value.gsub('info:fedora/', '')) end end end ## # Return the items in this collection def items - @items ||= begin - druids = dor_fetcher_client.druid_array(dor_fetcher_client.get_collection(bare_druid, {})) - druids.map { |x| Harvestdor::Indexer::Resource.new(indexer, x) } + return [] unless collection? + return to_enum(:items) unless block_given? + + items_druids.each do |x| + yield Harvestdor::Indexer::Resource.new(indexer, x) end end + def items_druids + @items_druids ||= dor_fetcher_client.druid_array(dor_fetcher_client.get_collection(bare_druid, {})) + end + # given a druid, get its objectLabel from its purl page identityMetadata # @return [String] the value of the <objectLabel> element in the identityMetadata for the object def identity_md_obj_label logger.error("#{druid} missing identityMetadata") unless identity_metadata identity_metadata.xpath('identityMetadata/objectLabel').text end - # return the MODS for the druid as a Stanford::Mods::Record object # @return [Stanford::Mods::Record] created from the MODS xml for the druid def smods_rec @smods_rec ||= benchmark "smods_rec(#{druid})", level: :debug do ng_doc = mods - raise "Empty MODS metadata for #{druid}: #{ng_doc.to_xml}" if ng_doc.root.xpath('//text()').empty? + fail "Empty MODS metadata for #{druid}: #{ng_doc.to_xml}" if ng_doc.root.xpath('//text()').empty? mods_rec = Stanford::Mods::Record.new mods_rec.from_nk_node(ng_doc.root) mods_rec end end @@ -90,12 +96,12 @@ # the public xml for this DOR object, from the purl page # @return [Nokogiri::XML::Document] the public xml for the DOR object def public_xml @public_xml ||= benchmark "public_xml(#{druid})", level: :debug do ng_doc = harvestdor_client.public_xml bare_druid - raise "No public xml for #{druid}" if !ng_doc - raise "Empty public xml for #{druid}: #{ng_doc.to_xml}" if ng_doc.root.xpath('//text()').empty? + fail "No public xml for #{druid}" unless ng_doc + fail "Empty public xml for #{druid}: #{ng_doc.to_xml}" if ng_doc.root.xpath('//text()').empty? ng_doc end end ## @@ -119,45 +125,45 @@ # @return [Nokogiri::XML::Document] the contentMetadata for the DOR object def content_metadata ng_doc = benchmark "content_metadata (#{druid})", level: :debug do harvestdor_client.content_metadata public_xml_or_druid end - raise "No contentMetadata for \"#{druid}\"" if !ng_doc || ng_doc.children.empty? + fail "No contentMetadata for \"#{druid}\"" if !ng_doc || ng_doc.children.empty? ng_doc end # the identityMetadata for this DOR object, ultimately from the purl public xml # @return [Nokogiri::XML::Document] the identityMetadata for the DOR object def identity_metadata ng_doc = benchmark "identity_metadata (#{druid})", level: :debug do harvestdor_client.identity_metadata public_xml_or_druid end - raise "No identityMetadata for \"#{druid}\"" if !ng_doc || ng_doc.children.empty? + fail "No identityMetadata for \"#{druid}\"" if !ng_doc || ng_doc.children.empty? ng_doc end # the rightsMetadata for this DOR object, ultimately from the purl public xml # @return [Nokogiri::XML::Document] the rightsMetadata for the DOR object def rights_metadata ng_doc = benchmark "rights_metadata (#{druid})", level: :debug do harvestdor_client.rights_metadata public_xml_or_druid end - raise "No rightsMetadata for \"#{druid}\"" if !ng_doc || ng_doc.children.empty? + fail "No rightsMetadata for \"#{druid}\"" if !ng_doc || ng_doc.children.empty? ng_doc end # the RDF for this DOR object, ultimately from the purl public xml # @return [Nokogiri::XML::Document] the RDF for the DOR object def rdf ng_doc = benchmark "rdf (#{druid})", level: :debug do harvestdor_client.rdf public_xml_or_druid end - raise "No RDF for \"#{druid}\"" if !ng_doc || ng_doc.children.empty? + fail "No RDF for \"#{druid}\"" if !ng_doc || ng_doc.children.empty? ng_doc end def eql?(other) - other.is_a? Harvestdor::Indexer::Resource and other.indexer == indexer and other.druid == druid + other.is_a?(Harvestdor::Indexer::Resource) && other.indexer == indexer && other.druid == druid end def hash druid.hash ^ indexer.hash end \ No newline at end of file