-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1182 from sul-dlss/extract-cocina
- Loading branch information
Showing
21 changed files
with
104 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
# Retrieves the public JSON (cocina) and extracts data from it. | ||
class Cocina | ||
extend ActiveSupport::Benchmarkable | ||
|
||
THUMBNAIL_MIME_TYPE = 'image/jp2' | ||
|
||
def self.find(druid) | ||
Rails.cache.fetch("purl/#{druid}.json", expires_in: 10.minutes) do | ||
benchmark "Fetching public json for #{druid}" do | ||
response = Faraday.get(public_json_url(druid)) | ||
raise Purl::Exception, response.status unless response.success? | ||
|
||
new(JSON.parse(response.body)) | ||
end | ||
end | ||
end | ||
|
||
def self.public_json_url(druid) | ||
"#{Settings.purl.url}#{druid}.json" | ||
end | ||
|
||
def self.logger | ||
Rails.logger | ||
end | ||
|
||
def initialize(data) | ||
@data = data | ||
end | ||
|
||
attr_accessor :data | ||
|
||
def find_file(file_name) | ||
file_sets = data.dig('structural', 'contains') | ||
raise(ActionController::MissingFile, "File not found '#{file_name}'") unless file_sets # Trap for Collections | ||
|
||
file_sets.lazy.flat_map { |file_set| file_set.dig('structural', 'contains') } | ||
.find { |file| file['filename'] == file_name } || raise(ActionController::MissingFile, "File not found '#{file_name}'") | ||
end | ||
|
||
def thumbnail_file | ||
data.dig('structural', 'contains') | ||
.lazy.flat_map { |file_set| file_set.dig('structural', 'contains') } | ||
.find { |file| file['hasMimeType'] == THUMBNAIL_MIME_TYPE } | ||
end | ||
|
||
def embargo_release_date | ||
data.dig('access', 'embargo', 'releaseDate') | ||
end | ||
|
||
def files(&) | ||
return to_enum(:files) unless block_given? | ||
|
||
files_from_json(&) | ||
end | ||
|
||
private | ||
|
||
def files_from_json | ||
data.dig('structural', 'contains').each do |fileset| | ||
fileset.dig('structural', 'contains').each do |file| | ||
file = StacksFile.new(id: data.fetch('externalIdentifier').delete_prefix('druid:'), file_name: file['filename']) | ||
yield file | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.