forked from OpenNeo/impress
pet type now loads image hash on save
This commit is contained in:
parent
bb233359d8
commit
ff629b9419
2 changed files with 31 additions and 0 deletions
|
@ -25,6 +25,7 @@ class Pet < ActiveRecord::Base
|
|||
pet_data.color_id.to_i
|
||||
)
|
||||
self.pet_type.body_id = pet_data.body_id
|
||||
self.pet_type.origin_pet = self
|
||||
@pet_state = self.pet_type.add_pet_state_from_biology! pet_data.biology_by_zone
|
||||
@items = Item.collection_from_pet_type_and_registries(self.pet_type,
|
||||
contents.object_info_registry, contents.object_asset_registry)
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
require 'open-uri'
|
||||
|
||||
class PetType < ActiveRecord::Base
|
||||
IMAGE_CPN_FORMAT = 'http://pets.neopets.com/cpn/%s/1/1.png';
|
||||
IMAGE_CP_LOCATION_REGEX = %r{^/cp/(.+?)/1/1\.png$};
|
||||
|
||||
has_many :pet_states
|
||||
|
||||
attr_writer :origin_pet
|
||||
|
||||
BasicHashes = YAML::load_file(Rails.root.join('config', 'basic_type_hashes.yml'))
|
||||
|
||||
StandardBodyIds = PetType.select(arel_table[:body_id]).
|
||||
|
@ -60,4 +67,27 @@ class PetType < ActiveRecord::Base
|
|||
self.pet_states << pet_state
|
||||
pet_state
|
||||
end
|
||||
|
||||
def before_save
|
||||
if @origin_pet
|
||||
cpn_uri = URI.parse sprintf(IMAGE_CPN_FORMAT, @origin_pet.name);
|
||||
res = Net::HTTP.get_response(cpn_uri)
|
||||
unless res.is_a? Net::HTTPFound
|
||||
begin
|
||||
res.error!
|
||||
rescue Exception => e
|
||||
raise "Error loading CPN image at #{cpn_uri}: #{e.message}"
|
||||
else
|
||||
raise "Error loading CPN image at #{cpn_uri}. Response: #{res.inspect}"
|
||||
end
|
||||
end
|
||||
new_url = res['location']
|
||||
match = new_url.match(IMAGE_CP_LOCATION_REGEX)
|
||||
if match
|
||||
self.image_hash = match[1]
|
||||
else
|
||||
raise "CPN image pointed to #{new_url}, which does not match CP image format"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue