Class: Harvestdor::Indexer::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Benchmarkable
Defined in:
lib/harvestdor/indexer/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indexer, druid, options = {}) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • indexer (Harvestdor::Indexer)

    an instance of Harvestdor::Indexer

  • druid (String)

    a druid of the form ‘druid:oo123oo1234’



11
12
13
14
15
# File 'lib/harvestdor/indexer/resource.rb', line 11

def initialize(indexer, druid, options = {})
  @indexer = indexer
  @druid = druid
  @options = options
end

Instance Attribute Details

#druidObject (readonly)

Returns the value of attribute druid.



7
8
9
# File 'lib/harvestdor/indexer/resource.rb', line 7

def druid
  @druid
end

#indexerObject (readonly)

Returns the value of attribute indexer.



7
8
9
# File 'lib/harvestdor/indexer/resource.rb', line 7

def indexer
  @indexer
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/harvestdor/indexer/resource.rb', line 7

def options
  @options
end

Instance Method Details

#bare_druidString

Returns string of form oo123oo1234.

Returns:

  • (String)

    string of form oo123oo1234



26
27
28
# File 'lib/harvestdor/indexer/resource.rb', line 26

def bare_druid
  @bare_druid ||= druid.gsub('druid:', '')
end

#collection?Boolean

Is this resource a collection?

Returns:

  • (Boolean)


58
59
60
# File 'lib/harvestdor/indexer/resource.rb', line 58

def collection?
  .xpath('/identityMetadata/objectType').any? { |x| x.text == 'collection' }
end

#collectionsArray<String>

get the druids from isMemberOfCollection relationships in rels-ext from public_xml

Returns:

  • (Array<String>)

    the druids (e.g. ww123yy1234) this object has isMemberOfColletion relationship with, or nil if none



64
65
66
67
68
69
70
71
72
73
# File 'lib/harvestdor/indexer/resource.rb', line 64

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#', '' => '' }
    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/', ''))
    end
  end
end

#content_metadataNokogiri::XML::Document

the contentMetadata for this DOR object, ultimately from the purl public xml

Returns:

  • (Nokogiri::XML::Document)

    the contentMetadata for the DOR object



149
150
151
152
153
# File 'lib/harvestdor/indexer/resource.rb', line 149

def 
  @content_metadata ||= benchmark "content_metadata (#{druid})", level: :debug do
    harvestdor_client. public_xml
  end
end

#dor_fetcher_clientObject



36
37
38
# File 'lib/harvestdor/indexer/resource.rb', line 36

def dor_fetcher_client
  indexer.dor_fetcher_client
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/harvestdor/indexer/resource.rb', line 179

def eql?(other)
  other.is_a?(Harvestdor::Indexer::Resource) && other.indexer == indexer && other.druid == druid
end

#exists?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/harvestdor/indexer/resource.rb', line 50

def exists?
  public_xml
rescue Harvestdor::Errors::MissingPublicXml, Harvestdor::Errors::MissingPurlPage
  false
end

#harvestdor_clientObject

The harvestdor client used for retrieving resources



32
33
34
# File 'lib/harvestdor/indexer/resource.rb', line 32

def harvestdor_client
  indexer.harvestdor_client
end

#hashObject



183
184
185
# File 'lib/harvestdor/indexer/resource.rb', line 183

def hash
  druid.hash ^ indexer.hash
end

#identity_md_obj_labelString

given a druid, get its objectLabel from its purl page identityMetadata

Returns:

  • (String)

    the value of the <objectLabel> element in the identityMetadata for the object



99
100
101
102
# File 'lib/harvestdor/indexer/resource.rb', line 99

def identity_md_obj_label
  logger.error("#{druid} missing identityMetadata") unless 
  .xpath('identityMetadata/objectLabel').text
end

#identity_metadataNokogiri::XML::Document

the identityMetadata for this DOR object, ultimately from the purl public xml

Returns:

  • (Nokogiri::XML::Document)

    the identityMetadata for the DOR object



157
158
159
160
161
# File 'lib/harvestdor/indexer/resource.rb', line 157

def 
  @identity_metadata ||= benchmark "identity_metadata (#{druid})", level: :debug do
    harvestdor_client. public_xml
  end
