From 0c32c027713596843e3e83a382ca8591ebccd927 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 25 Nov 2023 08:42:14 +1100 Subject: [PATCH] refactor: clean up decorator context creation --- .../api/decorators/base_decorator.rb | 1 - .../api/decorators/decorator_context.rb | 23 --------- .../decorators/decorator_context_creator.rb | 49 ++++++++++++++++++- .../decorators/pact_version_decorator_spec.rb | 4 +- .../api/decorators/webhooks_decorator_spec.rb | 10 ++-- .../api/resources/all_webhooks_spec.rb | 2 +- .../api/resources/integrations_spec.rb | 2 +- .../resources/pacticipant_webhooks_spec.rb | 2 +- .../api/resources/pacticipants_spec.rb | 2 +- 9 files changed, 60 insertions(+), 35 deletions(-) delete mode 100644 lib/pact_broker/api/decorators/decorator_context.rb diff --git a/lib/pact_broker/api/decorators/base_decorator.rb b/lib/pact_broker/api/decorators/base_decorator.rb index ec5a801c4..96bc5892c 100644 --- a/lib/pact_broker/api/decorators/base_decorator.rb +++ b/lib/pact_broker/api/decorators/base_decorator.rb @@ -1,7 +1,6 @@ require "roar/decorator" require "roar/json/hal" require "pact_broker/api/pact_broker_urls" -require "pact_broker/api/decorators/decorator_context" require "pact_broker/api/decorators/format_date_time" require "pact_broker/string_refinements" require "pact_broker/hash_refinements" diff --git a/lib/pact_broker/api/decorators/decorator_context.rb b/lib/pact_broker/api/decorators/decorator_context.rb deleted file mode 100644 index 2bf4eded7..000000000 --- a/lib/pact_broker/api/decorators/decorator_context.rb +++ /dev/null @@ -1,23 +0,0 @@ -module PactBroker - module Api - module Decorators - class DecoratorContext < Hash - attr_reader :base_url, :resource_url, :resource_title, :env, :query_string, :request_url - - def initialize base_url, resource_url, env, options = {} - @base_url = self[:base_url] = base_url - @resource_url = self[:resource_url] = resource_url - @resource_title = self[:resource_title] = options[:resource_title] - @env = self[:env] = env - @query_string = self[:query_string] = (env["QUERY_STRING"] && !env["QUERY_STRING"].empty? ? env["QUERY_STRING"] : nil) - @request_url = self[:request_url] = query_string ? resource_url + "?" + query_string : resource_url - merge!(options) - end - - def to_s - "DecoratorContext #{super}" - end - end - end - end -end diff --git a/lib/pact_broker/api/decorators/decorator_context_creator.rb b/lib/pact_broker/api/decorators/decorator_context_creator.rb index 6d9441a3c..c712941d3 100644 --- a/lib/pact_broker/api/decorators/decorator_context_creator.rb +++ b/lib/pact_broker/api/decorators/decorator_context_creator.rb @@ -1,11 +1,56 @@ -require "pact_broker/api/decorators/decorator_context" +# Builds the Hash that is passed into the Decorator as the `user_options`. It contains the request details, rack env, the (optional) title +# and anything else that is required by the decorator to render the resource (eg. the pacticipant that the versions belong to) module PactBroker module Api module Decorators class DecoratorContextCreator + + # @param [PactBroker::BaseResource] the Pact Broker webmachine resource + # @param [Hash] options any extra options that need to be passed through to the decorator. + # @return [Hash] decorator_context + + # decorator_context [String] :base_url + # The location where the Pact Broker is hosted. + # eg. http://some.host:9292/pact_broker + # Always present + + # decorator_context [String] :resource_url + # The resource URL without any query string. + # eg. http://some.host:9292/pact_broker/pacticipants/Foo/versions + # Always present + + # decorator_context [String] :query_string + # The query string. + # "page=1&size=50" + # May be empty + + # decorator_context [String] :request_url + # The full request URL. + # eg. http://some.host:9292/pact_broker/pacticipants/Foo/versions?page=1&size=50 + # Always present + + # decorator_context [Hash] :env + # The rack env. + # Always present + + # decorator_context [Hash] :resource_title + # eg. "Pacticipant versions for Foo" + # Optional + # Used when a single decorator is being used for multiple resources and the title needs to be + # set from the resource. + def self.call(resource, options) - Decorators::DecoratorContext.new(resource.base_url, resource.resource_url, resource.request.env, options) + env = resource.request.env + decorator_context = {} + decorator_context[:base_url] = resource.base_url + decorator_context[:resource_url] = resource.resource_url + decorator_context[:query_string] = query_string = (env["QUERY_STRING"] && !env["QUERY_STRING"].empty? ? env["QUERY_STRING"] : nil) + decorator_context[:request_url] = query_string ? resource.resource_url + "?" + query_string : resource.resource_url + decorator_context[:env] = env + decorator_context[:resource_title] = options[:resource_title] + decorator_context.merge!(options) + decorator_context end end end diff --git a/spec/lib/pact_broker/api/decorators/pact_version_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/pact_version_decorator_spec.rb index 280c040ec..f7ad6c7d6 100644 --- a/spec/lib/pact_broker/api/decorators/pact_version_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/pact_version_decorator_spec.rb @@ -33,9 +33,9 @@ module Decorators build_url: "http://build" ) end - let(:decorator_context) { DecoratorContext.new(base_url, "", {}) } + let(:user_options) { { base_url: base_url } } - let(:json) { PactVersionDecorator.new(pact).to_json(user_options: decorator_context) } + let(:json) { PactVersionDecorator.new(pact).to_json(user_options: user_options) } subject { JSON.parse(json, symbolize_names: true) } diff --git a/spec/lib/pact_broker/api/decorators/webhooks_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/webhooks_decorator_spec.rb index 320fb4ea7..082abeedb 100644 --- a/spec/lib/pact_broker/api/decorators/webhooks_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/webhooks_decorator_spec.rb @@ -15,15 +15,19 @@ module Decorators let(:base_url) { "http://example.org" } let(:resource_url) { "http://example.org/webhooks" } - let(:decorator_context) do - DecoratorContext.new(base_url, resource_url, {}, resource_title: "Title") + let(:user_options) do + { + base_url: base_url, + resource_url: resource_url, + resource_title: "Title" + } end let(:webhooks) { [webhook] } describe "to_json" do - let(:json) { WebhooksDecorator.new(webhooks).to_json(user_options: decorator_context) } + let(:json) { WebhooksDecorator.new(webhooks).to_json(user_options: user_options) } subject { JSON.parse(json, symbolize_names: true) } diff --git a/spec/lib/pact_broker/api/resources/all_webhooks_spec.rb b/spec/lib/pact_broker/api/resources/all_webhooks_spec.rb index ab048a040..e657c2f5c 100644 --- a/spec/lib/pact_broker/api/resources/all_webhooks_spec.rb +++ b/spec/lib/pact_broker/api/resources/all_webhooks_spec.rb @@ -129,7 +129,7 @@ module Resources it "generates a JSON representation of the webhook" do expect(Decorators::WebhooksDecorator).to receive(:new).with(webhooks) - expect(decorator).to receive(:to_json).with(user_options: instance_of(Decorators::DecoratorContext)) + expect(decorator).to receive(:to_json).with(user_options: instance_of(Hash)) subject end diff --git a/spec/lib/pact_broker/api/resources/integrations_spec.rb b/spec/lib/pact_broker/api/resources/integrations_spec.rb index 30bf76e20..ff0246510 100644 --- a/spec/lib/pact_broker/api/resources/integrations_spec.rb +++ b/spec/lib/pact_broker/api/resources/integrations_spec.rb @@ -41,7 +41,7 @@ module Resources it "renders the integrations" do expect(decorator_class).to receive(:new).with(integrations) - expect(decorator).to receive(:to_json).with(user_options: instance_of(Decorators::DecoratorContext)) + expect(decorator).to receive(:to_json).with(user_options: instance_of(Hash)) expect(subject.body).to eq json end diff --git a/spec/lib/pact_broker/api/resources/pacticipant_webhooks_spec.rb b/spec/lib/pact_broker/api/resources/pacticipant_webhooks_spec.rb index a997c15e4..a58300ac8 100644 --- a/spec/lib/pact_broker/api/resources/pacticipant_webhooks_spec.rb +++ b/spec/lib/pact_broker/api/resources/pacticipant_webhooks_spec.rb @@ -40,7 +40,7 @@ module Resources it "generates a JSON body" do expect(Decorators::WebhooksDecorator).to receive(:new).with(webhooks) - expect(decorator).to receive(:to_json).with(user_options: instance_of(Decorators::DecoratorContext)) + expect(decorator).to receive(:to_json).with(user_options: instance_of(Hash)) subject end diff --git a/spec/lib/pact_broker/api/resources/pacticipants_spec.rb b/spec/lib/pact_broker/api/resources/pacticipants_spec.rb index 14a30a6a6..034306b11 100644 --- a/spec/lib/pact_broker/api/resources/pacticipants_spec.rb +++ b/spec/lib/pact_broker/api/resources/pacticipants_spec.rb @@ -123,7 +123,7 @@ module Resources it "creates a JSON representation of the new pacticipant" do expect(decorator_class).to receive(:new).with(created_model) - expect(decorator).to receive(:to_json).with(user_options: instance_of(Decorators::DecoratorContext)) + expect(decorator).to receive(:to_json).with(user_options: instance_of(Hash)) subject end