diff --git a/app/controllers/pets_controller.rb b/app/controllers/pets_controller.rb index 8dbaf3e4..97b25329 100644 --- a/app/controllers/pets_controller.rb +++ b/app/controllers/pets_controller.rb @@ -1,12 +1,10 @@ class PetsController < ApplicationController rescue_from Neopets::CustomPets::PetNotFound, with: :pet_not_found rescue_from Neopets::CustomPets::DownloadError, with: :pet_download_error + rescue_from Pet::ModelingDisabled, with: :modeling_disabled rescue_from Pet::UnexpectedDataFormat, with: :unexpected_data_format def load - # Uncomment this to temporarily disable modeling for most users. - # return modeling_disabled unless user_signed_in? && current_user.admin? - raise Neopets::CustomPets::PetNotFound unless params[:name] @pet = Pet.load(params[:name]) points = contribute(current_user, @pet) diff --git a/app/models/pet.rb b/app/models/pet.rb index 50cd4992..40eeb57c 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -4,6 +4,8 @@ class Pet < ApplicationRecord attr_reader :items, :pet_state, :alt_style def load!(timeout: nil) + raise ModelingDisabled unless Rails.configuration.modeling_enabled + viewer_data_hash = Neopets::CustomPets.fetch_viewer_data(name, timeout:) use_modeling_snapshot(ModelingSnapshot.new(viewer_data_hash)) end @@ -50,5 +52,5 @@ class Pet < ApplicationRecord end class UnexpectedDataFormat < RuntimeError;end + class ModelingDisabled < RuntimeError;end end - diff --git a/config/environments/development.rb b/config/environments/development.rb index 41211e87..8f374d14 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -103,6 +103,10 @@ Rails.application.configure do # Allow connections on Vagrant's private network. config.web_console.permissions = '10.0.2.2' + # Allow pets to model new data. (If modeling is ever broken, disable this in + # production while we fix it!) + config.modeling_enabled = true + # Use a local copy of Impress 2020, presumably running on port 4000. (Can # override this with the IMPRESS_2020_ORIGIN environment variable!) config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN", diff --git a/config/environments/production.rb b/config/environments/production.rb index 370e0246..58eae953 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -122,6 +122,10 @@ Rails.application.configure do # Skip DNS rebinding protection for the default health check endpoint. # config.host_authorization = { exclude: ->(request) { request.path == "/up" } } + # Allow pets to model new data. (If modeling is ever broken, disable this + # here while we fix it!) + config.modeling_enabled = false + # Use the live copy of Impress 2020. (Can override this with the # IMPRESS_2020_ORIGIN environment variable!) config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN", diff --git a/config/environments/test.rb b/config/environments/test.rb index f677ede2..e57cbb53 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -62,6 +62,10 @@ Rails.application.configure do # Raise error when a before_action's only/except options reference missing actions config.action_controller.raise_on_missing_callback_actions = true + # Allow pets to model new data. (If modeling is ever broken, disable this in + # production while we fix it!) + config.modeling_enabled = true + # Use a local copy of Impress 2020, presumably running on port 4000. (Can # override this with the IMPRESS_2020_ORIGIN environment variable!) config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN", diff --git a/spec/models/pet_spec.rb b/spec/models/pet_spec.rb index 55441286..22a6be0c 100644 --- a/spec/models/pet_spec.rb +++ b/spec/models/pet_spec.rb @@ -448,5 +448,13 @@ RSpec.describe Pet, type: :model do end end end + + context "when modeling is disabled" do + before { allow(Rails.configuration).to receive(:modeling_enabled) { false } } + + it("raises an error") do + expect { Pet.load("matts_bat") }.to raise_error(Pet::ModelingDisabled) + end + end end end