2010-10-10 19:18:42 -07:00
|
|
|
class PetsController < ApplicationController
|
2010-11-05 15:45:05 -07:00
|
|
|
rescue_from Pet::PetNotFound, :with => :pet_not_found
|
2011-02-19 19:49:13 -08:00
|
|
|
rescue_from PetType::DownloadError, SwfAsset::DownloadError, :with => :asset_download_error
|
|
|
|
rescue_from Pet::DownloadError, :with => :pet_download_error
|
2010-11-05 15:45:05 -07:00
|
|
|
|
2010-12-11 06:37:39 -08:00
|
|
|
cache_sweeper :user_sweeper
|
|
|
|
|
2010-11-05 15:45:05 -07:00
|
|
|
DESTINATIONS = {
|
|
|
|
'needed_items' => '?',
|
|
|
|
'root' => '#',
|
|
|
|
'wardrobe' => '#'
|
|
|
|
}
|
|
|
|
|
|
|
|
def load
|
2011-01-27 13:35:46 -08:00
|
|
|
if params[:name] == '!'
|
|
|
|
redirect_to roulette_path
|
2010-11-06 16:07:15 -07:00
|
|
|
else
|
2011-01-27 13:35:46 -08:00
|
|
|
raise Pet::PetNotFound unless params[:name]
|
|
|
|
@pet = Pet.load(params[:name])
|
|
|
|
if user_signed_in?
|
|
|
|
points = current_user.contribute! @pet
|
|
|
|
else
|
|
|
|
@pet.save
|
|
|
|
points = true
|
2010-11-05 17:09:03 -07:00
|
|
|
end
|
2011-01-27 13:35:46 -08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
destination = params[:destination] || params[:origin]
|
|
|
|
destination = 'root' unless DESTINATIONS[destination]
|
|
|
|
query_joiner = DESTINATIONS[destination]
|
|
|
|
path = send("#{destination}_path") + query_joiner + @pet.wardrobe_query
|
|
|
|
redirect_to path
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render :json => points
|
|
|
|
end
|
2010-11-05 17:09:03 -07:00
|
|
|
end
|
|
|
|
end
|
2010-11-05 15:45:05 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def pet_not_found
|
2011-02-19 19:09:12 -08:00
|
|
|
pet_load_error :long_message => 'Could not find any pet by that name. Did you spell it correctly?',
|
|
|
|
:short_message => 'Pet not found',
|
|
|
|
:status => :not_found
|
|
|
|
end
|
|
|
|
|
2011-02-19 19:49:13 -08:00
|
|
|
def asset_download_error(e)
|
2011-02-19 19:09:12 -08:00
|
|
|
Rails.logger.warn e.message
|
|
|
|
pet_load_error :long_message => "We found the pet all right, but the " +
|
|
|
|
"Neopets image server didn't respond to our download request. Maybe it's " +
|
|
|
|
"down, or maybe it's just having trouble. Try again later, maybe. Sorry!",
|
|
|
|
:short_message => 'Neopets seems down. Try again?',
|
|
|
|
:status => :gateway_timeout
|
|
|
|
end
|
|
|
|
|
2011-02-19 19:49:13 -08:00
|
|
|
def pet_download_error(e)
|
|
|
|
Rails.logger.warn e.message
|
|
|
|
pet_load_error :long_message => "Could not connect to the Neopets server " +
|
|
|
|
"to look up the pet. Maybe they're down. Try again later, maybe. Sorry!",
|
|
|
|
:short_message => 'Neopets seems down. Try again?',
|
|
|
|
:status => :gateway_timeout
|
|
|
|
end
|
|
|
|
|
2011-02-19 19:09:12 -08:00
|
|
|
def pet_load_error(options)
|
2010-11-05 17:09:03 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
path = params[:origin] || root_path
|
|
|
|
path += "?name=#{params[:name]}"
|
2011-02-19 19:09:12 -08:00
|
|
|
redirect_to path, :alert => options[:long_message]
|
2010-11-05 17:09:03 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
2011-02-19 19:09:12 -08:00
|
|
|
render :text => options[:short_message], :status => options[:status]
|
2010-11-05 17:09:03 -07:00
|
|
|
end
|
|
|
|
end
|
2010-10-10 11:33:54 -07:00
|
|
|
end
|
|
|
|
end
|