Skip to content

Commit 7d1f8d1

Browse files
Merge pull request #307 from MITLibraries/use-232-libkey
Integrate Libkey with results UI
2 parents 2888060 + dc38998 commit 7d1f8d1

File tree

7 files changed

+89
-4
lines changed

7 files changed

+89
-4
lines changed

app/models/normalize_primo_record.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def normalize
1818
links:,
1919
citation:,
2020
identifier:,
21+
doi:,
22+
pmid:,
2123
summary:,
2224
publisher:,
2325
location:,
@@ -142,6 +144,30 @@ def identifier
142144
@record['pnx']['control']['recordid'].join
143145
end
144146

147+
def doi
148+
return unless @record['pnx']['addata'] && @record['pnx']['addata']['doi']
149+
150+
if @record['pnx']['addata']['doi'].length > 1
151+
Sentry.set_tags('mitlib.recordId': identifier || 'empty record id')
152+
Sentry.set_tags('mitlib.dois': @record['pnx']['addata']['doi'])
153+
Sentry.capture_message('Multiple DOIs found in one record')
154+
end
155+
156+
@record['pnx']['addata']['doi'].first
157+
end
158+
159+
def pmid
160+
return unless @record['pnx']['addata'] && @record['pnx']['addata']['pmid']
161+
162+
if @record['pnx']['addata']['pmid'].length > 1
163+
Sentry.set_tags('mitlib.recordId': identifier || 'empty record id')
164+
Sentry.set_tags('mitlib.pmids': @record['pnx']['addata']['pmid'])
165+
Sentry.capture_message('Multiple PMIDs found in one record')
166+
end
167+
168+
@record['pnx']['addata']['pmid'].first
169+
end
170+
145171
def summary
146172
return unless @record['pnx']['display']['description']
147173

app/views/libkey/lookup.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<% if Libkey.enabled? && @libkey.present? %>
2-
<%= link_to( @libkey[:text], @libkey[:link], class: 'button button-primary' ) %>
2+
<%= link_to( @libkey[:text], @libkey[:link], class: 'button', style: 'border: none;' ) %>
33
<% end %>

app/views/search/_result_primo.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<%= link_to link['kind'].titleize, link['url'], class: 'button' %>
4141
<% end %>
4242
<% end %>
43+
<% if Libkey.enabled? && result[:doi].present? %>
44+
<%= render(partial: 'trigger_libkey', locals: { kind: 'doi', identifier: result[:doi] }) %>
45+
<% elsif Libkey.enabled? && result[:pmid].present? %>
46+
<%= render(partial: 'trigger_libkey', locals: { kind: 'pmid', identifier: result[:pmid] }) %>
47+
<% end %>
4348
<% if result[:availability].present? %>
4449
<span class="availability"><%= availability(result[:availability], result[:location], result[:other_availability]) %></span>
4550
<% end %>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<% return unless Libkey.enabled? %>
2+
3+
<% data_url = "/lookup?type=#{kind}&identifier=#{identifier}" %>
4+
5+
<span class="tacos-container"
6+
data-controller="content-loader"
7+
data-content-loader-url-value=<%= data_url %>>
8+
<span style="background-color:lightgray;
9+
display: inline-block;
10+
font-size: 1.6rem;
11+
font-weight: 500;
12+
margin-right: 16px;
13+
min-width: 120px;">&nbsp;</span>
14+
</span>

test/controllers/libkey_controller_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class LibkeyControllerTest < ActionDispatch::IntegrationTest
2727
get '/lookup?type=doi&identifier=10.1038/s41567-023-02305-y'
2828

2929
assert_response :success
30-
assert_select 'a.button-primary', { count: 1 }
30+
assert_select 'a.button', { count: 1 }
3131
end
3232

3333
VCR.use_cassette('libkey pmid') do
3434
get '/lookup?type=pmid&identifier=22110403'
3535

3636
assert_response :success
37-
assert_select 'a.button-primary', { count: 1 }
37+
assert_select 'a.button', { count: 1 }
3838
end
3939
end
4040

test/fixtures/primo/full_record.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"pages": ["123-145"],
1818
"jtitle": ["Journal of Testing"],
1919
"isbn": ["9781234567890", "1234567890"],
20-
"pub": ["MIT Press"]
20+
"pub": ["MIT Press"],
21+
"doi": ["10.1038/s41567-023-02305-y"],
22+
"pmid": ["22110403"]
2123
},
2224
"facets": {
2325
"frbrtype": ["5"],

test/models/normalize_primo_record_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,44 @@ def cdi_record
106106
assert_equal 'alma991000000001234567', normalized[:identifier]
107107
end
108108

109+
test 'normalizes doi' do
110+
normalized = NormalizePrimoRecord.new(full_record, 'test').normalize
111+
assert_equal normalized[:doi], '10.1038/s41567-023-02305-y'
112+
end
113+
114+
test 'handles missing doi' do
115+
normalized = NormalizePrimoRecord.new(minimal_record, 'test').normalize
116+
assert_nil normalized[:doi]
117+
end
118+
119+
test 'multiple dois normalize to the first one' do
120+
temp_record = full_record
121+
temp_record['pnx']['addata']['doi'] = %w[three two one]
122+
123+
normalized = NormalizePrimoRecord.new(temp_record, 'test').normalize
124+
125+
assert_equal normalized[:doi], 'three'
126+
end
127+
128+
test 'normalizes pmid' do
129+
normalized = NormalizePrimoRecord.new(full_record, 'test').normalize
130+
assert_equal normalized[:pmid], '22110403'
131+
end
132+
133+
test 'handles missing pmid' do
134+
normalized = NormalizePrimoRecord.new(minimal_record, 'test').normalize
135+
assert_nil normalized[:pmid]
136+
end
137+
138+
test 'multiple pmids normalize to the first one' do
139+
temp_record = full_record
140+
temp_record['pnx']['addata']['pmid'] = %w[three two one]
141+
142+
normalized = NormalizePrimoRecord.new(temp_record, 'test').normalize
143+
144+
assert_equal normalized[:pmid], 'three'
145+
end
146+
109147
test 'normalizes summary' do
110148
normalized = NormalizePrimoRecord.new(full_record, 'test').normalize
111149
assert_equal 'A comprehensive study of testing methodologies', normalized[:summary]

0 commit comments

Comments
 (0)