Skip to content

Commit

Permalink
Merge pull request #21 from alphagov/split-repo-2
Browse files Browse the repository at this point in the history
Decouple repositories from `PublishingEventPipeline`
  • Loading branch information
csutter authored Sep 26, 2023
2 parents 2d655ce + fd5b27b commit b04f61d
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 33 deletions.
1 change: 0 additions & 1 deletion lib/publishing_event_pipeline.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "publishing_event_pipeline/configuration"
require "publishing_event_pipeline/document"
require "publishing_event_pipeline/document_lifecycle_event"

require "publishing_event_pipeline/message_processor"
Expand Down
17 changes: 0 additions & 17 deletions lib/publishing_event_pipeline/document.rb

This file was deleted.

12 changes: 9 additions & 3 deletions lib/publishing_event_pipeline/document_lifecycle_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ def initialize(message_hash)
@document_type = message_hash.fetch("document_type")
@payload_version = message_hash.fetch("payload_version")

@document = Document.from_message_hash(message_hash) unless delete?
unless delete?
# TODO: Flesh these out as we need them
@metadata = {
base_path: message_hash.fetch("base_path"),
}
@content = nil
end
end

# Persists the document to, or removes it from, a repository for a search product.
def synchronize_to(repository)
if delete?
repository.delete(content_id, payload_version:)
else
repository.put(content_id, document, payload_version:)
repository.put(content_id, metadata, content:, payload_version:)
end
end

private

attr_reader :content_id, :document_type, :payload_version, :document
attr_reader :content_id, :document_type, :payload_version, :metadata, :content

def delete?
UNPUBLISH_DOCUMENT_TYPES.include?(document_type)
Expand Down
8 changes: 5 additions & 3 deletions lib/search_repositories/null/null_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module SearchRepositories
module Null
# A repository that does nothing, for use until we can integrate with the real product.
class NullRepository
def put(content_id, document, payload_version: nil)
def put(content_id, metadata, content: nil, payload_version: nil)
content_snippet = content ? content[0..50] : "<no content>"

Rails.logger.info(
sprintf(
"[%s] Persisted %s: %s (@v%s)",
self.class.name, content_id, document.metadata[:base_path], payload_version
"[%s] Persisted %s: %s (@v%s): '%s...'",
self.class.name, content_id, metadata[:base_path], payload_version, content_snippet
),
)
end
Expand Down
8 changes: 5 additions & 3 deletions spec/integration/publishing_event_pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

it "is added to the repository" do
result = repository.get("f75d26a3-25a4-4c31-beea-a77cada4ce12")
# TODO: Flesh out the document model and test that it is as expected
expect(result).to be_present
# TODO: Flesh out the document model and test that everything is as expected
expect(result[:metadata]).to eq(
base_path: "/government/news/ebola-medal-for-over-3000-heroes",
)
expect(result[:content]).to be_nil
end
end

Expand All @@ -29,7 +32,6 @@

it "is removed from the repository" do
result = repository.get("966bae6d-223e-4102-a6e5-e874012390e5")
# TODO: Flesh out the document model and test that it is as expected
expect(result).to be_nil
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@

expect(repository).to have_received(:put).with(
"f75d26a3-25a4-4c31-beea-a77cada4ce12",
an_instance_of(PublishingEventPipeline::Document),
{ base_path: "/government/news/ebola-medal-for-over-3000-heroes" },
content: nil,
payload_version: 65_861_808,
)
end
Expand Down
9 changes: 6 additions & 3 deletions spec/lib/search_repositories/null/null_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
let(:repository) { described_class.new }
let(:content_id) { "some_content_id" }
let(:metadata) { { base_path: "/some/path" } }
let(:document) { instance_double(PublishingEventPipeline::Document, metadata:) }
let(:content) { "Lorem ipsum dolor sit amet, consecutur edipiscing elit" }
let(:payload_version) { "1" }

describe "#put" do
it "logs the put operation" do
expect(Rails.logger).to receive(:info).with(
a_string_ending_with("Persisted some_content_id: /some/path (@v1)"),
a_string_ending_with(
"Persisted some_content_id: /some/path (@v1): " \
"'Lorem ipsum dolor sit amet, consecutur edipiscing e...'",
),
)

repository.put(content_id, document, payload_version:)
repository.put(content_id, metadata, content:, payload_version:)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def get(content_id)
end

# rubocop:disable Lint/UnusedMethodArgument
def put(content_id, document, payload_version: nil)
documents[content_id] = document
def put(content_id, metadata, content: nil, payload_version: nil)
documents[content_id] = { metadata:, content: }
end

def delete(content_id, payload_version: nil)
Expand Down

0 comments on commit b04f61d

Please sign in to comment.