Skip to content

Commit

Permalink
Merge pull request #639 from CDLUC3/bug/ror-downloads
Browse files Browse the repository at this point in the history
Fix ROR Service
  • Loading branch information
briri authored Jul 31, 2024
2 parents b3bcc25 + 58690ee commit bb4fa38
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions app/services/external_apis/ror_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def fetch(force: false)

# Fetch the Zenodo metadata for ROR to see if we have the latest data dump
metadata = fetch_zenodo_metadata

if metadata.present?
FileUtils.mkdir_p(file_dir)

Expand All @@ -67,29 +66,25 @@ def fetch(force: false)
if old_checksum_val == metadata[:checksum]
log_message(method: method, message: 'There is no new ROR file to process.')
else
download_file = metadata.fetch(:links, {})[:download]
download_file = download_file = metadata['key']
download_url = metadata.fetch('links', {}).fetch('download', metadata.fetch('links', {})['self'])
log_message(method: method, message: "New ROR file detected - checksum #{metadata[:checksum]}")
log_message(method: method, message: "Downloading #{download_file}")
log_message(method: method, message: "From #{download_url}")

payload = download_ror_file(url: metadata.fetch(:links, {})[:download])
payload = download_ror_file(url: download_url)
if payload.present?
file = File.open(zip_file, 'wb')
file.write(payload)

# rubocop:disable Metrics/BlockNesting
if validate_downloaded_file(file_path: zip_file, checksum: metadata[:checksum])
json_file = download_file.split('/').last.gsub('.zip', '')
json_file = "#{json_file}.json" unless json_file.end_with?('.json')

# Process the ROR JSON
if process_ror_file(zip_file: zip_file, file: json_file)
checksum = File.open(checksum_file, 'w')
checksum.write(metadata[:checksum])
end
else
log_error(method: method, error: StandardError.new('Downloaded ROR zip does not match checksum!'))
json_file = download_file.split('/').last.gsub('.zip', '')
json_file = "#{json_file}.json" unless json_file.end_with?('.json')

# Process the ROR JSON
if process_ror_file(zip_file: zip_file, file: json_file)
checksum = File.open(checksum_file, 'w')
checksum.write(metadata[:checksum])
end
# rubocop:enable Metrics/BlockNesting
else
log_error(method: method, error: StandardError.new('Unable to download ROR file!'))
end
Expand Down Expand Up @@ -121,7 +116,7 @@ def fetch_zenodo_metadata

# Extract the most recent file's metadata
file_metadata = json.fetch('hits', {}).fetch('hits', []).first&.fetch('files', [])&.last&.with_indifferent_access
unless file_metadata.present? && file_metadata.fetch(:links, {})[:download].present?
unless file_metadata.present?
handle_http_failure(method: 'No file found in ROR metadata from Zenodo', http_response: resp)
notify_administrators(obj: 'RorService', response: resp)
return nil
Expand All @@ -140,9 +135,12 @@ def download_ror_file(url:)

headers = {
host: 'zenodo.org',
Accept: 'application/zip'
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': "California Digital Library - dmptool.org (mailto:[email protected])"
}
resp = http_get(uri: url, additional_headers: headers, debug: false)

unless resp.present? && resp.code == 200
handle_http_failure(method: "Fetching ROR file from Zenodo - #{url}", http_response: resp)
notify_administrators(obj: 'RorService', response: resp)
Expand Down

0 comments on commit bb4fa38

Please sign in to comment.