1
0
Fork 1

Fix bug parsing discount time when importing from NC Mall

Previously, we took the incoming date as just an integer, which was
raising a database error when we attempted to save it to a timestamp
field. Now, we correctly parse it into a timestamp.

I imagine we just haven't run the import manually while there's a real
discount in the records before? Weird!
This commit is contained in:
Emi Matchu 2025-01-18 10:49:04 -08:00
parent 86a1875d6d
commit b3f635c96c
2 changed files with 49 additions and 2 deletions
app/services/neopets
spec/services

View file

@ -136,14 +136,15 @@ module Neopets::NCMall
end end
# Given item info, return a hash of discount-specific info, if any. # Given item info, return a hash of discount-specific info, if any.
NST = Time.find_zone("Pacific Time (US & Canada)")
def self.parse_item_discount(item_info) def self.parse_item_discount(item_info)
discount_price = item_info["discountPrice"] discount_price = item_info["discountPrice"]
return nil unless discount_price.present? && discount_price > 0 return nil unless discount_price.present? && discount_price > 0
{ {
price: discount_price, price: discount_price,
begins_at: item_info["discountBegin"], begins_at: NST.at(item_info["discountBegin"]),
ends_at: item_info["discountEnd"], ends_at: NST.at(item_info["discountEnd"]),
} }
end end

View file

@ -2,6 +2,52 @@ require 'webmock/rspec'
require_relative '../rails_helper' require_relative '../rails_helper'
RSpec.describe Neopets::NCMall, type: :model do RSpec.describe Neopets::NCMall, type: :model do
describe ".load_page" do
def stub_page_request
stub_request(:get, "https://ncmall.neopets.com/mall/ajax/load_page.phtml?type=new&cat=52&lang=en").
with(
headers: {
"User-Agent": Rails.configuration.user_agent_for_neopets,
},
)
end
subject(:page) do
Neopets::NCMall.load_page("new", 52)
end
it "loads a page from the NC Mall" do
stub_page_request.to_return(
body: '{"html":"","render_html":"0","render":[82936,90226],"object_data":{"82936":{"id":82936,"name":"+1 Extra Pet Slot","description":"Just ONE more Neopet... just ONE more...! This pack includes 1 extra pet slot. Each extra pet slot can be used to create a new pet, adopt a pet, or bring back any idle pets lost from non-premium accounts.","price":500,"discountPrice":0,"atPurchaseDiscountPrice":null,"discountBegin":1735372800,"discountEnd":1735718399,"uses":1,"isSuperpack":0,"isBundle":0,"packContents":null,"isAvailable":1,"imageFile":"mall_petslots_1","saleBegin":1703094300,"saleEnd":0,"duration":0,"isSoldOut":0,"isNeohome":0,"isWearable":0,"isBuyable":1,"isAlbumTheme":0,"isGiftbox":0,"isInRandomWindow":null,"isElite":0,"isCollectible":0,"isKeyquest":0,"categories":null,"isHabitarium":0,"isNoInvInsert":1,"isLimitedQuantity":0,"isPresale":0,"isGambling":0,"petSlotPack":1,"maxPetSlots":10,"currentUserBoughtPetSlots":0,"formatted":{"name":"+1 Extra Pet Slot","ck":false,"price":"500","discountPrice":"0","limited":false},"converted":true},"90226":{"id":90226,"name":"Weekend Sales 2025 Mega Gram","description":"Lets go shopping! Purchase this Weekend Sales Mega Gram and choose from exclusive Weekend Sales items to send to a Neofriend, no gift box needed! This gram also has a chance of including a Limited Edition NC item. Please visit the NC Mall FAQs for more information on this item.","price":250,"discountPrice":125,"atPurchaseDiscountPrice":null,"discountBegin":1737136800,"discountEnd":1737446399,"uses":1,"isSuperpack":0,"isBundle":0,"packContents":null,"isAvailable":1,"imageFile":"42embjc204","saleBegin":1737136800,"saleEnd":1739865599,"duration":0,"isSoldOut":0,"isNeohome":0,"isWearable":0,"isBuyable":1,"isAlbumTheme":0,"isGiftbox":0,"isInRandomWindow":null,"isElite":0,"isCollectible":0,"isKeyquest":0,"categories":null,"isHabitarium":0,"isNoInvInsert":0,"isLimitedQuantity":0,"isPresale":0,"isGambling":0,"formatted":{"name":"Weekend Sales 2025 Mega Gram","ck":false,"price":"250","discountPrice":"125","limited":false},"converted":true}},"response":{"category":"52","type":"new","image":{"location":"//images.neopets.com/items/","star_location":"//images.neopets.com/ncmall/","extension":".gif","stars":{"blue":"star_blue","red":"star_red","orange":"star_orange","leso":"leso_star"}},"heading":"New","no_items_msg":"","shopkeeper":{"img":"//images.neopets.com/ncmall/shopkeepers/mall_new.jpg","title":"Style is all about what\'s new… good thing that\'s all I stock!","message":"Come browse my shop and find the latest and greatest the NC Mall has to offer!","new_format":true},"strings":{"claim_it":"Claim it","none_left":"Sorry, there are none left!","nc":"NC","free":"FREE","add_to_cart":"Add to cart"}}}'
)
expect(page[:items]).to contain_exactly(
{
id: 82936,
name: "+1 Extra Pet Slot",
description: "Just ONE more Neopet... just ONE more...! This pack includes 1 extra pet slot. Each extra pet slot can be used to create a new pet, adopt a pet, or bring back any idle pets lost from non-premium accounts.",
price: 500,
discount: nil,
is_available: true,
},
{
id: 90226,
name: "Weekend Sales 2025 Mega Gram",
description: "Lets go shopping! Purchase this Weekend Sales Mega Gram and choose from exclusive Weekend Sales items to send to a Neofriend, no gift box needed! This gram also has a chance of including a Limited Edition NC item. Please visit the NC Mall FAQs for more information on this item.",
price: 250,
discount: {
price: 125,
begins_at: Time.find_zone("Pacific Time (US & Canada)").
local(2025, 1, 17, 10),
ends_at: Time.find_zone("Pacific Time (US & Canada)").
local(2025, 1, 20, 23, 59, 59),
},
is_available: true,
},
)
end
end
describe ".load_styles" do describe ".load_styles" do
def stub_styles_request(tab:) 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").