@@ -9,32 +9,52 @@ def initialize(entries=[])
9
9
end
10
10
11
11
def check_dois
12
- doi_summary = { ok : [ ] , missing : [ ] , invalid : [ ] }
12
+ doi_summary = { ok : [ ] , skip : [ ] , missing : [ ] , invalid : [ ] }
13
13
14
14
if @entries . any?
15
15
@entries . each do |entry |
16
- if entry . has_field? ( 'doi' ) && !entry . doi . empty?
16
+ # handle special cases first
17
+ special_case = self . handle_special_case ( entry )
18
+ if special_case
19
+ doi_validity = special_case
20
+ elsif entry . has_field? ( 'doi' ) && !entry . doi . empty?
21
+ # Validate entries with DOIs
17
22
doi_validity = validate_doi ( entry . doi . value )
18
- doi_summary [ doi_validity [ :validity ] ] . push ( doi_validity [ :msg ] )
19
- # If there's no DOI present, check Crossref to see if we can find a candidate DOI for this entry.
20
23
elsif entry . has_field? ( 'title' )
21
- candidate_doi = crossref_lookup ( entry . title . value )
22
- truncated_title = entry . title . to_s [ 0 , 50 ]
23
- truncated_title += "..." if truncated_title . length < entry . title . to_s . length
24
- if candidate_doi == "CROSSREF-ERROR"
25
- doi_summary [ :missing ] . push ( "Errored finding suggestions for \" #{ truncated_title } \" , please try later" )
26
- elsif candidate_doi
27
- doi_summary [ :missing ] . push ( "#{ candidate_doi } may be a valid DOI for title: #{ truncated_title } " )
28
- else
29
- doi_summary [ :missing ] . push ( "No DOI given, and none found for title: #{ truncated_title } " )
30
- end
24
+ # Try and find candidate entries if doi absent, but title present
25
+ doi_validity = handle_missing_doi ( entry )
31
26
else
32
- doi_summary [ : missing] . push ( "Entry without DOI or title found" )
27
+ doi_validity = { validity : : missing, msg : "Entry without DOI or title found" }
33
28
end
29
+
30
+ doi_summary [ doi_validity [ :validity ] ] . push ( doi_validity [ :msg ] )
34
31
end
35
32
end
36
33
37
34
doi_summary
35
+ end
36
+
37
+ # any special case should return false if not applicable, and an object like
38
+ # {:validity => :ok, :msg => "whatever"} otherwise.
39
+ # Add additional special cases as private methods and chain in a tidy sequence plz <3
40
+ def handle_special_case ( entry )
41
+ validity = acm_105555_prefix ( entry ) and return validity
42
+ false
43
+ end
44
+
45
+
46
+ # If there's no DOI present, check Crossref to see if we can find a candidate DOI for this entry.
47
+ def handle_missing_doi ( entry )
48
+ candidate_doi = crossref_lookup ( entry . title . value )
49
+ truncated_title = entry . title . to_s [ 0 , 50 ]
50
+ truncated_title += "..." if truncated_title . length < entry . title . to_s . length
51
+ if candidate_doi == "CROSSREF-ERROR"
52
+ { validity : :missing , msg : "Errored finding suggestions for \" #{ truncated_title } \" , please try later" }
53
+ elsif candidate_doi
54
+ { validity : :missing , msg : "#{ candidate_doi } may be a valid DOI for title: #{ truncated_title } " }
55
+ else
56
+ { validity : :skip , msg : "No DOI given, and none found for title: #{ truncated_title } " }
57
+ end
38
58
end
39
59
40
60
def validate_doi ( doi_string )
@@ -112,4 +132,16 @@ def levenshtein_distance(s, t)
112
132
def similar? ( string_1 , string_2 )
113
133
levenshtein_distance ( string_1 , string_2 ) < 3
114
134
end
135
+
136
+ private
137
+
138
+ def acm_105555_prefix ( entry )
139
+ if entry . has_field? ( 'doi' ) && entry . doi . include? ( "10.5555/" )
140
+ { validity : :invalid , msg : "#{ entry . doi } is INVALID - 10.5555 is a known broken prefix, replace with https://dl.acm.org/doi/{doi} in the {url} field" }
141
+ elsif entry . has_field? ( 'url' ) && entry . url . include? ( "https://dl.acm.org/doi/10.5555" )
142
+ { validity : :skip , msg : "#{ entry . url } - correctly put 10.5555 prefixed doi in the url field, editor should ensure this resolves" }
143
+ else
144
+ false
145
+ end
146
+ end
115
147
end
0 commit comments