From 73a49c1fecf467d658513e805ed1ce46c295d9f1 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Tue, 21 May 2024 17:54:12 -0700 Subject: [PATCH] Update item URL helpers (like Shop Wiz) to use URL templates This is just a tech update: instead of using hand-built URLs with `CGI::escape`, I use `Addressable::Template`, which is a more reliable way to build URLs in general. The motivation here is that I noticed the Shop Wizard link is actually broken! And I wanted to fix this while I was here, but I figured let's split that into a separate commit than this refactor. See next! --- app/helpers/items_helper.rb | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/app/helpers/items_helper.rb b/app/helpers/items_helper.rb index d2c5ff34..4c0a9f45 100644 --- a/app/helpers/items_helper.rb +++ b/app/helpers/items_helper.rb @@ -1,8 +1,6 @@ require "addressable/template" module ItemsHelper - JNItemsURLFormat = 'https://items.jellyneo.net/search/?name=%s&name_type=3' - module PetTypeImage Format = 'https://pets.neopets.com/cp/%s/%i/%i.png' @@ -82,8 +80,11 @@ module ItemsHelper first_seen_at.strftime("%b %Y") end + JN_ITEMS_URL_TEMPLATE = Addressable::Template.new( + "https://items.jellyneo.net/search/?name_type=3{&name}" + ) def jn_items_url_for(item) - sprintf(JNItemsURLFormat, CGI::escape(item.name)) + JN_ITEMS_URL_TEMPLATE.expand(name: item.name).to_s end IMPRESS_2020_ITEM_URL_TEMPLATE = Addressable::Template.new( @@ -93,20 +94,32 @@ module ItemsHelper IMPRESS_2020_ITEM_URL_TEMPLATE.expand(id: item.id).to_s end + SHOP_WIZARD_URL_TEMPLATE = Addressable::Template.new( + "https://www.neopets.com/market.phtml?type=wizard{&string}" + ) def shop_wizard_url_for(item) - "https://www.neopets.com/market.phtml?type=wizard&string=#{CGI::escape item.name}" + SHOP_WIZARD_URL_TEMPLATE.expand(string: item.name).to_s end + SUPER_SHOP_WIZARD_URL_TEMPLATE = Addressable::Template.new( + "https://www.neopets.com/portal/supershopwiz.phtml{?string}" + ) def super_shop_wizard_url_for(item) - "https://www.neopets.com/portal/supershopwiz.phtml?string=#{CGI::escape item.name}" + SUPER_SHOP_WIZARD_URL_TEMPLATE.expand(string: item.name).to_s end + TRADING_POST_URL_TEMPLATE = Addressable::Template.new( + "https://www.neopets.com/island/tradingpost.phtml?type=browse&criteria=item_exact{&search_string}" + ) def trading_post_url_for(item) - "https://www.neopets.com/island/tradingpost.phtml?type=browse&criteria=item_exact&search_string=#{CGI::escape item.name}" + TRADING_POST_URL_TEMPLATE.expand(search_string: item.name).to_s end + AUCTION_GENIE_URL_TEMPLATE = Addressable::Template.new( + "https://www.neopets.com/genie.phtml?type=process_genie&criteria=exact{&auctiongenie}" + ) def auction_genie_url_for(item) - "https://www.neopets.com/genie.phtml?type=process_genie&criteria=exact&auctiongenie=#{CGI::escape item.name}" + AUCTION_GENIE_URL_TEMPLATE.expand(auctiongenie: item.name).to_s end def format_contribution_count(count)