Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/thirdiron_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def libkey
@libkey = Libkey.lookup(type: params[:type], identifier: params[:identifier])
@doi = params[:type] == 'doi' ? params[:identifier] : nil
@pmid = params[:type] == 'pmid' ? params[:identifier] : nil
@format = params[:format]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for libkey is too high. [<4, 17, 6> 18.47/17] [rubocop:Metrics/AbcSize]

end

def browzine
Expand Down
10 changes: 10 additions & 0 deletions app/helpers/results_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ def search_worldcat_link

'https://mit.on.worldcat.org/search?' + worldcat_params
end

# Determines if a format value represents an article type
#
# @param format [String] The format value to check (e.g., 'Article', 'Magazine Article')
# @return [Boolean] True if format contains 'article' as a whole word (case-insensitive), false otherwise
def article?(format)
return false if format.blank?

format.match?(/\barticle\b/i)
end
end
7 changes: 4 additions & 3 deletions app/views/search/_result_primo.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@

<%# LibKey links are our preferred links so we call the API when possible to get these preferred links %>
<% if ThirdIron.enabled? && result[:doi].present? %>
<%= render(partial: 'trigger_libkey', locals: { kind: 'doi', identifier: result[:doi] }) %>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting partially to help me keep track. I think this is different than some other triggers because we only put OpenAlex links if LibKey fails in this condition... so we want to only do that if it is an article hence our need to pass I that state here versus some other states in which we only trigger at by checking the new helper.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that's correct!

<%= render(partial: 'trigger_libkey', locals: { kind: 'doi', identifier: result[:doi], format: result[:format] }) %>
<% elsif ThirdIron.enabled? && result[:pmid].present? %>
<%= render(partial: 'trigger_libkey', locals: { kind: 'pmid', identifier: result[:pmid] }) %>
<%= render(partial: 'trigger_libkey', locals: { kind: 'pmid', identifier: result[:pmid], format: result[:format] }) %>
<% end %>

<%# OpenAccess Links: If OA_ALWAYS is enabled, include them when available. If not we'll trigger when we have a DOI/PMID but LibKey does not return data. See app/views/thirdiron/libkey.html.erb %>
<% if Feature.enabled?(:oa_always) %>
<%# Only show OpenAccess links for articles %>
<% if Feature.enabled?(:oa_always) && article?(result[:format]) %>
<%= render(partial: 'trigger_openalex_setup', locals: { doi: result[:doi], pmid: result[:pmid] }) %>
<% end %>

Expand Down
4 changes: 3 additions & 1 deletion app/views/search/_trigger_libkey.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<% return unless ThirdIron.enabled? %>

<% data_url = "/libkey?type=#{kind}&identifier=#{identifier}" %>
<% query_params = { type: kind, identifier: identifier } %>
<% query_params[:format] = format if format.present? %>
<% data_url = "/libkey?#{query_params.to_query}" %>

<span class="libkey-container"
data-controller="content-loader"
Expand Down
3 changes: 2 additions & 1 deletion app/views/thirdiron/libkey.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<% end %>

<%# If we didn't get data back from LibKey, we try OpenAlex unless OA_ALWAYS is enabled at which point we would have already requested data from OpenAlex in parallel to LibKey %>
<% elsif @libkey.blank? && !Feature.enabled?(:oa_always) %>
<%# Only show OpenAlex links for articles %>
<% elsif @libkey.blank? && !Feature.enabled?(:oa_always) && article?(@format) %>
<%= render(partial: 'search/trigger_openalex_setup', locals: { doi: @doi, pmid: @pmid }) %>
<% end %>
14 changes: 12 additions & 2 deletions test/controllers/thirdiron_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ThirdironControllerTest < ActionDispatch::IntegrationTest
test 'libkey for non-existent identifier inserts openalex setup when oa_always is false' do
# Libkey responds here, so we have a cassette - but the response is empty
VCR.use_cassette('libkey nonexistent') do
get '/libkey?type=doi&identifier=foobar'
get '/libkey?type=doi&identifier=foobar&format=Article'

assert_response :success
assert_select '.openalex-container', { count: 1 }
Expand All @@ -52,14 +52,24 @@ class ThirdironControllerTest < ActionDispatch::IntegrationTest
ClimateControl.modify(FEATURE_OA_ALWAYS: 'true') do
# Libkey responds here, so we have a cassette - but the response is empty
VCR.use_cassette('libkey nonexistent') do
get '/libkey?type=doi&identifier=foobar'
get '/libkey?type=doi&identifier=foobar&format=Article'

assert_response :success
assert response.body.blank?
end
end
end

test 'libkey for non-existent identifier does not insert OpenAlex setup for non-article formats' do
# OpenAlex should only appear for articles
VCR.use_cassette('libkey nonexistent') do
get '/libkey?type=doi&identifier=foobar&format=Book'

assert_response :success
assert_select '.openalex-container', { count: 0 }
end
end

test 'libkey no response when either env var is not set' do
# No cassette because this never results in traffic to Libkey
ClimateControl.modify(THIRDIRON_ID: nil) do
Expand Down
33 changes: 33 additions & 0 deletions test/helpers/results_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,37 @@ class ResultsHelperTest < ActionView::TestCase
assert link.start_with?('https://mit.on.worldcat.org/search?')
assert_includes link, 'queryString=climate+change'
end

test 'article? returns true for Article format' do
assert article?('Article')
assert article?('Journal Article')
assert article?('Newspaper Article')
end

test 'article? returns true for lowercase article formats' do
assert article?('article')
assert article?('journal article')
assert article?('newspaper article')
end

test 'article? returns true for mixed case article formats' do
assert article?('ARTICLE')
assert article?('Article')
assert article?('JoUrNaL ArTiClE')
end

test 'article? returns false for non-article formats' do
assert_not article?('Journal')
assert_not article?('eBook')
assert_not article?('Book Chapter')
assert_not article?('Conference Proceeding')
assert_not article?('Reference Entry')
assert_not article?('Research Database')
assert_not article?('Dataset')
end

test 'article? returns false for blank or nil format' do
assert_not article?(nil)
assert_not article?('')
end
end