Skip to content

Commit

Permalink
Merge pull request #875 from alphagov/introduce-services-module
Browse files Browse the repository at this point in the history
Introduce Services module
  • Loading branch information
chrisroos committed Mar 16, 2017
2 parents 20d0662 + b999ce9 commit 0c345e5
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 66 deletions.
4 changes: 3 additions & 1 deletion app/lib/organisation_fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require "services"

class OrganisationFetcher
def self.instance
@organisations ||= {}
->(organisation_slug) {
@organisations[organisation_slug] ||= OrganisationsApi.instance.organisation(organisation_slug)
@organisations[organisation_slug] ||= Services.organisations.organisation(organisation_slug)
}
end
end
7 changes: 0 additions & 7 deletions app/lib/organisations_api.rb

This file was deleted.

11 changes: 0 additions & 11 deletions app/lib/publishing_api.rb

This file was deleted.

10 changes: 0 additions & 10 deletions app/lib/publishing_api_v2.rb

This file was deleted.

7 changes: 0 additions & 7 deletions app/lib/rummager_api.rb

This file was deleted.

12 changes: 7 additions & 5 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "services"

class Attachment
include Mongoid::Document
include Mongoid::Timestamps
Expand All @@ -20,10 +22,10 @@ def snippet
end

def file
raise ApiClientNotPresent unless AttachmentApi.client
raise ApiClientNotPresent unless Services.attachment_api
unless file_id.nil?
@attachments ||= {}
@attachments[field] ||= AttachmentApi.client.asset(file_id)
@attachments[field] ||= Services.attachment_api.asset(file_id)
end
end

Expand All @@ -37,13 +39,13 @@ def file_has_changed?
end

def upload_file
raise ApiClientNotPresent unless AttachmentApi.client
raise ApiClientNotPresent unless Services.attachment_api
begin
if file_id.nil?
response = AttachmentApi.client.create_asset(file: @uploaded_file)
response = Services.attachment_api.create_asset(file: @uploaded_file)
self.file_id = response["id"].split("/").last
else
response = AttachmentApi.client.update_asset(file_id, file: @uploaded_file)
response = Services.attachment_api.update_asset(file_id, file: @uploaded_file)
end
self.file_url = response["file_url"]
rescue GdsApi::HTTPNotFound => e
Expand Down
3 changes: 2 additions & 1 deletion app/observers/manual_observers_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "rummager_indexer"
require "formatters/manual_indexable_formatter"
require "formatters/section_indexable_formatter"
require "services"

class ManualObserversRegistry
def publication
Expand Down Expand Up @@ -207,7 +208,7 @@ def publishing_api_withdrawer
end

def publishing_api_v2
PublishingApiV2.instance
Services.publishing_api_v2
end

def organisation(slug)
Expand Down
5 changes: 3 additions & 2 deletions app/services/abstract_manual_document_service_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "list_manual_documents_service"
require "reorder_manual_documents_service"
require "remove_manual_document_service"
require "services"

class AbstractManualDocumentServiceRegistry
def preview(context)
Expand Down Expand Up @@ -144,10 +145,10 @@ def publishing_api_draft_manual_document_discarder
end

def publishing_api_v2
PublishingApiV2.instance
Services.publishing_api_v2
end

def organisations_api
GdsApi::Organisations.new(ORGANISATIONS_API_BASE_PATH)
Services.organisations
end
end
3 changes: 2 additions & 1 deletion bin/delete_documents_marked_for_delete
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env ruby

require File.expand_path("../../config/environment", __FILE__)
require "services"

def publishing_api
PublishingApiV2.instance
Services.publishing_api_v2
end

def in_publishing_api?(content_id)
Expand Down
11 changes: 0 additions & 11 deletions config/initializers/attachment_api.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/cli_manual_deleter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "services"

class CliManualDeleter
def initialize(manual_slug, manual_id: nil, stdin: STDIN, stdout: STDOUT)
@manual_slug = manual_slug
Expand Down Expand Up @@ -101,6 +103,6 @@ def discard_draft_from_publishing_api(content_id)
end

def publishing_api_v2
PublishingApiV2.instance
Services.publishing_api_v2
end
end
4 changes: 3 additions & 1 deletion lib/duplicate_draft_deleter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "services"

class DuplicateDraftDeleter
def call
duplicated_editions_not_in_publishing_api = duplicated_editions.reject { |data| in_publishing_api?(data[:content_id]) }
Expand All @@ -14,7 +16,7 @@ def call
private

