diff --git a/config.rb b/config.rb index 8489d3091..3cd454f10 100644 --- a/config.rb +++ b/config.rb @@ -14,21 +14,31 @@ csv_path = File.expand_path("data/inputs/apic.csv", __dir__) api_catalogue = ApiCatalogue.from_csv(csv_path) -api_catalogue.organisations_apis.each do |organisation, apis| +# Order organisations from A-Z in the Table of Contents, +# leaving a buffer from 0-999 for static content to be given priority +initial_org_weight = 1_000 + +api_catalogue.organisations_apis.each.with_index(initial_org_weight) do |(organisation, apis), org_weight| proxy( UrlHelpers.organisation_path(organisation), "organisation_index.html", locals: { organisation: organisation, apis: apis }, - data: { title: organisation.name }, + data: { + title: organisation.name, + weight: org_weight, + }, ignore: true, ) - apis.each do |api| + apis.each_with_index do |api, api_weight| proxy( UrlHelpers.api_path(organisation: organisation, api: api), "api_details.html", locals: { api: api }, - data: { title: api.name }, + data: { + title: api.name, + weight: api_weight, + }, ignore: true, ) end diff --git a/lib/api_catalogue.rb b/lib/api_catalogue.rb index 216603638..9e158f9b1 100644 --- a/lib/api_catalogue.rb +++ b/lib/api_catalogue.rb @@ -6,25 +6,28 @@ class ApiCatalogue def self.from_csv(csv_path) header_converter = lambda { |header| header.underscore.to_sym } - data = CSV + apis = CSV .foreach(csv_path, headers: true, header_converters: [header_converter]) .map { |row| Api.new(row.to_hash) } - .group_by(&:organisation) - .each_with_object({}) do |(organisation, apis), result| - organisation = Organisation.new(name: organisation, alternate_name: apis.first&.provider) - result[organisation] = apis.sort_by(&:name) - end - new(data) + new(apis) end attr_reader :organisations_apis - def initialize(organisations_apis) - @organisations_apis = organisations_apis + def initialize(apis) + @organisations_apis = group_by_organisation(apis) end private - attr_reader :data + def group_by_organisation(apis) + apis + .sort_by(&:organisation) + .group_by(&:organisation) + .each_with_object({}) do |(organisation, apis), result| + organisation = Organisation.new(name: organisation, alternate_name: apis.first&.provider) + result[organisation] = apis.sort_by(&:name) + end + end end diff --git a/spec/lib/api_catalogue_spec.rb b/spec/lib/api_catalogue_spec.rb index 0e6d458d6..77df7df46 100644 --- a/spec/lib/api_catalogue_spec.rb +++ b/spec/lib/api_catalogue_spec.rb @@ -1,30 +1,49 @@ require "api_catalogue" -RSpec.describe ApiCatalogue, "::from_csv" do - let(:csv_path) { File.expand_path("../../data/inputs/apic.csv", __dir__) } +RSpec.describe ApiCatalogue do + describe "::from_csv" do + let(:csv_path) { File.expand_path("../../data/inputs/apic.csv", __dir__) } - it "parses the CSV source" do - api_catalogue = described_class.from_csv(csv_path) + it "parses the CSV source" do + api_catalogue = described_class.from_csv(csv_path) - expect(api_catalogue.organisations_apis.size).to be > 10 + expect(api_catalogue.organisations_apis.size).to be > 10 - gds, gds_apis = api_catalogue.organisations_apis.detect do |org, _| - org.name.casecmp?("Government Digital Service") - end + gds, gds_apis = api_catalogue.organisations_apis.detect do |org, _| + org.name.casecmp?("Government Digital Service") + end + + notify_api = gds_apis.detect do |api| + api.name.casecmp?("GOV.UK Notify") + end + + expect(gds).to have_attributes( + name: "Government Digital Service", + alternate_name: "GDS", + ) - notify_api = gds_apis.detect do |api| - api.name.casecmp?("GOV.UK Notify") + expect(notify_api).to have_attributes( + name: "GOV.UK Notify", + description: be_present, + url: "https://api.notifications.service.gov.uk/", + ) end + end + + describe "ordering" do + it "sorts organisations and APIs from A-Z" do + apis = [ + instance_double(Api, organisation: "B", name: "B2", provider: nil), + instance_double(Api, organisation: "B", name: "B1", provider: nil), + instance_double(Api, organisation: "A", name: "A2", provider: nil), + instance_double(Api, organisation: "A", name: "A1", provider: nil), + ] - expect(gds).to have_attributes( - name: "Government Digital Service", - alternate_name: "GDS", - ) + api_catalogue = described_class.new(apis) + first_org, first_org_apis = api_catalogue.organisations_apis.first - expect(notify_api).to have_attributes( - name: "GOV.UK Notify", - description: be_present, - url: "https://api.notifications.service.gov.uk/", - ) + expect(first_org.name).to eq "A" + expect(first_org_apis.map(&:name)).to eq ["A1", "A2"] + end end end