-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to OCLC Discovery API Citation service
- Loading branch information
Showing
30 changed files
with
498 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<% citations.each do |style, citation| %> | ||
<div class="mb-3"> | ||
<% unless style == 'NULL' %> | ||
<h4><%= t("searchworks.citations.styles.#{style}") %></h4> | ||
<% end %> | ||
<% Array(citation).each do |cite| %> | ||
<div class="mb-2"> | ||
<%= cite %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Citations | ||
class CitationComponent < ViewComponent::Base | ||
attr_reader :citations | ||
|
||
def initialize(citations:) | ||
@citations = citations | ||
super() | ||
end | ||
|
||
def render? | ||
citations.present? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render Citations::CitationComponent.new(citations: grouped_citations) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Citations | ||
class GroupedCitationComponent < ViewComponent::Base | ||
attr_reader :citations | ||
|
||
PREFERRED_CITATION_KEY = 'preferred' | ||
|
||
def initialize(citations:) | ||
@citations = citations | ||
super() | ||
end | ||
|
||
def grouped_citations | ||
citation_styles.index_with { |style| citations.pluck(style).compact } | ||
end | ||
|
||
def render? | ||
citations.present? | ||
end | ||
|
||
private | ||
|
||
def citation_styles | ||
keys = citations.map(&:keys).flatten.uniq | ||
|
||
return keys unless keys.include?(PREFERRED_CITATION_KEY) | ||
|
||
# If the preferred citation is present, move it to the front of the list | ||
# so that it always displays first | ||
keys.delete(PREFERRED_CITATION_KEY) | ||
keys.unshift(PREFERRED_CITATION_KEY) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
### | ||
# Returns an EDS citation formatted for use by SearchWorks | ||
module Citations | ||
class EdsCitation | ||
CITATION_STYLES = %w[apa chicago harvard mla turabian].freeze | ||
|
||
attr_reader :eds_citation_styles | ||
|
||
def initialize(eds_citation_styles:) | ||
@eds_citation_styles = eds_citation_styles | ||
end | ||
|
||
def all_citations | ||
matching_styles.index_with do |id| | ||
eds_citation_styles.select { |style| style.fetch('id', nil) == id }.pick('data')&.html_safe # rubocop:disable Rails/OutputSafety | ||
end.compact | ||
end | ||
|
||
private | ||
|
||
def matching_styles | ||
eds_citation_styles.pluck('id').select { |id| CITATION_STYLES.include?(id) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
### | ||
# Returns an MODS citation formatted for use by SearchWorks | ||
module Citations | ||
class ModsCitation | ||
CITATION_STYLE = 'preferred' | ||
|
||
attr_reader :notes | ||
|
||
def initialize(notes:) | ||
@notes = notes | ||
end | ||
|
||
def all_citations | ||
return { CITATION_STYLE => "<p>#{mods_citation}</p>".html_safe } if mods_citation.present? # rubocop:disable Rails/OutputSafety | ||
|
||
{} | ||
end | ||
|
||
private | ||
|
||
def mods_citation | ||
notes.find { |note| note.label.downcase.match?(/preferred citation:?/) }&.values&.join | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
### | ||
# Returns an OCLC citation formatted for use by SearchWorks | ||
module Citations | ||
class OclcCitation | ||
CITATION_STYLES = %w[apa chicago harvard mla turabian].freeze | ||
|
||
attr_reader :oclc_number | ||
|
||
def initialize(oclc_number:) | ||
@oclc_number = oclc_number | ||
end | ||
|
||
def all_citations | ||
@all_citations ||= CITATION_STYLES.index_with { |citation_style| citation(citation_style) }.compact | ||
end | ||
|
||
private | ||
|
||
def citation(citation_style) | ||
citation_from_oclc(citation_style:).fetch('entries', []).first.fetch('citationText', nil)&.html_safe # rubocop:disable Rails/OutputSafety | ||
end | ||
|
||
def citation_from_oclc(citation_style:) | ||
oclc_client.citation(oclc_number:, citation_style: oclc_style_code(citation_style)) | ||
end | ||
|
||
def oclc_client | ||
@oclc_client ||= OclcDiscoveryClient.new | ||
end | ||
|
||
def oclc_style_code(searchworks_style_code) | ||
case searchworks_style_code | ||
when 'chicago' | ||
'chicago-author-date' | ||
when 'harvard' | ||
'harvard-cite-them-right' | ||
when 'mla' | ||
'modern-language-association' | ||
when 'turabian' | ||
'turabian-author-date' | ||
else | ||
searchworks_style_code | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.