batch_preview: accept pre-fetched wiki_data from client

This commit is contained in:
Justin Edmund 2025-12-14 13:12:25 -08:00
parent 9e72e828f7
commit f589f58eb5
4 changed files with 24 additions and 14 deletions

View file

@ -163,6 +163,7 @@ module Api
# Fetches wiki data and suggestions for multiple wiki page names
def batch_preview
wiki_pages = params[:wiki_pages]
wiki_data = params[:wiki_data] || {}
unless wiki_pages.is_a?(Array) && wiki_pages.any?
return render json: { error: 'wiki_pages must be a non-empty array' }, status: :unprocessable_entity
@ -172,7 +173,7 @@ module Api
wiki_pages = wiki_pages.first(10)
results = wiki_pages.map do |wiki_page|
process_wiki_preview(wiki_page, :character)
process_wiki_preview(wiki_page, :character, wiki_raw: wiki_data[wiki_page])
end
render json: { results: results }

View file

@ -154,6 +154,7 @@ module Api
# Fetches wiki data and suggestions for multiple wiki page names
def batch_preview
wiki_pages = params[:wiki_pages]
wiki_data = params[:wiki_data] || {}
unless wiki_pages.is_a?(Array) && wiki_pages.any?
return render json: { error: 'wiki_pages must be a non-empty array' }, status: :unprocessable_entity
@ -163,7 +164,7 @@ module Api
wiki_pages = wiki_pages.first(10)
results = wiki_pages.map do |wiki_page|
process_wiki_preview(wiki_page, :summon)
process_wiki_preview(wiki_page, :summon, wiki_raw: wiki_data[wiki_page])
end
render json: { results: results }

View file

@ -154,6 +154,7 @@ module Api
# Fetches wiki data and suggestions for multiple wiki page names
def batch_preview
wiki_pages = params[:wiki_pages]
wiki_data = params[:wiki_data] || {}
unless wiki_pages.is_a?(Array) && wiki_pages.any?
return render json: { error: 'wiki_pages must be a non-empty array' }, status: :unprocessable_entity
@ -163,7 +164,7 @@ module Api
wiki_pages = wiki_pages.first(10)
results = wiki_pages.map do |wiki_page|
process_wiki_preview(wiki_page, :weapon)
process_wiki_preview(wiki_page, :weapon, wiki_raw: wiki_data[wiki_page])
end
render json: { results: results }

View file

@ -9,19 +9,25 @@ module BatchPreviewable
# Process a single wiki page and return preview data
# @param wiki_page [String] The wiki page name to fetch
# @param entity_type [Symbol] The type of entity (:character, :weapon, :summon)
# @param wiki_raw [String, nil] Pre-fetched wiki text (from client-side fetch)
# @return [Hash] Preview data including status, suggestions, and errors
def process_wiki_preview(wiki_page, entity_type)
def process_wiki_preview(wiki_page, entity_type, wiki_raw: nil)
result = {
wiki_page: wiki_page,
status: 'success'
}
begin
# Fetch wiki content
# Use provided wiki_raw or fetch from wiki
wiki_text = if wiki_raw.present?
wiki_raw
else
wiki = Granblue::Parsers::Wiki.new
wiki_text = wiki.fetch(wiki_page)
wiki.fetch(wiki_page)
end
# Handle redirects
# Handle redirects (only if we fetched server-side)
if wiki_raw.blank?
redirect_match = wiki_text.match(/#REDIRECT \[\[(.*?)\]\]/)
if redirect_match
redirect_target = redirect_match[1]
@ -29,6 +35,7 @@ module BatchPreviewable
result[:wiki_page] = redirect_target
wiki_text = wiki.fetch(redirect_target)
end
end
result[:wiki_raw] = wiki_text