From a8ab993a3153725eaa67c3fc6d239ee3748babf4 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 19 Feb 2011 23:09:59 -0500 Subject: [PATCH] support pet names with illegal characters by not including them in pet type image hash updates --- app/models/pet_type.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index f87f07bd..bd46b372 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -1,6 +1,7 @@ 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$}; + IMAGE_CPN_ACCEPTABLE_NAME = /^[a-z0-9_]+$/ has_one :contribution, :as => :contributed has_many :pet_states @@ -107,8 +108,8 @@ class PetType < ActiveRecord::Base end before_save do - if @origin_pet - cpn_uri = URI.parse sprintf(IMAGE_CPN_FORMAT, @origin_pet.name); + if @origin_pet && @origin_pet.name =~ IMAGE_CPN_ACCEPTABLE_NAME + cpn_uri = URI.parse sprintf(IMAGE_CPN_FORMAT, CGI.escape(@origin_pet.name)); begin res = Net::HTTP.get_response(cpn_uri) rescue Exception => e @@ -127,6 +128,7 @@ class PetType < ActiveRecord::Base match = new_url.match(IMAGE_CP_LOCATION_REGEX) if match self.image_hash = match[1] + Rails.logger.info "Successfully loaded #{cpn_uri}, saved image hash #{match[1]}" else raise DownloadError, "CPN image pointed to #{new_url}, which does not match CP image format" end