def publishing_api
PublishingApiV2.instance
Services.publishing_api_v2
end

def in_publishing_api?(content_id)
Expand Down
4 changes: 3 additions & 1 deletion lib/manual_relocator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "services"

class ManualRelocator
attr_reader :from_slug, :to_slug

Expand Down Expand Up @@ -227,7 +229,7 @@ def send_gone(document_id, slug)
end

def publishing_api
PublishingApiV2.instance
Services.publishing_api_v2
end

def fetch_organisation(slug)
Expand Down
4 changes: 3 additions & 1 deletion lib/rummager_indexer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "services"

class RummagerIndexer
def add(document)
api.add_document(document.type, document.id, document.indexable_attributes)
Expand All @@ -10,6 +12,6 @@ def delete(document)
private

def api
RummagerApi.instance
Services.rummager
end
end
7 changes: 4 additions & 3 deletions lib/section_reslugger.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "gds_api/content_store"
require "manual_service_registry"
require "services"

class SectionReslugger
RUMMAGER_FORMAT = "manual_section".freeze
Expand Down Expand Up @@ -63,7 +64,7 @@ def validate_new_section_in_content_store

def redirect_section
PublishingAPIRedirecter.new(
publishing_api: PublishingApi.instance,
publishing_api: Services.publishing_api,
entity: current_section_edition,
redirect_to_location: "/#{full_new_section_slug}"
).call
Expand Down Expand Up @@ -132,7 +133,7 @@ def section_in_content_store(slug)
end

def content_store
GdsApi::ContentStore.new(Plek.current.find("content-store"))
Services.content_store
end

def full_current_section_slug
Expand All @@ -148,7 +149,7 @@ def full_section_slug(slug)
end

def remove_old_section_from_rummager
rummager = RummagerApi.instance
rummager = Services.rummager
rummager.delete_document(RUMMAGER_FORMAT, "/#{full_current_section_slug}")
end
end
41 changes: 41 additions & 0 deletions lib/services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "gds_api/asset_manager"
require "gds_api/organisations"
require "gds_api/publishing_api"
require "gds_api/publishing_api_v2"
require "gds_api/rummager"

module Services
def self.attachment_api
@attachment_api ||= GdsApi::AssetManager.new(
Plek.find("asset-manager"),
bearer_token: ENV["ASSET_MANAGER_BEARER_TOKEN"] || '12345678',
)
end

def self.content_store
@content_store ||= GdsApi::ContentStore.new(Plek.find("content-store"))
end

def self.organisations
@organisations ||= GdsApi::Organisations.new(ORGANISATIONS_API_BASE_PATH)
end

def self.publishing_api
@publishing_api ||= GdsApi::PublishingApi.new(
Plek.find("publishing-api"),
bearer_token: ENV["PUBLISHING_API_BEARER_TOKEN"] || "example",
timeout: 30
)
end

def self.publishing_api_v2
@publishing_api_v2 ||= GdsApi::PublishingApiV2.new(
Plek.find("publishing-api"),
bearer_token: ENV["PUBLISHING_API_BEARER_TOKEN"] || "example"
)
end

def self.rummager
@rummager_api ||= GdsApi::Rummager.new(Plek.find("search"))
end
end
6 changes: 3 additions & 3 deletions spec/models/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
it "raises an informative exception if the asset manager service can't be found" do
client = double('client')
allow(client).to receive(:create_asset).and_raise(GdsApi::HTTPNotFound.new(404))
allow(AttachmentApi).to receive(:client).and_return(client)
allow(Services).to receive(:attachment_api).and_return(client)
attachment = Attachment.new
expect { attachment.upload_file }.to raise_error(/Error uploading file. Is the Asset Manager service available\?/)
end
Expand All @@ -38,7 +38,7 @@
end

it "uploads a file before saving" do
expect(AttachmentApi.client).to receive(:create_asset)
expect(Services.attachment_api).to receive(:create_asset)
.with(file: upload_file)
.and_return("file_url" => "some/file/url", "id" => "some_file_id")

Expand All @@ -57,7 +57,7 @@
end

it "updates the uploaded file on the Attachment" do
expect(AttachmentApi.client).to receive(:update_asset)
expect(Services.attachment_api).to receive(:update_asset)
.with("some_file_id", file: upload_file)
.and_return("file_url" => "some/file/url", "id" => "some_file_id")

Expand Down

0 comments on commit 0c345e5

Please sign in to comment.