forked from OpenNeo/impress
load pet by locale, prepare needed translations
This commit is contained in:
parent
9c8f7f498f
commit
29ed401238
9 changed files with 68 additions and 27 deletions
|
@ -265,6 +265,11 @@ class Item < ActiveRecord::Base
|
||||||
def parent_swf_asset_relationships_to_update=(rels)
|
def parent_swf_asset_relationships_to_update=(rels)
|
||||||
@parent_swf_asset_relationships_to_update = rels
|
@parent_swf_asset_relationships_to_update = rels
|
||||||
end
|
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)
|
def self.all_by_ids_or_children(ids, swf_assets)
|
||||||
swf_asset_ids = []
|
swf_asset_ids = []
|
||||||
|
|
|
@ -2,8 +2,8 @@ require 'rocketamf/remote_gateway'
|
||||||
|
|
||||||
class Pet < ActiveRecord::Base
|
class Pet < ActiveRecord::Base
|
||||||
GATEWAY_URL = 'http://www.neopets.com/amfphp/gateway.php'
|
GATEWAY_URL = 'http://www.neopets.com/amfphp/gateway.php'
|
||||||
AMF_SERVICE_NAME = 'CustomPetService'
|
PET_VIEWER = RocketAMF::RemoteGateway.new(GATEWAY_URL).
|
||||||
PET_VIEWER_METHOD = 'getViewerData'
|
service('CustomPetService').action('getViewerData')
|
||||||
PET_NOT_FOUND_REMOTE_ERROR = 'PHP: Unable to retrieve records from the database.'
|
PET_NOT_FOUND_REMOTE_ERROR = 'PHP: Unable to retrieve records from the database.'
|
||||||
WARDROBE_PATH = '/wardrobe'
|
WARDROBE_PATH = '/wardrobe'
|
||||||
|
|
||||||
|
@ -19,8 +19,13 @@ class Pet < ActiveRecord::Base
|
||||||
def load!
|
def load!
|
||||||
require 'ostruct'
|
require 'ostruct'
|
||||||
begin
|
begin
|
||||||
envelope = Pet.amf_service.request(PET_VIEWER_METHOD, name, nil).
|
neopets_language_code = I18n.translate('neopets_language_code')
|
||||||
fetch(:timeout => 2)
|
envelope = PET_VIEWER.request([name, 0]).post(
|
||||||
|
:timeout => 2,
|
||||||
|
:headers => {
|
||||||
|
'Cookie' => "lang=#{neopets_language_code}"
|
||||||
|
}
|
||||||
|
)
|
||||||
rescue RocketAMF::RemoteGateway::AMFError => e
|
rescue RocketAMF::RemoteGateway::AMFError => e
|
||||||
if e.message == PET_NOT_FOUND_REMOTE_ERROR
|
if e.message == PET_NOT_FOUND_REMOTE_ERROR
|
||||||
raise PetNotFound, "Pet #{name.inspect} does not exist"
|
raise PetNotFound, "Pet #{name.inspect} does not exist"
|
||||||
|
@ -64,6 +69,20 @@ class Pet < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
contributables
|
contributables
|
||||||
end
|
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
|
before_validation do
|
||||||
pet_type.save!
|
pet_type.save!
|
||||||
|
@ -86,19 +105,6 @@ class Pet < ActiveRecord::Base
|
||||||
pet
|
pet
|
||||||
end
|
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 PetNotFound < Exception;end
|
||||||
class DownloadError < Exception;end
|
class DownloadError < Exception;end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,7 @@ en-MEEP:
|
||||||
infinite_closet: Infinite Meepit
|
infinite_closet: Infinite Meepit
|
||||||
modeling_hub: Meepiting Hub
|
modeling_hub: Meepiting Hub
|
||||||
locale_name: English (meep!)
|
locale_name: English (meep!)
|
||||||
|
neopets_language_code: en
|
||||||
|
|
||||||
activerecord:
|
activerecord:
|
||||||
attributes:
|
attributes:
|
||||||
|
|
|
@ -5,6 +5,7 @@ en:
|
||||||
infinite_closet: Infinite Closet
|
infinite_closet: Infinite Closet
|
||||||
modeling_hub: Modeling Hub
|
modeling_hub: Modeling Hub
|
||||||
locale_name: English
|
locale_name: English
|
||||||
|
neopets_language_code: en
|
||||||
|
|
||||||
activerecord:
|
activerecord:
|
||||||
attributes:
|
attributes:
|
||||||
|
|
6
config/locales/pt.yml
Normal file
6
config/locales/pt.yml
Normal 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.
|
|
@ -1,7 +1,6 @@
|
||||||
require 'net/http'
|
require 'net/http'
|
||||||
require 'rocketamf'
|
require 'rocketamf'
|
||||||
require File.join(File.dirname(__FILE__), 'remote_gateway', 'service')
|
require File.join(File.dirname(__FILE__), 'remote_gateway', 'service')
|
||||||
require File.join(File.dirname(__FILE__), 'remote_gateway', 'request')
|
|
||||||
|
|
||||||
module RocketAMF
|
module RocketAMF
|
||||||
class RemoteGateway
|
class RemoteGateway
|
||||||
|
|
18
lib/rocketamf/remote_gateway/action.rb
Normal file
18
lib/rocketamf/remote_gateway/action.rb
Normal 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
|
|
@ -5,18 +5,21 @@ module RocketAMF
|
||||||
class Request
|
class Request
|
||||||
ERROR_CODE = 'AMFPHP_RUNTIME_ERROR'
|
ERROR_CODE = 'AMFPHP_RUNTIME_ERROR'
|
||||||
|
|
||||||
def initialize(service, method, *params)
|
def initialize(action, params)
|
||||||
@service = service
|
@action = action
|
||||||
@method = method
|
|
||||||
@params = params
|
@params = params
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch(options={})
|
def post(options={})
|
||||||
uri = @service.gateway.uri
|
uri = @action.service.gateway.uri
|
||||||
data = envelope.serialize
|
data = envelope.serialize
|
||||||
|
|
||||||
req = Net::HTTP::Post.new(uri.path)
|
req = Net::HTTP::Post.new(uri.path)
|
||||||
req.body = data
|
req.body = data
|
||||||
|
headers = options[:headers] || {}
|
||||||
|
headers.each do |key, value|
|
||||||
|
req[key] = value
|
||||||
|
end
|
||||||
|
|
||||||
res = nil
|
res = nil
|
||||||
|
|
||||||
|
@ -68,8 +71,8 @@ module RocketAMF
|
||||||
|
|
||||||
def remoting_message
|
def remoting_message
|
||||||
message = Values::RemotingMessage.new
|
message = Values::RemotingMessage.new
|
||||||
message.source = @service.name
|
message.source = @action.service.name
|
||||||
message.operation = @method
|
message.operation = @action.name
|
||||||
message.body = @params
|
message.body = @params
|
||||||
message
|
message
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require File.join(File.dirname(__FILE__), 'action')
|
||||||
|
|
||||||
module RocketAMF
|
module RocketAMF
|
||||||
class RemoteGateway
|
class RemoteGateway
|
||||||
class Service
|
class Service
|
||||||
|
@ -8,8 +10,8 @@ module RocketAMF
|
||||||
@name = name
|
@name = name
|
||||||
end
|
end
|
||||||
|
|
||||||
def request(method, *params)
|
def action(name)
|
||||||
Request.new(self, method, *params)
|
Action.new(self, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue