Add Prismatic styles to Styling Studio auto-importer

I missed this the first time, and had just kinda been putting it off.
But now there's a lot of new Prismatics, so let's finally do it!
This commit is contained in:
Emi Matchu 2024-12-28 15:52:48 -08:00
parent b61526f6ad
commit d75492bd1d
2 changed files with 47 additions and 15 deletions

View file

@ -38,8 +38,20 @@ module Neopets::NCMall
uniq uniq
end end
STYLING_STUDIO_URL = "https://www.neopets.com/np-templates/ajax/stylingstudio/studio.php"
def self.load_styles(species_id:, neologin:) def self.load_styles(species_id:, neologin:)
Sync do
tabs = [
Async { load_styles_tab(species_id:, neologin:, tab: 1) },
Async { load_styles_tab(species_id:, neologin:, tab: 2) },
]
tabs.map(&:wait).flatten(1)
end
end
private
STYLING_STUDIO_URL = "https://www.neopets.com/np-templates/ajax/stylingstudio/studio.php"
def self.load_styles_tab(species_id:, neologin:, tab:)
Sync do Sync do
DTIRequests.post( DTIRequests.post(
STYLING_STUDIO_URL, STYLING_STUDIO_URL,
@ -48,7 +60,7 @@ module Neopets::NCMall
["Cookie", "neologin=#{neologin}"], ["Cookie", "neologin=#{neologin}"],
["X-Requested-With", "XMLHttpRequest"], ["X-Requested-With", "XMLHttpRequest"],
], ],
{tab: 1, mode: "getStyles", species: species_id}.to_query, {tab:, mode: "getStyles", species: species_id}.to_query,
) do |response| ) do |response|
if response.status != 200 if response.status != 200
raise ResponseNotOK.new(response.status), raise ResponseNotOK.new(response.status),
@ -60,7 +72,8 @@ module Neopets::NCMall
# HACK: styles is a hash, unless it's empty, in which case it's an # HACK: styles is a hash, unless it's empty, in which case it's an
# array? Weird. Normalize this by converting to hash. # array? Weird. Normalize this by converting to hash.
data.fetch(:styles).to_h.values data.fetch(:styles).to_h.values.
map { |s| s.slice(:oii, :name, :image, :limited) }
rescue JSON::ParserError, KeyError rescue JSON::ParserError, KeyError
raise UnexpectedResponseFormat raise UnexpectedResponseFormat
end end
@ -68,8 +81,6 @@ module Neopets::NCMall
end end
end end
private
def self.load_page_by_url(url) def self.load_page_by_url(url)
Sync do Sync do
DTIRequests.get(url) do |response| DTIRequests.get(url) do |response|

View file

