Module: CocinaDisplay::Concerns::Identifiers

Included in:
CocinaDisplay::CocinaRecord, RelatedResource
Defined in:
lib/cocina_display/concerns/identifiers.rb

Overview

Methods for extracting and formatting identifiers from Cocina records.

Instance Method Summary collapse

Instance Method Details

#bare_druidString?

Note:

A RelatedResource may not have a DRUID.

The DRUID for the object, without the druid: prefix.

Examples:

record.bare_druid #=> "bb099mt5053"

Returns:

  • (String, nil)


19
20
21
# File 'lib/cocina_display/concerns/identifiers.rb', line 19

def bare_druid
  druid&.delete_prefix("druid:")
end

#doiString?

The DOI for the object, if there is one – just the identifier part.

Examples:

record.doi #=> "10.25740/ppax-bf07"

Returns:

  • (String, nil)


27
28
29
# File 'lib/cocina_display/concerns/identifiers.rb', line 27

def doi
  identifiers.find(&:doi?)&.identifier
end

#doi_urlString?

The DOI as a URL, if there is one. Any valid DOI should resolve via doi.org.

Examples:

record.doi_url #=> "https://doi.org/10.25740/ppax-bf07"

Returns:

  • (String, nil)


35
36
37
# File 'lib/cocina_display/concerns/identifiers.rb', line 35

def doi_url
  URI.join("https://doi.org", doi).to_s if doi.present?
end

#druidString?

Note:

A RelatedResource may not have a DRUID.

The DRUID for the object, with the druid: prefix.

Examples:

record.druid #=> "druid:bb099mt5053"

Returns:

  • (String, nil)


10
11
12
# File 'lib/cocina_display/concerns/identifiers.rb', line 10

def druid
  cocina_doc["externalIdentifier"] || purl_url&.split("/")&.last
end

#folio_hrid(refresh: nil) ⇒ String? Also known as: catkey

Note:

This doesn’t imply the object is available in Searchworks at this ID.

The HRID of the item in FOLIO, if defined.

Examples:

With a link regardless of refresh:

record.folio_hrid #=> "a12845814"

With a link that is not refreshed:

record.folio_hrid(refresh: true) #=> nil

Parameters:

  • [Boolean] (refresh)

    Filter to links with refresh set to this value.

Returns:

  • (String, nil)


47
48
49
50
51
52
53
54
# File 'lib/cocina_display/concerns/identifiers.rb', line 47

def folio_hrid(refresh: nil)
  link = path("$.identification.catalogLinks[?(@.catalog == 'folio')]").first
  hrid = link&.dig("catalogRecordId")
  return if hrid.blank?
  return hrid if refresh.nil?

  (link["refresh"] == refresh) ? hrid : nil
end

#identifier_display_dataArray<DisplayData>

Labelled display data for identifiers.

Returns:



76
77
78
# File 'lib/cocina_display/concerns/identifiers.rb', line 76

def identifier_display_data
  CocinaDisplay::DisplayData.from_objects(identifiers)
end

#identifiers(type: nil) ⇒ Array<Identifier>

Note:

Type matching is case insensitive.

All identifier objects, optionally filtered by type.

Parameters:

  • type (String, nil) (defaults to: nil)

    The type of identifier to filter by (e.g. “DOI”).

Returns:



70
71
72
# File 'lib/cocina_display/concerns/identifiers.rb', line 70

def identifiers(type: nil)
  type.present? ? all_identifiers.filter { |id| id.type&.casecmp?(type) } : all_identifiers
end

#searchworks_idString?

Note:

This doesn’t imply the object is available in Searchworks at this ID.

The FOLIO HRID if defined, otherwise the bare DRUID.

Returns:

  • (String, nil)

See Also:



62
63
64
# File 'lib/cocina_display/concerns/identifiers.rb', line 62

def searchworks_id
  folio_hrid || bare_druid
end