support pet names with illegal characters by not including them in pet type image hash updates

This commit is contained in:
Emi Matchu 2011-02-19 23:09:59 -05:00
parent 2a7fea11e6
commit a8ab993a31

View file

@ -1,6 +1,7 @@
class PetType < ActiveRecord::Base class PetType < ActiveRecord::Base
IMAGE_CPN_FORMAT = 'http://pets.neopets.com/cpn/%s/1/1.png'; IMAGE_CPN_FORMAT = 'http://pets.neopets.com/cpn/%s/1/1.png';
IMAGE_CP_LOCATION_REGEX = %r{^/cp/(.+?)/1/1\.png$}; IMAGE_CP_LOCATION_REGEX = %r{^/cp/(.+?)/1/1\.png$};
IMAGE_CPN_ACCEPTABLE_NAME = /^[a-z0-9_]+$/
has_one :contribution, :as => :contributed has_one :contribution, :as => :contributed
has_many :pet_states has_many :pet_states
@ -107,8 +108,8 @@ class PetType < ActiveRecord::Base
end end
before_save do before_save do
if @origin_pet if @origin_pet && @origin_pet.name =~ IMAGE_CPN_ACCEPTABLE_NAME
cpn_uri = URI.parse sprintf(IMAGE_CPN_FORMAT, @origin_pet.name); cpn_uri = URI.parse sprintf(IMAGE_CPN_FORMAT, CGI.escape(@origin_pet.name));
begin begin
res = Net::HTTP.get_response(cpn_uri) res = Net::HTTP.get_response(cpn_uri)
rescue Exception => e rescue Exception => e
@ -127,6 +128,7 @@ class PetType < ActiveRecord::Base
match = new_url.match(IMAGE_CP_LOCATION_REGEX) match = new_url.match(IMAGE_CP_LOCATION_REGEX)
if match if match
self.image_hash = match[1] self.image_hash = match[1]
Rails.logger.info "Successfully loaded #{cpn_uri}, saved image hash #{match[1]}"
else else
raise DownloadError, "CPN image pointed to #{new_url}, which does not match CP image format" raise DownloadError, "CPN image pointed to #{new_url}, which does not match CP image format"
end end