diff --git a/app/views/govuk_web_banners/_emergency_banner.html.erb b/app/views/govuk_web_banners/_emergency_banner.html.erb index fae038d..83734c9 100644 --- a/app/views/govuk_web_banners/_emergency_banner.html.erb +++ b/app/views/govuk_web_banners/_emergency_banner.html.erb @@ -1,4 +1,4 @@ -<% emergency_banner = GovukWebBanners::EmergencyBanner.new(redis_client: Rails.application.config.emergency_banner_redis_client) %> +<% emergency_banner = GovukWebBanners::EmergencyBanner.new %> <% if emergency_banner.active? %> <%= render "govuk_publishing_components/components/emergency_banner", { campaign_class: emergency_banner.campaign_class, diff --git a/lib/govuk_web_banners/emergency_banner.rb b/lib/govuk_web_banners/emergency_banner.rb index 0ba0e2f..41a9ceb 100644 --- a/lib/govuk_web_banners/emergency_banner.rb +++ b/lib/govuk_web_banners/emergency_banner.rb @@ -5,8 +5,8 @@ module GovukWebBanners class EmergencyBanner attr_reader :campaign_class, :heading, :short_description, :link, :link_text - def initialize(redis_client:) - content = content_from_redis(redis_client) + def initialize + content = content_from_redis @campaign_class = content[:campaign_class].presence @heading = content[:heading].presence @@ -21,9 +21,13 @@ def active? private - def content_from_redis(client) + def redis_client + Rails.application.config.emergency_banner_redis_client + end + + def content_from_redis Rails.cache.fetch("#emergency_banner/config", expires_in: 1.minute) do - client.hgetall("emergency_banner").try(:symbolize_keys) + redis_client.hgetall("emergency_banner").try(:symbolize_keys) end rescue StandardError => e GovukError.notify(e, extra: { context: "Emergency Banner Redis" }) diff --git a/spec/units/emergency_banner_spec.rb b/spec/units/emergency_banner_spec.rb index 031ef2e..515bdce 100644 --- a/spec/units/emergency_banner_spec.rb +++ b/spec/units/emergency_banner_spec.rb @@ -1,6 +1,9 @@ RSpec.describe GovukWebBanners::EmergencyBanner do - let(:redis_client) { double(hgetall: {}) } - subject(:emergency_banner) { GovukWebBanners::EmergencyBanner.new(redis_client:) } + subject(:emergency_banner) { GovukWebBanners::EmergencyBanner.new } + + before do + Rails.application.config.emergency_banner_redis_client = double(hgetall: {}) + end describe "caching" do context "with a Rails cache" do @@ -8,15 +11,15 @@ allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache.lookup_store(:memory_store)) Rails.cache.clear - GovukWebBanners::EmergencyBanner.new(redis_client:) - GovukWebBanners::EmergencyBanner.new(redis_client:) + GovukWebBanners::EmergencyBanner.new + GovukWebBanners::EmergencyBanner.new - expect(redis_client).to have_received(:hgetall).once + expect(Rails.application.config.emergency_banner_redis_client).to have_received(:hgetall).once travel_to(Time.now + 61.seconds) - GovukWebBanners::EmergencyBanner.new(redis_client:) - expect(redis_client).to have_received(:hgetall).twice + GovukWebBanners::EmergencyBanner.new + expect(Rails.application.config.emergency_banner_redis_client).to have_received(:hgetall).twice travel_back end @@ -31,7 +34,9 @@ end context "with the emergency banner active" do - let(:redis_client) { double(hgetall: { heading: "Emergency!", campaign_class: "notable-death" }) } + before do + Rails.application.config.emergency_banner_redis_client = double(hgetall: { heading: "Emergency!", campaign_class: "notable-death" }) + end it "returns true" do expect(emergency_banner.active?).to be true @@ -40,7 +45,9 @@ context "if the call to Redis fails" do before do + redis_client = double allow(redis_client).to receive(:hgetall).with("emergency_banner").and_raise(StandardError) + Rails.application.config.emergency_banner_redis_client = redis_client end it "returns false" do