From c78d51ab01b1b71265034bf61b76c1dd37d19545 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Wed, 29 May 2024 18:36:30 -0700 Subject: [PATCH] Don't hardcode the series name in AltStyle thumbnail_url This is just a bit of future-proofing! We also add a default thumbnail URL of the cute "Neopets Circle Background", for cases where the series name isn't known yet. --- app/models/alt_style.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/models/alt_style.rb b/app/models/alt_style.rb index 15e43810..9ebc38e0 100644 --- a/app/models/alt_style.rb +++ b/app/models/alt_style.rb @@ -1,3 +1,5 @@ +require "addressable/template" + class AltStyle < ApplicationRecord belongs_to :species belongs_to :color @@ -35,11 +37,20 @@ class AltStyle < ApplicationRecord "#{series_name} #{color.human_name}" end + THUMBNAIL_URL_TEMPLATE = Addressable::Template.new( + "https://images.neopets.com/items/{series}_{color}_{species}.gif" + ) + DEFAULT_THUMBNAIL_URL = "https://images.neopets.com/items/mall_bg_circle.gif" def thumbnail_url - # HACK: Just assume this is a Nostalgic Alt Style, and that the thumbnail - # is named reliably! - "https://images.neopets.com/items/nostalgic_" + - "#{color.name.gsub(/\s+/, '').downcase}_#{species.name.downcase}.gif" + return DEFAULT_THUMBNAIL_URL unless has_real_series_name? + + # HACK: We're assuming this is the format long-term! But if it changes, we + # may need to add it as a database field instead. + THUMBNAIL_URL_TEMPLATE.expand( + series: series_name.gsub(/\s+/, '').downcase, + color: color.name.gsub(/\s+/, '').downcase, + species: species.name.gsub(/\s+/, '').downcase, + ).to_s end def preview_image_url