Skip to content

Commit

Permalink
Merge pull request #646 from pact-foundation/chore/clean-up-decorator…
Browse files Browse the repository at this point in the history
…-context-2

refactor: clean up decorator context creation
  • Loading branch information
bethesque authored Nov 26, 2023
2 parents ce7dc70 + 0c32c02 commit add6b29
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 35 deletions.
1 change: 0 additions & 1 deletion lib/pact_broker/api/decorators/base_decorator.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
23 changes: 0 additions & 23 deletions lib/pact_broker/api/decorators/decorator_context.rb

This file was deleted.

49 changes: 47 additions & 2 deletions lib/pact_broker/api/decorators/decorator_context_creator.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
10 changes: 7 additions & 3 deletions spec/lib/pact_broker/api/decorators/webhooks_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/resources/all_webhooks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/resources/integrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/resources/pacticipants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit add6b29

Please sign in to comment.