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)


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

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)


100
101
102
# File 'lib/cocina_display/identifier.rb', line 100

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
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cocina_display/identifier.rb', line 44

def identifier
  # the uri property is a valid uri, but the value isn't necessarily a valid uri
  uri = if cocina["uri"]
    URI(cocina["uri"])
  elsif cocina["value"]
    begin
      URI(cocina["value"])
    rescue URI::InvalidURIError
      nil
    end
  end

  uri&.path&.delete_prefix("/")
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)


92
93
94
95
# File 'lib/cocina_display/identifier.rb', line 92

def label
  cocina["displayLabel"].presence ||
    I18n.t(label_key, default: default_label, 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)


84
85
86
# File 'lib/cocina_display/identifier.rb', line 84

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)


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

def type
  ("DOI" if doi?) || ("ORCID" if orcid_uri?) || 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)


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

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"] || cocina["value"]
end