@ -3,7 +3,7 @@ require_relative '../rails_helper'
RSpec.describe Neopets::NCMall, type: :model do RSpec.describe Neopets::NCMall, type: :model do
describe ".load_styles" do describe ".load_styles" do
def stub_styles_request def stub_styles_request(tab:)
stub_request(:post, "https://www.neopets.com/np-templates/ajax/stylingstudio/studio.php"). stub_request(:post, "https://www.neopets.com/np-templates/ajax/stylingstudio/studio.php").
with( with(
headers: { headers: {
@ -12,10 +12,15 @@ RSpec.describe Neopets::NCMall, type: :model do
"Cookie": "neologin=STUB_NEOLOGIN", "Cookie": "neologin=STUB_NEOLOGIN",
"User-Agent": Rails.configuration.user_agent_for_neopets, "User-Agent": Rails.configuration.user_agent_for_neopets,
}, },
body: "mode=getStyles&species=2&tab=1", body: "mode=getStyles&species=2&tab=#{tab}",
) )
end end
def empty_styles_response
# You'd think styles would be `{}` in this case, but it's `[]`. Huh!
{ body: '{"success":true,"styles":[]}' }
end
subject(:styles) do subject(:styles) do
Neopets::NCMall.load_styles( Neopets::NCMall.load_styles(
species_id: 2, species_id: 2,
@ -24,9 +29,12 @@ RSpec.describe Neopets::NCMall, type: :model do
end end
it "loads current NC styles from the NC Mall" do it "loads current NC styles from the NC Mall" do
stub_styles_request.to_return( stub_styles_request(tab: 1).to_return(
body: '{"success":true,"styles":{"87966":{"oii":87966,"name":"Nostalgic Alien Aisha","image":"https:\/\/images.neopets.com\/items\/nostalgic_alien_aisha.gif","limited":false},"87481":{"oii":87481,"name":"Nostalgic Sponge Aisha","image":"https:\/\/images.neopets.com\/items\/nostalgic_sponge_aisha.gif","limited":false},"90031":{"oii":90031,"name":"Celebratory Anniversary Aisha","image":"https:\/\/images.neopets.com\/items\/624dc08bcf.gif","limited":true},"90050":{"oii":90050,"name":"Nostalgic Tyrannian Aisha","image":"https:\/\/images.neopets.com\/items\/b225e06541.gif","limited":true}}}', body: '{"success":true,"styles":{"87966":{"oii":87966,"name":"Nostalgic Alien Aisha","image":"https:\/\/images.neopets.com\/items\/nostalgic_alien_aisha.gif","limited":false},"87481":{"oii":87481,"name":"Nostalgic Sponge Aisha","image":"https:\/\/images.neopets.com\/items\/nostalgic_sponge_aisha.gif","limited":false},"90031":{"oii":90031,"name":"Celebratory Anniversary Aisha","image":"https:\/\/images.neopets.com\/items\/624dc08bcf.gif","limited":true},"90050":{"oii":90050,"name":"Nostalgic Tyrannian Aisha","image":"https:\/\/images.neopets.com\/items\/b225e06541.gif","limited":true}}}',
) )
stub_styles_request(tab: 2).to_return(
body: '{"success":true,"styles":{"90338":{"oii":90338,"name":"Prismatic Pine: Christmas Aisha","image":"https:\/\/images.neopets.com\/items\/3m182ff5.gif","limited":true,"style":90252},"90348":{"oii":90348,"name":"Prismatic Tinsel: Christmas Aisha","image":"https:\/\/images.neopets.com\/items\/4j29mb91.gif","limited":true,"style":90252}}}'
)
expect(styles).to contain_exactly( expect(styles).to contain_exactly(
{ {
@ -52,37 +60,50 @@ RSpec.describe Neopets::NCMall, type: :model do
name: "Nostalgic Tyrannian Aisha", name: "Nostalgic Tyrannian Aisha",
image: "https://images.neopets.com/items/b225e06541.gif", image: "https://images.neopets.com/items/b225e06541.gif",
limited: true, limited: true,
},
{
oii: 90338,
name: "Prismatic Pine: Christmas Aisha",
image: "https://images.neopets.com/items/3m182ff5.gif",
limited: true,
},
{
oii: 90348,
name: "Prismatic Tinsel: Christmas Aisha",
image: "https://images.neopets.com/items/4j29mb91.gif",
limited: true,
} }
) )
end end
it "handles the NC Mall's odd API behavior for zero styles" do it "handles the NC Mall's odd API behavior for zero styles" do
stub_styles_request.to_return( stub_styles_request(tab: 1).to_return(empty_styles_response)
# You'd think styles would be `{}` in this case, but it's `[]`. Huh! stub_styles_request(tab: 2).to_return(empty_styles_response)
body: '{"success":true,"styles":[]}',
)
expect(styles).to be_empty expect(styles).to be_empty
end end
it "raises an error if the request returns a non-200 status" do it "raises an error if the request returns a non-200 status" do
stub_styles_request.to_return(status: 400) stub_styles_request(tab: 1).to_return(status: 400)
stub_styles_request(tab: 2).to_return(empty_styles_response)
expect { styles }.to raise_error(Neopets::NCMall::ResponseNotOK) expect { styles }.to raise_error(Neopets::NCMall::ResponseNotOK)
end end
it "raises an error if the request returns a non-JSON response" do it "raises an error if the request returns a non-JSON response" do
stub_styles_request.to_return( stub_styles_request(tab: 1).to_return(
body: "Oops, this request failed for some weird reason!", body: "Oops, this request failed for some weird reason!",
) )
stub_styles_request(tab: 2).to_return(empty_styles_response)
expect { styles }.to raise_error(Neopets::NCMall::UnexpectedResponseFormat) expect { styles }.to raise_error(Neopets::NCMall::UnexpectedResponseFormat)
end end
it "raises an error if the request returns unexpected JSON" do it "raises an error if the request returns unexpected JSON" do
stub_styles_request.to_return( stub_styles_request(tab: 1).to_return(
body: '{"success": false}', body: '{"success": false}',
) )
stub_styles_request(tab: 2).to_return(empty_styles_response)
expect { styles }.to raise_error(Neopets::NCMall::UnexpectedResponseFormat) expect { styles }.to raise_error(Neopets::NCMall::UnexpectedResponseFormat)
end end