From eb665f10a4945bb0f4620087de29e375182cd11d Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 28 May 2015 17:12:39 -0500 Subject: [PATCH] use MobileService for loading the pets for a username --- app/models/neopets_user.rb | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/app/models/neopets_user.rb b/app/models/neopets_user.rb index 27a8bada..6ce5e35f 100644 --- a/app/models/neopets_user.rb +++ b/app/models/neopets_user.rb @@ -1,9 +1,14 @@ +require 'rocketamf/remote_gateway' require 'open-uri' class NeopetsUser include ActiveModel::Conversion extend ActiveModel::Naming + GATEWAY_URL = 'http://www.neopets.com/amfphp/gateway.php' + GET_PETS_METHOD = RocketAMF::RemoteGateway.new(GATEWAY_URL). + service('MobileService').action('getPets') + attr_accessor :username attr_reader :hangers, :list_id @@ -27,15 +32,24 @@ class NeopetsUser end def load! - user = Neopets::User.new(@username) - + neopets_language_code = I18n.compatible_neopets_language_code_for(I18n.locale) begin - pets = user.pets - rescue Neopets::User::Error => e + envelope = GET_PETS_METHOD.request([@username]).post( + :timeout => 4, + :headers => { + 'Cookie' => "lang=#{neopets_language_code}" + } + ) + rescue RocketAMF::RemoteGateway::AMFError => e raise NotFound, e.message + rescue RocketAMF::RemoteGateway::ConnectionError => e + raise NotFound, e.message, e.backtrace end - pets = pets.map { |pet| Pet.find_or_initialize_by_name(pet.name) } + + pets_data = envelope.messages[0].data.body + raise NotFound if pets_data == false + pets = pets_data.map { |pet| Pet.find_or_initialize_by_name(pet['name']) } items = pets.each(&:load!).map(&:items).flatten item_ids = items.map(&:id) item_quantities = {}