Skip to content

Commit 62c54e0

Browse files
committed
Don't throw 500 on invalid citation style
Also allow default citation style to be configured per instance
1 parent 7034ad3 commit 62c54e0

File tree

8 files changed

+56
-13
lines changed

8 files changed

+56
-13
lines changed

app/helpers/citations_helper.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
module CitationsHelper
55
def render_doi_citation(doi, style)
66
Seek::Citations.from_doi(doi, style)
7+
rescue Seek::Citations::InvalidStyleException
8+
t('citations.errors.invalid_style')
79
rescue JSON::ParserError, RestClient::Exception
8-
'An error occurred whilst fetching the citation'
10+
t('citations.errors.general')
911
end
1012

1113
def render_cff_citation(blob, style)
1214
Seek::Citations.from_cff(blob, style)
13-
rescue StandardError
14-
'An error occurred whilst fetching the citation'
15+
rescue Seek::Citations::InvalidStyleException
16+
t('citations.errors.invalid_style')
17+
rescue JSON::ParserError, RestClient::Exception
18+
t('citations.errors.general')
1519
end
1620

1721
def citation_style_options(selected = nil)
@@ -20,6 +24,6 @@ def citation_style_options(selected = nil)
2024
end
2125

2226
def selected_citation_style
23-
session[:citation_style] || Seek::Citations::DEFAULT
27+
session[:citation_style] || Seek::Config.default_citation_style
2428
end
2529
end

config/initializers/seek_configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ def load_seek_config_defaults!
287287
]
288288

289289
Seek::Config.default :reindex_all_batch_size, 50
290+
Seek::Config.default :default_citation_style, 'apa'
290291

291292
load_seek_testing_defaults! if Rails.env.test?
292293
end

config/locales/en.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,8 @@ you can choose to have a %{project} linked to a site managed %{programme} instea
351351

352352
tooltips:
353353
batch_permission_changes_button: "You can change the sharing policy and permissions for your items as a batch. A preview of selected items will be given before you choose new permissions for them."
354-
publish_your_items_button: "Publish your owned items as a batch. A preview will be given before publishing"
354+
publish_your_items_button: "Publish your owned items as a batch. A preview will be given before publishing"
355+
citations:
356+
errors:
357+
general: An error occurred whilst fetching the citation
358+
invalid_style: Invalid citation style

lib/seek/citations.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
module Seek
66
class Citations
7-
DEFAULT = 'apa' # This could be a setting one day
8-
97
def self.from_doi(doi, style)
10-
generate(doi_to_csl(doi), style)
8+
validate_style(style)
9+
csl = doi_to_csl(doi)
10+
generate(csl, style)
1111
end
1212

1313
def self.from_cff(blob, style)
14-
generate(cff_to_csl(blob), style)
14+
validate_style(style)
15+
csl = cff_to_csl(blob)
16+
generate(csl, style)
1517
end
1618

1719
def self.generate(csl, style)
@@ -26,6 +28,12 @@ def self.style_pairs
2628
end
2729
end
2830

31+
def self.valid_styles
32+
Rails.cache.fetch("citation-styles-set") do
33+
Set.new(style_pairs.map(&:last))
34+
end
35+
end
36+
2937
def self.doi_to_csl(doi)
3038
Rails.cache.fetch("citation-#{doi}") do
3139
resp = RestClient.get("https://doi.org/#{Addressable::URI.escape(doi)}", accept: 'application/vnd.citationstyles.csl+json')
@@ -47,5 +55,11 @@ def self.generate_style_pairs
4755
def self.style_dictionary_path
4856
Rails.root.join('config/default_data/csl_styles.yml')
4957
end
58+
59+
private
60+
61+
def self.validate_style(style)
62+
raise Seek::Citations::InvalidStyleException unless valid_styles.include?(style)
63+
end
5064
end
5165
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Seek::Citations::InvalidStyleException < StandardError; end

lib/seek/config_setting_attributes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,4 @@ max_filters:
278278
convert: :to_i
279279
reindex_all_batch_size:
280280
convert: :to_i
281+
default_citation_style:

lib/seek/renderers/citation_renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def render_content
1212
private
1313

1414
def style
15-
params[:style] || Seek::Citations::DEFAULT
15+
params[:style] || Seek::Config.default_citation_style
1616
end
1717
end
1818
end

test/integration/citation_style_test.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def setup
1515
get model_path(@model)
1616
# APA by default
1717
assert_select '#citation' do
18-
assert_select 'div[data-citation-style=?]', Seek::Citations::DEFAULT, text: /Bacall, F/, count: 1
18+
assert_select 'div[data-citation-style=?]', Seek::Config.default_citation_style, text: /Bacall, F/, count: 1
1919
end
2020
assert_select '#citation-style-select' do
21-
assert_select "option[selected='selected'][value=?]", Seek::Citations::DEFAULT
21+
assert_select "option[selected='selected'][value=?]", Seek::Config.default_citation_style
2222
end
2323

2424
new_style = 'journal-of-infectious-diseases'
@@ -27,14 +27,32 @@ def setup
2727

2828
get model_path(@model)
2929
assert_select '#citation' do
30-
assert_select 'div[data-citation-style=?]', Seek::Citations::DEFAULT, count: 0
30+
assert_select 'div[data-citation-style=?]', Seek::Config.default_citation_style, count: 0
3131
assert_select 'div[data-citation-style=?]', new_style, text: /Bacall F/, count: 1
3232
end
3333
assert_select '#citation-style-select' do
3434
assert_select "option[selected='selected'][value=?]", new_style
3535
end
3636
end
3737

38+
test 'handles invalid style selection' do
39+
doi_citation_mock
40+
41+
get citation_path(@doi, style: 'fjkgdfhgkjdf123', format: :js), xhr: true
42+
43+
assert_includes response.body, 'Invalid citation style'
44+
end
45+
46+
test 'handles CSL fetch error' do
47+
stub_request(:get, /(https?:\/\/)?(dx\.)?doi\.org\/.+/)
48+
.with(headers: { 'Accept' => 'application/vnd.citationstyles.csl+json' })
49+
.to_return(body: 'Error!', status: 500)
50+
51+
get citation_path(@doi, style: 'apa', format: :js), xhr: true
52+
53+
assert_includes response.body, 'An error occurred whilst fetching the citation'
54+
end
55+
3856
private
3957

4058
def doi_citation_mock

0 commit comments

Comments
 (0)