Skip to content

Commit

Permalink
Merge pull request #4470 from sul-dlss/oclc-citation-multi-type
Browse files Browse the repository at this point in the history
Use new OCLC Citation API feature to fetch multiple citation styles per request
  • Loading branch information
jcoyne authored Oct 28, 2024
2 parents f9f9416 + 2897ea3 commit 477b2d2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
4 changes: 1 addition & 3 deletions app/models/citations/oclc_citation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def transform_grouped_citations(grouped_citations)
end

def oclc_citations
CITATION_STYLES.flat_map do |citation_style|
Thread.new { oclc_client.citations(oclc_numbers:, citation_style:) }
end.flat_map(&:value)
oclc_client.citations(oclc_numbers: oclc_numbers, citation_styles: CITATION_STYLES)
end

def oclc_client
Expand Down
10 changes: 5 additions & 5 deletions app/services/oclc_discovery_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ def ping

# Fetch one or more citations from the OCLC Discovery API
# @param [Array] or [String] the OCLC numbers to fetch citations for
# @param [String] citation_style the citation style to fetch (only one style may be requested at a time)
# @param [Array] or [String] citation_styles one or more citation styles to fetch
# @return [Array] one or more citation responses
def citations(oclc_numbers:, citation_style: 'apa')
def citations(oclc_numbers:, citation_styles: ['apa'])
Array(oclc_numbers).each_slice(MAX_CITATIONS_PER_REQUEST).map do |ids|
Thread.new { get_json(citation_query(ids.join(','), citation_style)) }
Thread.new { get_json(citation_query(ids.join(','), Array(citation_styles).join(','))) }
end.map(&:value)
end

private

# OCLC Citation API documentation:
# https://developer.api.oclc.org/citations-api
def citation_query(oclc_number, citation_style)
query = { oclcNumbers: oclc_number, style: citation_style }.to_query
def citation_query(oclc_number, citation_styles)
query = { oclcNumbers: oclc_number, style: citation_styles }.to_query
"/reference/citations?#{query}"
end

Expand Down
2 changes: 1 addition & 1 deletion spec/services/oclc_discovery_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
end

describe '#citations' do
subject(:citations) { client.citations(oclc_numbers: '905869', citation_style: 'modern-language-association') }
subject(:citations) { client.citations(oclc_numbers: '905869', citation_styles: 'modern-language-association') }

before do
stub_request(:get, "https://oclc.example.edu/reference/citations?oclcNumbers=905869&style=modern-language-association")
Expand Down

0 comments on commit 477b2d2

Please sign in to comment.