end

#itemsObject

Return the items in this collection



77
78
79
80
81
82
83
84
85
86
# File 'lib/harvestdor/indexer/resource.rb', line 77

def items
  return [] unless collection?

  # return an enumerator, with an estimated size of the collection
  return to_enum(:items) { items_druids.count } unless block_given?

  items_druids.each do |x|
    yield Harvestdor::Indexer::Resource.new(indexer, x)
  end
end

#items_druidsObject



88
89
90
91
92
93
94
95
# File 'lib/harvestdor/indexer/resource.rb', line 88

def items_druids
  if purl_fetcher_client
    # we don't need to memoize purl_fetcher_client, since it natively uses enumerables
    purl_fetcher_client.druids_from_collection(namespaced_druid)
  else
    @items_druids ||= dor_fetcher_client.druid_array(dor_fetcher_client.get_collection(bare_druid, {}))
  end
end

#loggerObject

Get the logger



46
47
48
# File 'lib/harvestdor/indexer/resource.rb', line 46

def logger
  options[:logger] || (indexer.logger if indexer.respond_to? :logger) || Logger.new(STDERR)
end

#modsObject



116
117
118
# File 'lib/harvestdor/indexer/resource.rb', line 116

def mods
  @mods ||= harvestdor_client.mods bare_druid
end

#namespaced_druidObject



17
18
19
20
21
22
23
# File 'lib/harvestdor/indexer/resource.rb', line 17

def namespaced_druid
  if druid =~ /^druid:/
    druid
  else
    "druid:#{druid}"
  end
end

#public_xmlNokogiri::XML::Document

the public xml for this DOR object, from the purl page

Returns:

  • (Nokogiri::XML::Document)

    the public xml for the DOR object



122
123
124
125
126
# File 'lib/harvestdor/indexer/resource.rb', line 122

def public_xml
  @public_xml ||= benchmark "public_xml(#{druid})", level: :debug do
    harvestdor_client.public_xml bare_druid
  end
end

#public_xml?Boolean

Deprecated.

Has the public_xml been previously retrieved?

Returns:

  • (Boolean)


131
132
133
# File 'lib/harvestdor/indexer/resource.rb', line 131

def public_xml?
  !!@public_xml
end

#public_xml_or_druidObject

Deprecated.

Get the public_xml, if retrieved, or the druid. This is used to short-circuit retrieving metadata out of the public xml.



139
140
141
142
143
144
145
# File 'lib/harvestdor/indexer/resource.rb', line 139

def public_xml_or_druid
  if public_xml?
    public_xml
  else
    bare_druid
  end
end

#purl_fetcher_clientObject



40
41
42
# File 'lib/harvestdor/indexer/resource.rb', line 40

def purl_fetcher_client
  indexer.purl_fetcher_client
end

#rdfNokogiri::XML::Document

the RDF for this DOR object, ultimately from the purl public xml

Returns:

  • (Nokogiri::XML::Document)

    the RDF for the DOR object



173
174
175
176
177
# File 'lib/harvestdor/indexer/resource.rb', line 173

def rdf
  @rdf ||= benchmark "rdf (#{druid})", level: :debug do
    harvestdor_client.rdf public_xml
  end
end

#rights_metadataNokogiri::XML::Document

the rightsMetadata for this DOR object, ultimately from the purl public xml

Returns:

  • (Nokogiri::XML::Document)

    the rightsMetadata for the DOR object



165
166
167
168
169
# File 'lib/harvestdor/indexer/resource.rb', line 165

def 
  @rights_metadata ||= benchmark "rights_metadata (#{druid})", level: :debug do
    harvestdor_client. public_xml
  end
end

#smods_recStanford::Mods::Record

return the MODS for the druid as a Stanford::Mods::Record object

Returns:

  • (Stanford::Mods::Record)

    created from the MODS xml for the druid



106
107
108
109
110
111
112
113
114
# File 'lib/harvestdor/indexer/resource.rb', line 106

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?
    mods_rec = Stanford::Mods::Record.new
    mods_rec.from_nk_node(ng_doc.root)
    mods_rec
  end
end