diff --git a/app/services/neopets/nc_mall.rb b/app/services/neopets/nc_mall.rb index 5ae94170..5f9cbec8 100644 --- a/app/services/neopets/nc_mall.rb +++ b/app/services/neopets/nc_mall.rb @@ -65,7 +65,10 @@ module Neopets::NCMall begin data = JSON.parse(response.read).deep_symbolize_keys - data.fetch(:styles).values + + # HACK: styles is a hash, unless it's empty, in which case it's an + # array? Weird. Normalize this by converting to hash. + data.fetch(:styles).to_h.values rescue JSON::ParserError, KeyError raise UnexpectedResponseFormat end diff --git a/spec/services/nc_mall_spec.rb b/spec/services/nc_mall_spec.rb index 656f4fa5..4df9315b 100644 --- a/spec/services/nc_mall_spec.rb +++ b/spec/services/nc_mall_spec.rb @@ -55,6 +55,15 @@ RSpec.describe Neopets::NCMall, type: :model do ) end + it "handles the NC Mall's odd API behavior for zero styles" do + stub_styles_request.to_return( + # You'd think styles would be `{}` in this case, but it's `[]`. Huh! + body: '{"success":true,"styles":[]}', + ) + + expect(styles).to be_empty + end + it "raises an error if the request returns a non-200 status" do stub_styles_request.to_return(status: 400)