Skip to content

Commit

Permalink
Merge pull request #368 from openstax/fix_search_again
Browse files Browse the repository at this point in the history
Fix search again
  • Loading branch information
Dantemss authored May 10, 2022
2 parents c1e47d6 + 297c372 commit 91451ce
Show file tree
Hide file tree
Showing 5 changed files with 4,743 additions and 4,225 deletions.
52 changes: 30 additions & 22 deletions app/controllers/api/v1/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,41 @@ def index
# show #
########/

api :GET, '/books/:uuid(/:version)', 'Returns a list of pages in some book version or latest'
description 'Returns a list of pages in some book version (default: latest version)'
api :GET, '/books/:uuid', 'Returns a list of pages in some book'
description 'Returns a list of pages in some book in some archive version (default: latest)'
param :archive_version, String, desc: 'Archive code pipeline version (default: latest version)'
param :uuid, String, desc: 'Book uuid'
param :version, String, desc: 'Book version (defaults to latest version in S3 bucket)'
def show
OSU::AccessPolicy.require_action_allowed! :read, current_api_user, OpenStax::Content::Book

uuid = params[:uuid]
version = params[:version] || available_book_versions_by_uuid[uuid].last

tree = OpenStax::Content::Book.new(
archive: archive, uuid: uuid, version: version
).tree['contents'] rescue []
book = abl.approved_books(archive: archive).find { |book| book.uuid == params[:uuid] }
tree = []

loop do
begin
tree = book.tree['contents']
rescue StandardError => exception
# Sometimes books in the ABL fail to load
# Retry with an earlier version of archive, if possible
previous_version ||= book.archive.previous_version

# break from the loop if there are no more archive versions to try
break if previous_version.nil?

previous_archive ||= OpenStax::Content::Archive.new version: previous_version

book = OpenStax::Content::Book.new(
archive: previous_archive,
uuid: book.uuid,
version: book.version,
slug: book.slug,
style: book.style
)
else
# break from the loop if successful
break
end
end

render json: tree
end
Expand Down Expand Up @@ -73,18 +94,5 @@ def archive_version
def archive
@archive ||= OpenStax::Content::Archive.new(version: archive_version)
end

def available_book_versions_by_uuid
@available_book_versions_by_uuid ||= Hash.new { |hash, key| hash[key] = [] }.tap do |hash|
s3.ls(archive_version).each do |book|
uuid, version = book.split('@')
hash[uuid] << version
end
end.tap do |hash|
hash.values.each do |versions|
versions.sort_by! { |version| version.split('.').map(&:to_i) }
end
end
end
end
end
Loading

0 comments on commit 91451ce

Please sign in to comment.