Class: PurlFetcher::Client::PublicXmlRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/purl_fetcher/client/public_xml_record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(druid, options = {}) ⇒ PublicXmlRecord

Returns a new instance of PublicXmlRecord.



20
21
22
23
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 20

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

Instance Attribute Details

#druidObject (readonly)

Returns the value of attribute druid.



8
9
10
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 8

def druid
  @druid
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 8

def options
  @options
end

Class Method Details

.fetch(url) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 10

def self.fetch(url)
  if defined?(JRUBY_VERSION)
    response = Manticore.get(url)
    response.body if response.code == 200
  else
    response = HTTP.get(url)
    response.body if response.status.ok?
  end
end

Instance Method Details

#catkeyObject

Returns catkey value from the DOR identity_metadata, or nil if there is no catkey.

Returns:

  • catkey value from the DOR identity_metadata, or nil if there is no catkey



30
31
32
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 30

def catkey
  get_value(public_xml_doc.xpath("/publicObject/identityMetadata/otherId[@name='catkey']"))
end

#collectionsObject



131
132
133
134
135
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 131

def collections
  @collections ||= predicate_druids('isMemberOfCollection').map do |druid|
    PublicXmlRecord.new(druid, options)
  end
end

#constituentsObject



137
138
139
140
141
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 137

def constituents
  @constituents ||= predicate_druids('isConstituentOf').map do |druid|
    PublicXmlRecord.new(druid, options)
  end
end

#dor_content_typeString

the value of the type attribute for a DOR object’s contentMetadata

more info about these values is here:
  https://consul.stanford.edu/display/chimera/DOR+content+types%2C+resource+types+and+interpretive+metadata
  https://consul.stanford.edu/display/chimera/Summary+of+Content+Types%2C+Resource+Types+and+their+behaviors

Returns:

  • (String)


99
100
101
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 99

def dor_content_type
  public_xml_doc.xpath('//contentMetadata/@type').text
end

#druid_treeObject



170
171
172
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 170

def druid_tree
  druid.match(/(..)(...)(..)(....)/).captures.join('/')
end

#encoded_thumbString

the thumbnail in publicXML properly URI encoded, including the slash separator

Returns:

  • (String)

    thumb filename with druid prepended, e.g. oo000oo0001%2Ffilename%20withspace.jp2



151
152
153
154
155
156
157
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 151

def encoded_thumb
  thumb=parse_thumb
  return unless thumb
  thumb_druid=thumb.split('/').first # the druid (before the first slash)
  thumb_filename=thumb.split(/[a-zA-Z]{2}[0-9]{3}[a-zA-Z]{2}[0-9]{4}[\/]/).last # everything after the druid
  "#{thumb_druid}%2F#{ERB::Util.url_encode(thumb_filename)}"
end

#get_value(node) ⇒ Object



39
40
41
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 39

def get_value(node)
  (node && node.first) ? node.first.content : nil
end

#is_collectionObject

Returns true if the identityMetadata has <objectType>collection</objectType>, false otherwise.

Returns:

  • true if the identityMetadata has <objectType>collection</objectType>, false otherwise



81
82
83
84
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 81

def is_collection
  object_type_nodes = public_xml_doc.xpath('//objectType')
  object_type_nodes.find_index { |n| %w(collection set).include? n.text.downcase }
end

#items(&block) ⇒ Object



143
144
145
146
147
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 143

def items(&block)
  return [] unless is_collection

  purl_fetcher_client.collection_members(druid, &block)
end

#labelObject

Returns objectLabel value from the DOR identity_metadata, or nil if there is no barcode.

Returns:

  • objectLabel value from the DOR identity_metadata, or nil if there is no barcode



35
36
37
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 35

def label
  get_value(public_xml_doc.xpath('/publicObject/identityMetadata/objectLabel'))
end

#modsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 65

def mods
  @mods ||= if public_xml_doc.xpath('/publicObject/mods:mods', mods: 'http://www.loc.gov/mods/v3').any?
    public_xml_doc.xpath('/publicObject/mods:mods', mods: 'http://www.loc.gov/mods/v3').first
  else
    if defined?(Honeybadger)
      Honeybadger.notify(
        'Unable to find MODS in the public xml; falling back to stand-along mods document',
        context: { druid: druid }
      )
    end

    Nokogiri::XML(self.class.fetch(purl_base_url + "/#{druid}.mods"))
  end
end

#mods_displayObject



49
50
51
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 49

def mods_display
  @mods_display ||= ModsDisplay::HTML.new(stanford_mods)
end

#parse_sw_image_idsArray<String>

