diff --git a/lib/pact_broker/api/resources/badge.rb b/lib/pact_broker/api/resources/badge.rb index bdd299e08..1f7a77a04 100644 --- a/lib/pact_broker/api/resources/badge.rb +++ b/lib/pact_broker/api/resources/badge.rb @@ -31,7 +31,7 @@ def forbidden? private def to_svg - badges_service.pact_verification_badge pact, label, initials, verification_status + badge_service.pact_verification_badge pact, label, initials, verification_status end def pact diff --git a/lib/pact_broker/badges/cached_service.rb b/lib/pact_broker/badges/cached_service.rb new file mode 100644 index 000000000..71a62426d --- /dev/null +++ b/lib/pact_broker/badges/cached_service.rb @@ -0,0 +1,27 @@ +require 'pact_broker/logging' +require 'pact_broker/badges/service' + +module PactBroker + module Badges + module CachedService + + extend self + include PactBroker::Logging + extend PactBroker::Services + + CACHE = {} + private_constant :CACHE + + def pact_verification_badge pact, label, initials, verification_status + badge_key = key(pact, label, initials, verification_status) + CACHE[badge_key] ||= PactBroker::Badges::Service.pact_verification_badge(pact, label, initials, verification_status) + end + + private + + def key pact, label, initials, verification_status + "#{pact.consumer.name}-#{pact.provider.name}-#{label}-#{initials}-#{verification_status}" + end + end + end +end diff --git a/lib/pact_broker/services.rb b/lib/pact_broker/services.rb index 88c2741f4..d20727300 100644 --- a/lib/pact_broker/services.rb +++ b/lib/pact_broker/services.rb @@ -42,9 +42,9 @@ def verification_service Verifications::Service end - def badges_service - require 'pact_broker/badges/service' - Badges::Service + def badge_service + require 'pact_broker/badges/cached_service' + Badges::CachedService end end end diff --git a/spec/lib/pact_broker/badges/cached_service_spec.rb b/spec/lib/pact_broker/badges/cached_service_spec.rb new file mode 100644 index 000000000..bce8637fd --- /dev/null +++ b/spec/lib/pact_broker/badges/cached_service_spec.rb @@ -0,0 +1,49 @@ +require 'pact_broker/badges/cached_service' +require 'pact_broker/badges/service' + +module PactBroker + module Badges + describe CachedService do + + let(:consumer) { double('consumer', name: 'foo') } + let(:provider) { double('provider', name: 'bar') } + let(:pact) { double('pact', consumer: consumer, provider: provider) } + let(:label) { 'consumer' } + let(:initials) { false } + let(:verification_status) { 'status' } + + describe "#pact_verification_badge" do + + before do + allow(Service).to receive(:pact_verification_badge).and_return('badge') + stub_const('PactBroker::Badges::CachedService::CACHE', {}) + end + + subject { CachedService.pact_verification_badge pact, label, initials, verification_status } + + it "returns the badge" do + expect(subject).to eq 'badge' + end + + context "when the badge is not in the cache" do + before do + stub_const('PactBroker::Badges::CachedService::CACHE', {}) + end + + it "retrieves the badge from the Badges::Service" do + expect(Service).to receive(:pact_verification_badge) + subject + end + end + + context "when the badge is in the cache" do + it "returns the cached badge" do + expect(Service).to receive(:pact_verification_badge).once + CachedService.pact_verification_badge pact, label, initials, verification_status + CachedService.pact_verification_badge pact, label, initials, verification_status + end + end + end + end + end +end