Class: CocinaDisplay::Identifier

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/identifier.rb

Overview

An identifier for an object or a descriptive value.

Constant Summary collapse

SOURCE_URIS =

Source URI values for common identifiers If you have the bare ID, you can always add it to these to make a valid URL

{
  "ORCID" => "https://orcid.org/",
  "ROR" => "https://ror.org/",
  "DOI" => "https://doi.org/",
  "ISNI" => "https://isni.org/"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina) ⇒ Identifier

Initialize an Identifier from Cocina structured data.

Parameters:

  • cocina (Hash)


17
18
19
# File 'lib/cocina_display/identifier.rb', line 17

def initialize(cocina)
  @cocina = cocina
end

Instance Attribute Details

#cocinaObject (readonly)

Returns the value of attribute cocina.



4
5
6
# File 'lib/cocina_display/identifier.rb', line 4

def cocina
  @cocina
end

Instance Method Details

#==(other) ⇒ Object



28
29
30
# File 'lib/cocina_display/identifier.rb', line 28

def ==(other)
  other.is_a?(Identifier) && other.cocina == cocina
end

#codeString?

The declared encoding of the identifier, if any.

Returns:

  • (String, nil)


65
66
67
# File 'lib/cocina_display/identifier.rb', line 65

def code
  cocina.dig("source", "code").presence
end

#doi?Boolean

Check if the identifier is a DOI. There are several indicators that could suggest this.

Returns:

  • (Boolean)


89
90
91
# File 'lib/cocina_display/identifier.rb', line 89

def doi?
  cocina["type"]&.match?(/doi/i) || code == "doi" || cocina["uri"]&.include?("://doi.org")
end

#identifierString?

The “identifying” part of the identifier. Tries to parse from the end of the URI.

Examples:

DOI

10.1234/doi

Returns:

  • (String, nil)


44
45
46
# File 'lib/cocina_display/identifier.rb', line 44

def identifier
  URI(value).path.delete_prefix("/") if value
end

#labelString

Label used to render the identifier for display. Uses a displayLabel if available, otherwise tries to look up via type. Falls back to a generic label for any unknown identifier types.

Returns:

  • (String)


81
82
83
84
# File 'lib/cocina_display/identifier.rb', line 81

def label
  cocina["displayLabel"].presence ||
    I18n.t(label_key, default: :identifier, scope: "cocina_display.field_label.identifier")
end

#scheme_uriString?

The base URI used to resolve the identifier, if any.

Examples:

DOI

https://doi.org/

Returns:

  • (String, nil)


73
74
75
# File 'lib/cocina_display/identifier.rb', line 73

def scheme_uri
  cocina.dig("source", "uri") || SOURCE_URIS[type]
end

#to_sString

String representation of the identifier. Prefers the URI representation where present.

Returns:

  • (String)


24
25
26
# File 'lib/cocina_display/identifier.rb', line 24

def to_s
  uri || value
end

#typeString?

The type of the identifier, e.g. “DOI”.

Returns:

  • (String, nil)


59
60
61
# File 'lib/cocina_display/identifier.rb', line 59

def type
  ("DOI" if doi?) || cocina["type"].presence
end

#uriString?

The identifier as a URI, if available. Tries to construct a URI if the parts are available to do so.

Examples:

DOI

https://doi.org/10.1234/doi

Returns:

  • (String, nil)


53
54
55
# File 'lib/cocina_display/identifier.rb', line 53

def uri
  cocina["uri"].presence || ([scheme_uri.delete_suffix("/"), identifier].join("/") if scheme_uri && identifier)
end

#valueString?

The raw value from the Cocina structured data. Prefers the URI representation where present.

Returns:

  • (String, nil)


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

def value
  cocina["uri"].presence || cocina["value"].presence
end