load pet by locale, prepare needed translations

This commit is contained in:
Emi Matchu 2013-01-11 19:16:16 -06:00
parent 9c8f7f498f
commit 29ed401238
9 changed files with 68 additions and 27 deletions

View file

@ -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 = []

View file

@ -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

View file

@ -5,6 +5,7 @@ en-MEEP:
infinite_closet: Infinite Meepit
modeling_hub: Meepiting Hub
locale_name: English (meep!)
neopets_language_code: en
activerecord:
attributes:

View file

@ -5,6 +5,7 @@ en:
infinite_closet: Infinite Closet
modeling_hub: Modeling Hub
locale_name: English
neopets_language_code: en
activerecord:
attributes:

6
config/locales/pt.yml Normal file
View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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