Skip to content

Commit

Permalink
Order orgs & APIs in ToC from A-Z
Browse files Browse the repository at this point in the history
  • Loading branch information
odlp committed Oct 2, 2020
1 parent 14b5565 commit f888c2c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
18 changes: 14 additions & 4 deletions config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 13 additions & 10 deletions lib/api_catalogue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 38 additions & 19 deletions spec/lib/api_catalogue_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f888c2c

Please sign in to comment.