Extract Pet.fetch_viewer_data to a class method

This will make it easy to look up pet data from Neopets without messing
around with how the Pet model handles it!
This commit is contained in:
Emi Matchu 2024-01-24 00:51:20 -08:00
parent c76e8cd2a9
commit c33f1cb767

View file

@ -11,7 +11,7 @@ class Pet < ApplicationRecord
belongs_to :pet_type belongs_to :pet_type
attr_reader :items, :pet_state attr_reader :items, :pet_state, :viewer_data
scope :with_pet_type_color_ids, ->(color_ids) { scope :with_pet_type_color_ids, ->(color_ids) {
joins(:pet_type).where(PetType.arel_table[:id].in(color_ids)) joins(:pet_type).where(PetType.arel_table[:id].in(color_ids))
@ -20,7 +20,10 @@ class Pet < ApplicationRecord
def load!(options={}) def load!(options={})
options[:locale] ||= I18n.default_locale options[:locale] ||= I18n.default_locale
I18n.with_locale(options.delete(:locale)) do I18n.with_locale(options.delete(:locale)) do
use_viewer_data(fetch_viewer_data(options.delete(:timeout)), options) use_viewer_data(
self.class.fetch_viewer_data(name, options.delete(:timeout)),
options,
)
end end
true true
end end
@ -44,30 +47,6 @@ class Pet < ApplicationRecord
options[:item_scope]) options[:item_scope])
end end
# NOTE: Ideally pet requests shouldn't take this long, but Neopets can be
# slow sometimes! Since we're on the Falcon server, long timeouts shouldn't
# slow down the rest of the request queue, like it used to be in the past.
def fetch_viewer_data(timeout=10, locale=nil)
locale ||= I18n.default_locale
begin
neopets_language_code = I18n.compatible_neopets_language_code_for(locale)
envelope = PET_VIEWER.request([name, 0]).post(
:timeout => timeout,
:headers => {
'Cookie' => "lang=#{neopets_language_code}"
}
)
rescue RocketAMF::RemoteGateway::AMFError => e
if e.message == PET_NOT_FOUND_REMOTE_ERROR
raise PetNotFound, "Pet #{name.inspect} does not exist"
end
raise DownloadError, e.message
rescue RocketAMF::RemoteGateway::ConnectionError => e
raise DownloadError, e.message, e.backtrace
end
HashWithIndifferentAccess.new(envelope.messages[0].data.body)
end
def wardrobe_query def wardrobe_query
{ {
:name => self.name, :name => self.name,
@ -114,6 +93,30 @@ class Pet < ApplicationRecord
pet pet
end end
# NOTE: Ideally pet requests shouldn't take this long, but Neopets can be
# slow sometimes! Since we're on the Falcon server, long timeouts shouldn't
# slow down the rest of the request queue, like it used to be in the past.
def self.fetch_viewer_data(name, timeout=10, locale=nil)
locale ||= I18n.default_locale
begin
neopets_language_code = I18n.compatible_neopets_language_code_for(locale)
envelope = PET_VIEWER.request([name, 0]).post(
:timeout => timeout,
:headers => {
'Cookie' => "lang=#{neopets_language_code}"
}
)
rescue RocketAMF::RemoteGateway::AMFError => e
if e.message == PET_NOT_FOUND_REMOTE_ERROR
raise PetNotFound, "Pet #{name.inspect} does not exist"
end
raise DownloadError, e.message
rescue RocketAMF::RemoteGateway::ConnectionError => e
raise DownloadError, e.message, e.backtrace
end
HashWithIndifferentAccess.new(envelope.messages[0].data.body)
end
class PetNotFound < Exception;end class PetNotFound < Exception;end
class DownloadError < Exception;end class DownloadError < Exception;end
end end