|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# OpenAlex API integration |
| 4 | +# https://docs.openalex.org/how-to-use-the-api/api-overview |
| 5 | +# https://docs.openalex.org/api-entities/works |
| 6 | +class Openalex |
| 7 | + BASEURL = 'https://api.openalex.org' |
| 8 | + |
| 9 | + class LookupFailure < StandardError; end |
| 10 | + |
| 11 | + # enabled? confirms that the required environment variable is set. |
| 12 | + # |
| 13 | + # @return Boolean |
| 14 | + def self.enabled? |
| 15 | + openalex_email.present? |
| 16 | + end |
| 17 | + |
| 18 | + # OpenAlex accepts various identifier formats: full URI, short URN, or raw identifier |
| 19 | + # We are only supporting raw identifier lookups here for simplicity (ex: DOI without the "doi:" prefix) as that is |
| 20 | + # the shape of the data coming from Primo. We add the identifier type prefix accordingly. |
| 21 | + # Currently supported identifier types in OpenAlex are 'doi', 'pmid', 'pmcid', and 'mag' |
| 22 | + def self.work(identifier:, identifier_type: 'doi', openalex_client: nil) |
| 23 | + return nil unless enabled? |
| 24 | + |
| 25 | + # Check cache first |
| 26 | + cache_key = generate_cache_key(identifier_type, identifier) |
| 27 | + cached_result = Rails.cache.read(cache_key) |
| 28 | + return cached_result if cached_result.present? |
| 29 | + |
| 30 | + # Construct the OpenAlex Works endpoint URL |
| 31 | + url = "#{BASEURL}/works/#{identifier_type}:#{identifier}" |
| 32 | + |
| 33 | + openalex_http = setup(url, openalex_client) |
| 34 | + |
| 35 | + begin |
| 36 | + raw_response = openalex_http.timeout(6).get(url) |
| 37 | + raise LookupFailure, raw_response.status unless raw_response.status == 200 |
| 38 | + |
| 39 | + json_response = JSON.parse(raw_response.to_s) |
| 40 | + |
| 41 | + result = extract_metadata(json_response) |
| 42 | + Rails.logger.debug(result) |
| 43 | + |
| 44 | + # Cache the result for 24 hours |
| 45 | + Rails.cache.write(cache_key, result, expires_in: 24.hours) if result.present? |
| 46 | + result |
| 47 | + rescue LookupFailure => e |
| 48 | + # 404s are expected for missing works, so only log unexpected statuses |
| 49 | + if e.message != '404 Not Found' |
| 50 | + Sentry.set_tags('mitlib.openalex_url': url) |
| 51 | + Sentry.set_tags('mitlib.openalex_status': e.message) |
| 52 | + Sentry.capture_message('Unexpected OpenAlex response status') |
| 53 | + Rails.logger.error("Unexpected OpenAlex response status: #{e.message}") |
| 54 | + end |
| 55 | + nil |
| 56 | + rescue HTTP::Error |
| 57 | + Rails.logger.error('OpenAlex connection error') |
| 58 | + { 'error' => 'A connection error has occurred' } |
| 59 | + rescue JSON::ParserError |
| 60 | + Rails.logger.error('OpenAlex parsing error') |
| 61 | + { 'error' => 'A parsing error has occurred' } |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + def self.is_oa?(external_data) |
| 66 | + return false if external_data.blank? |
| 67 | + |
| 68 | + external_data.dig('open_access', 'is_oa') || false |
| 69 | + end |
| 70 | + |
| 71 | + # Using the OpenAlex best OA location logic. If we need to change the logic, we can update here by using locations |
| 72 | + # rather than best_oa_location from OpenAlex directly. |
| 73 | + def self.extract_metadata(external_data) |
| 74 | + return nil if external_data.blank? || external_data['id'].blank? |
| 75 | + return nil unless is_oa?(external_data) |
| 76 | + |
| 77 | + { |
| 78 | + record_id: external_data['id'], |
| 79 | + is_open: is_oa?(external_data), |
| 80 | + pdf_link: pdf_link(external_data), |
| 81 | + html_link: html_link(external_data), |
| 82 | + type: user_friendly_type(type(external_data)) |
| 83 | + } |
| 84 | + end |
| 85 | + |
| 86 | + def self.pdf_link(external_data) |
| 87 | + return nil if external_data.blank? || external_data['best_oa_location'].blank? |
| 88 | + |
| 89 | + external_data['best_oa_location']['pdf_url'] |
| 90 | + end |
| 91 | + |
| 92 | + def self.html_link(external_data) |
| 93 | + return nil if external_data.blank? || external_data['best_oa_location'].blank? |
| 94 | + |
| 95 | + external_data['best_oa_location']['landing_page_url'] |
| 96 | + end |
| 97 | + |
| 98 | + def self.type(external_data) |
| 99 | + return nil if external_data.blank? || external_data['best_oa_location'].blank? |
| 100 | + |
| 101 | + external_data['best_oa_location']['version'] |
| 102 | + end |
| 103 | + |
| 104 | + def self.openalex_email |
| 105 | + ENV.fetch('OPENALEX_EMAIL', nil) |
| 106 | + end |
| 107 | + |
| 108 | + def self.setup(url, openalex_client) |
| 109 | + openalex_client || HTTP.persistent(url) |
| 110 | + .headers(accept: 'application/json', |
| 111 | + 'User-Agent': "TIMDEX UI (#{openalex_email})") |
| 112 | + end |
| 113 | + |
| 114 | + def self.generate_cache_key(identifier_type, identifier) |
| 115 | + "openalex:works:#{identifier_type}:#{Digest::MD5.hexdigest(identifier)}" |
| 116 | + end |
| 117 | + |
| 118 | + def self.user_friendly_type(type_code) |
| 119 | + case type_code |
| 120 | + when 'acceptedVersion' |
| 121 | + 'Accepted Version' |
| 122 | + when 'publishedVersion' |
| 123 | + 'Published Version' |
| 124 | + when 'submittedVersion' |
| 125 | + 'Submitted Version' |
| 126 | + else |
| 127 | + Sentry.set_tags('mitlib.openalex_type_code': type_code) |
| 128 | + Sentry.capture_message('Unexpected OpenAlex type code') |
| 129 | + Rails.logger.error("Unexpected OpenAlex type code: #{type_code}") |
| 130 | + |
| 131 | + type_code |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments