forked from OpenNeo/impress
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:
parent
c76e8cd2a9
commit
c33f1cb767
1 changed files with 29 additions and 26 deletions
|
@ -11,7 +11,7 @@ class Pet < ApplicationRecord
|
|||
|
||||
belongs_to :pet_type
|
||||
|
||||
attr_reader :items, :pet_state
|
||||
attr_reader :items, :pet_state, :viewer_data
|
||||
|
||||
scope :with_pet_type_color_ids, ->(color_ids) {
|
||||
joins(:pet_type).where(PetType.arel_table[:id].in(color_ids))
|
||||
|
@ -20,7 +20,10 @@ class Pet < ApplicationRecord
|
|||
def load!(options={})
|
||||
options[:locale] ||= I18n.default_locale
|
||||
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
|
||||
true
|
||||
end
|
||||
|
@ -43,30 +46,6 @@ class Pet < ApplicationRecord
|
|||
viewer_data[:object_info_registry], viewer_data[:object_asset_registry],
|
||||
options[:item_scope])
|
||||
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
|
||||
{
|
||||
|
@ -114,6 +93,30 @@ class Pet < ApplicationRecord
|
|||
pet
|
||||
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 DownloadError < Exception;end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue