|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# LookupLibkey can take a DOI or PMID and return metadata, link resolver links, and journal browse links. |
| 4 | +class LookupLibkey |
| 5 | + BASEURL = 'https://public-api.thirdiron.com/public/v1/libraries' |
| 6 | + |
| 7 | + # Info is the main entry point into the LookupLibkey Class. |
| 8 | + # |
| 9 | + # @param doi [String] |
| 10 | + # @param pmid [String] |
| 11 | + # @return [Hash] or nil |
| 12 | + def self.info(doi: nil, pmid: nil) |
| 13 | + return unless expected_env? |
| 14 | + |
| 15 | + if doi.present? |
| 16 | + external_url = construct_url(doi:) |
| 17 | + Rails.logger.debug(external_url) |
| 18 | + external_data = fetch(external_url) |
| 19 | + return if external_data == 'Error' |
| 20 | + |
| 21 | + extract_metadata(external_data) |
| 22 | + elsif pmid.present? |
| 23 | + external_url = construct_url(pmid:) |
| 24 | + Rails.logger.debug(external_url) |
| 25 | + |
| 26 | + external_data = fetch(external_url) |
| 27 | + return if external_data == 'Error' |
| 28 | + |
| 29 | + extract_metadata(external_data) |
| 30 | + else |
| 31 | + Rails.logger.error('No doi or pmid provided to LookupLibkey') |
| 32 | + nil |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + # expected_env? confirms both required variables are set |
| 37 | + # |
| 38 | + # @return Boolean |
| 39 | + def self.expected_env? |
| 40 | + Rails.logger.error('No LIBKEY_KEY set') if libkey_key.nil? |
| 41 | + |
| 42 | + Rails.logger.error('No LIBKEY_ID set') if libkey_id.nil? |
| 43 | + |
| 44 | + libkey_id.present? && libkey_key.present? |
| 45 | + end |
| 46 | + |
| 47 | + # using method instead of constant to allow for mutating in testing without causing sporadic failures |
| 48 | + def self.libkey_key |
| 49 | + ENV.fetch('LIBKEY_KEY', nil) |
| 50 | + end |
| 51 | + |
| 52 | + # using method instead of constant to allow for mutating in testing without causing sporadic failures |
| 53 | + def self.libkey_id |
| 54 | + ENV.fetch('LIBKEY_ID', nil) |
| 55 | + end |
| 56 | + |
| 57 | + # extract_metadata maps data from the LibKey response to an internal hash |
| 58 | + # |
| 59 | + # @return Hash |
| 60 | + def self.extract_metadata(external_data) |
| 61 | + { |
| 62 | + title: external_data['data']['title'], |
| 63 | + authors: external_data['data']['authors'].gsub('; ', ';'), |
| 64 | + doi: external_data['data']['doi'], |
| 65 | + pmid: external_data['data']['pmid'], |
| 66 | + oa: external_data['data']['openAccess'], |
| 67 | + date: external_data['data']['date'], |
| 68 | + journal_name: external_data['included'].first['title'], |
| 69 | + journal_issns: external_data['included'].first['issn'], |
| 70 | + journal_image: external_data['included'].first['coverImageUrl'], |
| 71 | + journal_link: external_data['included'].first['browzineWebLink'], |
| 72 | + link_resolver_url: external_data['data']['bestIntegratorLink']['bestLink'] |
| 73 | + } |
| 74 | + end |
| 75 | + |
| 76 | + # https://thirdiron.atlassian.net/wiki/spaces/BrowZineAPIDocs/pages/65929220/BrowZine+Public+API+Overview |
| 77 | + # https://thirdiron.atlassian.net/wiki/spaces/BrowZineAPIDocs/pages/65699928/Article+DOI+PMID+Lookup+Endpoint+LibKey |
| 78 | + # public/v1/libraries/:library_id/articles/doi/:article_doi?access_token=ffffffff-ffff-ffff-ffff-ffffffffffff |
| 79 | + # /public/v1/libraries/:library_id/articles/pmid/:article_pmid?access_token=ffffffff-ffff-ffff-ffff-ffffffffffff |
| 80 | + def self.construct_url(doi: nil, pmid: nil) |
| 81 | + if doi.present? |
| 82 | + "#{BASEURL}/#{libkey_id}/articles/doi/#{doi}?include=journal&access_token=#{libkey_key}" |
| 83 | + elsif pmid.present? |
| 84 | + "#{BASEURL}/#{libkey_id}/articles/pmid/#{pmid}?include=journal&access_token=#{libkey_key}" |
| 85 | + else |
| 86 | + Rails.logger.error('No PMID or DOI provided to LookupLibkey.url()') |
| 87 | + nil |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + # Fetch performs the HTTP calls, parses JSON for successful requests. |
| 92 | + def self.fetch(url) |
| 93 | + resp = HTTP.headers(accept: 'application/json').get(url) |
| 94 | + if resp.status == 200 |
| 95 | + JSON.parse(resp.to_s) |
| 96 | + else |
| 97 | + Rails.logger.debug do |
| 98 | + 'Fact lookup error. DOI or PMID detected but LibKey returned no data or otherwise errored' |
| 99 | + end |
| 100 | + Rails.logger.debug { "Response status: #{resp.status}" } |
| 101 | + Rails.logger.debug { "URL: #{url}" } |
| 102 | + 'Error' |
| 103 | + end |
| 104 | + end |
| 105 | +end |
0 commit comments