Skip to content

Commit

Permalink
Remove usages and functionality to load finder by filename
Browse files Browse the repository at this point in the history
To make functionality consistent across publishing and unpublishing, and using slug to load the schema for ease of running support tasks, there is no longer a need to load a finder schema by filename.
  • Loading branch information
minhngocd committed Jun 6, 2023
1 parent b9f84c9 commit 21b57a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 49 deletions.
13 changes: 0 additions & 13 deletions app/lib/finder_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ def finders
end
end

def finder(name)
json_schema = "lib/documents/schemas/#{name}.json"

if File.exist?(json_schema)
[{
file: MultiJson.load(File.read(json_schema)),
timestamp: File.mtime(json_schema),
}]
else
raise "Could not find file: #{json_schema}"
end
end

def finder_by_slug(slug)
schema_file = files.find do |file|
File.foreach(file).grep(/"base_path": "\/#{slug}"/).any?
Expand Down
11 changes: 4 additions & 7 deletions lib/tasks/publishing_api.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,27 @@ namespace :publishing_api do
require "finder_loader"
require "publishing_api_finder_publisher"

finder_loader = FinderLoader.new

begin
PublishingApiFinderPublisher.new(finder_loader.finders).call
PublishingApiFinderPublisher.new(FinderLoader.new.finders).call
rescue GdsApi::HTTPServerError => e
puts "Error publishing finder: #{e.inspect}"
end
end

desc "Publish a single Finder to the Publishing API"
task :publish_finder, [:name] => :environment do |_, args|
task :publish_finder, [:slug] => :environment do |_, args|
require "finder_loader"
require "publishing_api_finder_publisher"

begin
finder_loader = FinderLoader.new
finder = finder_loader.finder(args.name)
finder = FinderLoader.new.finder_by_slug(args.slug)
rescue StandardError => e
puts "Error: #{e.inspect}"
end

if finder
begin
PublishingApiFinderPublisher.new(finder).call
PublishingApiFinderPublisher.new([finder]).call
rescue GdsApi::HTTPServerError => e
puts "Error publishing finder: #{e.inspect}"
end
Expand Down
11 changes: 8 additions & 3 deletions lib/tasks/unpublish.rake
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require "services"

namespace :unpublish do
desc "Unpublish a Finder by file name and redirect the finder page to specific URL"
task :redirect_finder, %i[finder_file redirect_url] => :environment do |_, args|
desc "Unpublish a Finder by slug and redirect the finder page to specific URL"
task :redirect_finder, %i[slug redirect_url] => :environment do |_, args|
require "finder_loader"

schema = FinderLoader.new.finder(args.finder_file).first[:file]
begin
schema = FinderLoader.new.finder_by_slug(args.slug)[:file]
rescue StandardError => e
puts "Error: #{e.inspect}"
end

puts "=== Finder found ==="
puts "Slug: #{schema['base_path']}"
puts "Finder name: #{schema['name']}"
Expand Down
32 changes: 6 additions & 26 deletions spec/lib/finder_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,32 @@

RSpec.describe FinderLoader do
it "returns matching finder data objects" do
expect(File).to receive(:read).with("lib/documents/schemas/format-1.json")
.and_return('{"name":"format-1"}')
expect(File).to receive(:mtime).with("lib/documents/schemas/format-1.json")
.and_return("yesterday")

expect(Dir).to receive(:glob).with("lib/documents/schemas/*.json").and_return(%w[
lib/documents/schemas/format-1.json
lib/documents/schemas/format-2.json
])

expect(File).to receive(:read).with("lib/documents/schemas/format-1.json")
.and_return('{"name":"format-1"}')
expect(File).to receive(:mtime).with("lib/documents/schemas/format-1.json")
.and_return("yesterday")
expect(File).to receive(:read).with("lib/documents/schemas/format-2.json")
.and_return('{"name":"format-2"}')
expect(File).to receive(:mtime).with("lib/documents/schemas/format-2.json")
.and_return("today")

loader = FinderLoader.new
expect(loader.finders).to match_array([
expect(FinderLoader.new.finders).to match_array([
{
file: { "name" => "format-1" },
timestamp: "yesterday",
},
{

file: { "name" => "format-2" },
timestamp: "today",
},
])
end

it "returns one matching finder data object" do
expect(File).to receive(:read).with("lib/documents/schemas/format-1.json")
.and_return('{"name":"format-1"}')
expect(File).to receive(:mtime).with("lib/documents/schemas/format-1.json")
.and_return("yesterday")

expect(File).to receive(:exist?).with("lib/documents/schemas/format-1.json")
.and_return(true)

loader = FinderLoader.new
expect(loader.finder("format-1")).to match_array([
{
file: { "name" => "format-1" },
timestamp: "yesterday",
},
])
end

it "returns one matching finder data object by slug" do
expect(Dir).to receive(:glob).with("lib/documents/schemas/*.json").and_return(%w[
spec/fixtures/documents/schemas/licence_transactions.json
Expand Down

0 comments on commit 21b57a0

Please sign in to comment.