Class: CocinaDisplay::Identifier
- Inherits:
-
Object
- Object
- CocinaDisplay::Identifier
- 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
-
#cocina ⇒ Object
readonly
Returns the value of attribute cocina.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#code ⇒ String?
The declared encoding of the identifier, if any.
-
#doi? ⇒ Boolean
Check if the identifier is a DOI.
-
#identifier ⇒ String?
The “identifying” part of the identifier.
-
#initialize(cocina) ⇒ Identifier
constructor
Initialize an Identifier from Cocina structured data.
-
#label ⇒ String
Label used to render the identifier for display.
-
#scheme_uri ⇒ String?
The base URI used to resolve the identifier, if any.
-
#to_s ⇒ String
String representation of the identifier.
-
#type ⇒ String?
The type of the identifier, e.g.
-
#uri ⇒ String?
The identifier as a URI, if available.
-
#value ⇒ String?
The raw value from the Cocina structured data.
Constructor Details
#initialize(cocina) ⇒ Identifier
Initialize an Identifier from Cocina structured data.
17 18 19 |
# File 'lib/cocina_display/identifier.rb', line 17 def initialize(cocina) @cocina = cocina end |
Instance Attribute Details
#cocina ⇒ Object (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 |
#code ⇒ String?
The declared encoding of the identifier, if any.
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.
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 |
#identifier ⇒ String?
The “identifying” part of the identifier. Tries to parse from the end of the URI.
44 45 46 |
# File 'lib/cocina_display/identifier.rb', line 44 def identifier URI(value).path.delete_prefix("/") if value end |
#label ⇒ String
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.
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_uri ⇒ String?
The base URI used to resolve the identifier, if any.
73 74 75 |
# File 'lib/cocina_display/identifier.rb', line 73 def scheme_uri cocina.dig("source", "uri") || SOURCE_URIS[type] end |
#to_s ⇒ String
String representation of the identifier. Prefers the URI representation where present.
24 25 26 |
# File 'lib/cocina_display/identifier.rb', line 24 def to_s uri || value end |
#type ⇒ String?
The type of the identifier, e.g. “DOI”.
59 60 61 |
# File 'lib/cocina_display/identifier.rb', line 59 def type ("DOI" if doi?) || cocina["type"].presence end |
#uri ⇒ String?
The identifier as a URI, if available. Tries to construct a URI if the parts are available to do so.
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 |
#value ⇒ String?
The raw value from the Cocina structured data. Prefers the URI representation where present.
35 36 37 |
# File 'lib/cocina_display/identifier.rb', line 35 def value cocina["uri"].presence || cocina["value"].presence end |