diff --git a/app/models/item.rb b/app/models/item.rb index 1b6c065b..1285825f 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -265,6 +265,11 @@ class Item < ActiveRecord::Base def parent_swf_asset_relationships_to_update=(rels) @parent_swf_asset_relationships_to_update = rels end + + def needed_translations + translated_locales = Set.new(translations.map(&:locale)) + I18n.available_locales.reject { |locale| translated_locales.include?(locale) } + end def self.all_by_ids_or_children(ids, swf_assets) swf_asset_ids = [] diff --git a/app/models/pet.rb b/app/models/pet.rb index 94c5d4c8..336a1468 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -2,8 +2,8 @@ require 'rocketamf/remote_gateway' class Pet < ActiveRecord::Base GATEWAY_URL = 'http://www.neopets.com/amfphp/gateway.php' - AMF_SERVICE_NAME = 'CustomPetService' - PET_VIEWER_METHOD = 'getViewerData' + PET_VIEWER = RocketAMF::RemoteGateway.new(GATEWAY_URL). + service('CustomPetService').action('getViewerData') PET_NOT_FOUND_REMOTE_ERROR = 'PHP: Unable to retrieve records from the database.' WARDROBE_PATH = '/wardrobe' @@ -19,8 +19,13 @@ class Pet < ActiveRecord::Base def load! require 'ostruct' begin - envelope = Pet.amf_service.request(PET_VIEWER_METHOD, name, nil). - fetch(:timeout => 2) + neopets_language_code = I18n.translate('neopets_language_code') + envelope = PET_VIEWER.request([name, 0]).post( + :timeout => 2, + :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" @@ -64,6 +69,20 @@ class Pet < ActiveRecord::Base end contributables end + + def item_translation_candidates + {}.tap do |candidates| + if @items + @items.each do |item| + puts "#{item.name}: #{item.translations_needed}" + item.needed_translations.each do |locale| + candidates[locale] ||= [] + candidates[locale] << item + end + end + end + end + end before_validation do pet_type.save! @@ -86,19 +105,6 @@ class Pet < ActiveRecord::Base pet end - private - - def self.amf_service - @amf_service ||= gateway.service AMF_SERVICE_NAME - end - - def self.gateway - unless @gateway - @gateway = RocketAMF::RemoteGateway.new(GATEWAY_URL) - end - @gateway - end - class PetNotFound < Exception;end class DownloadError < Exception;end end diff --git a/config/locales/en-MEEP.yml b/config/locales/en-MEEP.yml index 35d05d94..c7f2e67d 100644 --- a/config/locales/en-MEEP.yml +++ b/config/locales/en-MEEP.yml @@ -5,6 +5,7 @@ en-MEEP: infinite_closet: Infinite Meepit modeling_hub: Meepiting Hub locale_name: English (meep!) + neopets_language_code: en activerecord: attributes: diff --git a/config/locales/en.yml b/config/locales/en.yml index 196c670a..c56d4081 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -5,6 +5,7 @@ en: infinite_closet: Infinite Closet modeling_hub: Modeling Hub locale_name: English + neopets_language_code: en activerecord: attributes: diff --git a/config/locales/pt.yml b/config/locales/pt.yml new file mode 100644 index 00000000..cd052a0e --- /dev/null +++ b/config/locales/pt.yml @@ -0,0 +1,6 @@ +pt: + locale_name: Portuguese + neopets_language_code: pt + + # This is a placeholder Portuguese locale, so that we can look up items. + # Everything else will fall back to English. diff --git a/lib/rocketamf/remote_gateway.rb b/lib/rocketamf/remote_gateway.rb index 5007093b..cc48e25c 100644 --- a/lib/rocketamf/remote_gateway.rb +++ b/lib/rocketamf/remote_gateway.rb @@ -1,7 +1,6 @@ require 'net/http' require 'rocketamf' require File.join(File.dirname(__FILE__), 'remote_gateway', 'service') -require File.join(File.dirname(__FILE__), 'remote_gateway', 'request') module RocketAMF class RemoteGateway diff --git a/lib/rocketamf/remote_gateway/action.rb b/lib/rocketamf/remote_gateway/action.rb new file mode 100644 index 00000000..eefbcd57 --- /dev/null +++ b/lib/rocketamf/remote_gateway/action.rb @@ -0,0 +1,18 @@ +require File.join(File.dirname(__FILE__), 'request') + +module RocketAMF + class RemoteGateway + class Action + attr_reader :service, :name + + def initialize(service, name) + @service = service + @name = name + end + + def request(params) + Request.new(self, params) + end + end + end +end diff --git a/lib/rocketamf/remote_gateway/request.rb b/lib/rocketamf/remote_gateway/request.rb index 7b983720..a2b0bfcb 100644 --- a/lib/rocketamf/remote_gateway/request.rb +++ b/lib/rocketamf/remote_gateway/request.rb @@ -5,18 +5,21 @@ module RocketAMF class Request ERROR_CODE = 'AMFPHP_RUNTIME_ERROR' - def initialize(service, method, *params) - @service = service - @method = method + def initialize(action, params) + @action = action @params = params end - def fetch(options={}) - uri = @service.gateway.uri + def post(options={}) + uri = @action.service.gateway.uri data = envelope.serialize req = Net::HTTP::Post.new(uri.path) req.body = data + headers = options[:headers] || {} + headers.each do |key, value| + req[key] = value + end res = nil @@ -68,8 +71,8 @@ module RocketAMF def remoting_message message = Values::RemotingMessage.new - message.source = @service.name - message.operation = @method + message.source = @action.service.name + message.operation = @action.name message.body = @params message end diff --git a/lib/rocketamf/remote_gateway/service.rb b/lib/rocketamf/remote_gateway/service.rb index b510582e..6f19aa74 100644 --- a/lib/rocketamf/remote_gateway/service.rb +++ b/lib/rocketamf/remote_gateway/service.rb @@ -1,3 +1,5 @@ +require File.join(File.dirname(__FILE__), 'action') + module RocketAMF class RemoteGateway class Service @@ -8,8 +10,8 @@ module RocketAMF @name = name end - def request(method, *params) - Request.new(self, method, *params) + def action(name) + Action.new(self, name) end end end