diff --git a/app/lib/finder_loader.rb b/app/lib/finder_loader.rb index 98d41e248..739285e7c 100644 --- a/app/lib/finder_loader.rb +++ b/app/lib/finder_loader.rb @@ -23,6 +23,19 @@ def finder(name) end end + def finder_by_slug(slug) + schema_file = files.find do |file| + File.foreach(file).grep(/"base_path": "\/#{slug}"/).any? + end + + raise "Could not find any schema with slug: #{slug}" if schema_file.nil? + + { + file: MultiJson.load(File.read(schema_file)), + timestamp: File.mtime(schema_file), + } + end + private def files diff --git a/spec/lib/finder_loader_spec.rb b/spec/lib/finder_loader_spec.rb index e6f75569f..2290bd5b8 100644 --- a/spec/lib/finder_loader_spec.rb +++ b/spec/lib/finder_loader_spec.rb @@ -2,13 +2,12 @@ require "finder_loader" RSpec.describe FinderLoader do - before 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") - end - it "returns matching finder data objects" do + 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 @@ -33,6 +32,11 @@ 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) @@ -44,4 +48,25 @@ }, ]) 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 + ]) + expect(File).to receive(:mtime).with("spec/fixtures/documents/schemas/licence_transactions.json") + .and_return("today") + + loaded_finder = FinderLoader.new.finder_by_slug("find-licences") + expect(loaded_finder[:file]).to include({"base_path" => "/find-licences"}) + expect(loaded_finder[:timestamp]).to eq("today") + end + + + it "errors when no finder is found with slug" do + expect(Dir).to receive(:glob).with("lib/documents/schemas/*.json").and_return(%w[ + spec/fixtures/documents/schemas/licence_transactions.json + ]) + + expect { FinderLoader.new.finder_by_slug("test-slug") }.to raise_error("Could not find any schema with slug: test-slug") + end end