the druid and id attribute of resource/file and objectId and fileId of the resource/externalFile elements that match the image, page, or thumb resource type, including extension Also, prepends the corresponding druid and / specifically for Searchworks use

Returns:

  • (Array<String>)

    filenames



123
124
125
126
127
128
129
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 123

def parse_sw_image_ids
  public_xml_doc.xpath('//resource[@type="page" or @type="image" or @type="thumb"]').map do |node|
    node.xpath('./file[@mimetype="image/jp2"]/@id').map{ |x| "#{@druid.gsub('druid:','')}/" + x } << node.xpath('./externalFile[@mimetype="image/jp2"]').map do |y|
      "#{y.attributes['objectId'].text.split(':').last}" + "/" + "#{y.attributes['fileId']}"
    end
  end.flatten
end

#parse_thumbString

Returns thumb filename with druid prepended, e.g. oo000oo0001/filename withspace.jp2.

Returns:

  • (String)

    thumb filename with druid prepended, e.g. oo000oo0001/filename withspace.jp2



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 105

def parse_thumb
  unless public_xml_doc.nil?
    thumb = public_xml_doc.xpath('//thumb')
    # first try and parse what is in the thumb node of publicXML, but fallback to the first image if needed
    if thumb.size == 1
      thumb.first.content
    elsif thumb.size == 0 && parse_sw_image_ids.size > 0
      parse_sw_image_ids.first
    else
      nil
    end
  end
end

#predicate_druids(predicate, predicate_ns = 'info:fedora/fedora-system:def/relations-external#') ⇒ Array<String>?

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

Returns:

  • (Array<String>, nil)

    the druids (e.g. ww123yy1234) from the rdf:resource of the predicate relationships, or nil if none



161
162
163
164
165
166
167
168
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 161

def predicate_druids(predicate, predicate_ns = 'info:fedora/fedora-system:def/relations-external#')
  ns_hash = { 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'pred_ns' => predicate_ns }
  xpth = "/publicObject/rdf:RDF/rdf:Description/pred_ns:#{predicate}/@rdf:resource"
  pred_nodes = public_xml_doc.xpath(xpth, ns_hash)
  pred_nodes.reject { |n| n.value.empty? }.map do |n|
    n.value.split('druid:').last
  end
end

#public?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 182

def public?
  rights.world_unrestricted?
end

#public_xmlObject



53
54
55
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 53

def public_xml
  @public_xml ||= self.class.fetch(purl_base_url + "/#{druid}.xml")
end

#public_xml?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 57

def public_xml?
  !!public_xml
end

#public_xml_docObject



61
62
63
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 61

def public_xml_doc
  @public_xml_doc ||= Nokogiri::XML(public_xml)
end

#purl_base_urlObject



190
191
192
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 190

def purl_base_url
  options[:purl_url]&.sub(%r{/$}, '') || 'https://purl.stanford.edu'
end

#purl_fetcher_api_endpointObject



194
195
196
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 194

def purl_fetcher_api_endpoint
  options[:purl_fetcher_url] || 'https://purl-fetcher.stanford.edu'
end

#purl_fetcher_clientObject



198
199
200
201
202
203
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 198

def purl_fetcher_client
  @purl_fetcher_client ||= PurlFetcher::Client::Reader.new(
    nil,
    'purl_fetcher.api_endpoint' => purl_fetcher_api_endpoint
  )
end

#rightsObject



178
179
180
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 178

def rights
  @rights ||= ::Dor::RightsAuth.parse(rights_xml)
end

#rights_xmlObject



174
175
176
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 174

def rights_xml
  @rights_xml ||= public_xml_doc.xpath('//rightsMetadata').to_s
end

#searchworks_idObject



25
26
27
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 25

def searchworks_id
  catkey.nil? ? druid : catkey
end

#stanford_modsObject



43
44
45
46
47
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 43

def stanford_mods
  @smods_rec ||= Stanford::Mods::Record.new.tap do |smods_rec|
    smods_rec.from_str(mods.to_s)
  end
end

#stanford_only?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 186

def stanford_only?
  rights.stanford_only_unrestricted?
end

#thumbString

value is used to tell SearchWorks UI app of specific display needs for objects this comes from the <thumb> element in publicXML or the first image found (as parsed by discovery-indexer)

Returns:

  • (String)

    filename or nil if none found



89
90
91
92
# File 'lib/purl_fetcher/client/public_xml_record.rb', line 89

def thumb
  return if is_collection
  encoded_thumb if %w(book image manuscript map webarchive-seed).include?(dor_content_type)
end