Class: CocinaDisplay::CocinaRecord

Overview

Public Cocina metadata for an SDR object, as fetched from PURL.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CocinaDisplay::Concerns::Languages

#languages, #searchworks_language_names

Methods included from CocinaDisplay::Concerns::Forms

#archived_website?, #cartographic?, #dataset?, #extents, #forms, #genres, #genres_search, #periodical?, #resource_types

Methods included from CocinaDisplay::Concerns::Subjects

#subject_all, #subject_all_display, #subject_genres, #subject_names, #subject_occupations, #subject_other, #subject_places, #subject_temporal, #subject_temporal_genre, #subject_titles, #subject_topics, #subject_topics_other

Methods included from CocinaDisplay::Concerns::Access

#download_url, #iiif_manifest_url, #oembed_url, #purl_base_url, #purl_url, #stacks_base_url

Methods included from CocinaDisplay::Concerns::Titles

#additional_titles, #display_title, #full_title, #main_title, #sort_title

Methods included from CocinaDisplay::Concerns::Identifiers

#bare_druid, #doi, #doi_url, #druid, #folio_hrid, #searchworks_id

Methods included from CocinaDisplay::Concerns::Contributors

#additional_contributor_names, #additional_contributors, #conference_contributor_names, #contributor_names_by_role, #contributors, #impersonal_contributor_names, #main_contributor, #main_contributor_name, #organization_contributor_names, #person_contributor_names, #publisher_contributors, #publisher_names, #sort_contributor_name

Methods included from CocinaDisplay::Concerns::Events

#earliest_preferred_date, #event_dates, #events, #imprint_display_str, #imprint_events, #pub_date, #pub_date_edtf, #pub_year_display_str, #pub_year_int, #pub_year_int_range, #publication_events, #publication_places

Constructor Details

#initialize(cocina_doc) ⇒ CocinaRecord

Initialize a CocinaRecord with a Cocina document hash.

Parameters:

  • cocina_doc (Hash)


58
59
60
# File 'lib/cocina_display/cocina_record.rb', line 58

def initialize(cocina_doc)
  @cocina_doc = cocina_doc
end

Instance Attribute Details

#cocina_docHash (readonly)

The parsed Cocina document.

Returns:

  • (Hash)


54
55
56
# File 'lib/cocina_display/cocina_record.rb', line 54

def cocina_doc
  @cocina_doc
end

Class Method Details

.fetch(druid, deep_compact: false) ⇒ CocinaDisplay::CocinaRecord

Note:

This is intended to be used in development or testing only.

Fetch a public Cocina document from PURL and create a CocinaRecord. :nocov:

Parameters:

  • druid (String)

    The bare DRUID of the object to fetch.

  • deep_compact (Boolean) (defaults to: false)

    If true, compact the JSON to remove blank values.

Returns:



38
39
40
# File 'lib/cocina_display/cocina_record.rb', line 38

def self.fetch(druid, deep_compact: false)
  from_json(Net::HTTP.get(URI("https://purl.stanford.edu/#{druid}.json")), deep_compact: deep_compact)
end

.from_json(cocina_json, deep_compact: false) ⇒ CocinaDisplay::CocinaRecord

Create a CocinaRecord from a JSON string.

Parameters:

  • cocina_json (String)
  • deep_compact (Boolean) (defaults to: false)

    If true, compact the JSON to remove blank values.

Returns:



47
48
49
50
# File 'lib/cocina_display/cocina_record.rb', line 47

def self.from_json(cocina_json, deep_compact: false)
  cocina_doc = JSON.parse(cocina_json)
  deep_compact ? new(Utils.deep_compact_blank(cocina_doc)) : new(cocina_doc)
end

Instance Method Details

#collection?Boolean

True if the object is a collection.

Returns:

  • (Boolean)


106
107
108
# File 'lib/cocina_display/cocina_record.rb', line 106

def collection?
  content_type == "collection"
end

#content_typeString

SDR content type of the object.

Examples:

record.content_type #=> "image"

Returns:

  • (String)

See Also:



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

def content_type
  cocina_doc["type"].split("/").last
end

#created_timeTime

Note:

This is for the metadata itself, not the object.

Timestamp when the Cocina was created.

Returns:

  • (Time)


77
78
79
# File 'lib/cocina_display/cocina_record.rb', line 77

def created_time
  Time.parse(cocina_doc["created"])
end

#filesEnumerator

Traverse nested FileSets and return an enumerator over their files. Each file is a Hash.

Examples:

record.files.each do |file|
 puts file["filename"] #=> "image1.jpg"
 puts file["size"] #=> 123456
end

Returns:

  • (Enumerator)

    Enumerator over file hashes



118
119
120
# File 'lib/cocina_display/cocina_record.rb', line 118

def files
  path("$.structural.contains.*.structural.contains[*]")
end

#labelString?

Note:

This may or may not be the same as the title.

Primary processing label for the object.

Returns:

  • (String, nil)


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

def label
  cocina_doc["label"]
end

#modified_timeTime

Note:

This is for the metadata itself, not the object.

Timestamp when the Cocina was last modified.

Returns:

  • (Time)


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

def modified_time
  Time.parse(cocina_doc["modified"])
end

#path(path_expression) ⇒ Enumerator

Evaluate a JSONPath expression against the Cocina document.

Examples:

Name values for contributors

record.path("$.description.contributor.*.name.*.value").search #=> ["Smith, John", "ACME Corp."]

Filtering nodes using a condition

record.path("$.description.contributor[?(@.type == 'person')].name.*.value").search #=> ["Smith, John"]

Parameters:

  • path_expression (String)

    The JSONPath expression to evaluate.

Returns:

  • (Enumerator)

    An enumerator that yields results matching the expression.

See Also:



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

def path(path_expression)
  Janeway.enum_for(path_expression, cocina_doc)
end