Skip to content

Commit

Permalink
Change data source for organisations_for_world_location method
Browse files Browse the repository at this point in the history
This ends reliance on the Whitehall API by changing the data source to
use Search API and Content Store.

The output is in an identical format, but is no longer paginated.

Also increasing the test coverage by ensuring we are correctly
transforming the data. Previously we only checked that the title was
correct.
  • Loading branch information
brucebolt committed Jul 31, 2023
1 parent ade2e5f commit b2608f2
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 569 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 90.0.0

* BREAKING: Change source for `GdsApi.worldwide.organisations_for_world_location` method, remove pagination and remove `stub_worldwide_api_has_organisations_for_location` method.

# 89.0.0

* BREAKING: Change source for `GdsApi.worldwide.world_locations` method and remove pagination.
Expand Down
30 changes: 20 additions & 10 deletions lib/gds_api/test_helpers/worldwide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,28 @@ def stub_worldwide_api_has_location(location_slug)
stub_worldwide_api_has_locations([location_slug])
end

def stub_worldwide_api_has_organisations_for_location(location_slug, json_or_hash)
json = json_or_hash.is_a?(Hash) ? json_or_hash.to_json : json_or_hash
url = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}/organisations"
stub_request(:get, url)
.to_return(status: 200, body: json, headers: { "Link" => "<#{url}; rel\"self\"" })
def stub_search_api_has_organisations_for_location(location_slug, organisation_content_items)
response = {
"results": organisation_content_items.map do |content_item|
{
"link": content_item["base_path"],
}
end,
}

stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/search.json?filter_format=worldwide_organisation&filter_world_locations=#{location_slug}")
.to_return(status: 200, body: response.to_json)

organisation_content_items.each do |content_item|
stub_content_store_has_worldwide_organisation(content_item)
end
end

def stub_worldwide_api_has_no_organisations_for_location(location_slug)
details = { "results" => [], "total" => 0, "_response_info" => { "status" => "ok" } }
url = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}/organisations"
stub_request(:get, url)
.to_return(status: 200, body: details.to_json, headers: { "Link" => "<#{url}; rel\"self\"" })
def stub_content_store_has_worldwide_organisation(content_item)
base_path = content_item["base_path"]

stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/content#{base_path}")
.to_return(status: 200, body: content_item.to_json)
end
end
end
Expand Down
89 changes: 88 additions & 1 deletion lib/gds_api/worldwide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def world_location(location_slug)
end

def organisations_for_world_location(location_slug)
get_list("#{base_url}/world-locations/#{location_slug}/organisations")
worldwide_organisations = worldwide_organisations_for_location(location_slug)

worldwide_organisations.map do |organisation|
worldwide_organisation(organisation["link"])
end
end

private
Expand Down Expand Up @@ -55,4 +59,87 @@ def format_locations(locations, type)
}
end
end

def worldwide_organisations_for_location(world_location)
search_results = JSON.parse(get_raw("#{base_url}/search.json?filter_format=worldwide_organisation&filter_world_locations=#{world_location}"))

search_results["results"]
end

def worldwide_organisation(path)
content_item = JSON.parse(get_raw("#{base_url}/content#{path}"))

{
"id" => "#{Plek.new.website_root}#{path}",
"title" => content_item["title"],
"format" => "Worldwide Organisation",
"updated_at" => content_item["updated_at"],
"web_url" => "#{Plek.new.website_root}#{path}",
"details" => {
"slug" => path.gsub("/world/organisations/", ""),
},
"analytics_identifier" => content_item["analytics_identifier"],
"offices" => {
"main" => format_office(content_item.dig("links", "main_office", 0)),
"other" => content_item.dig("links", "home_page_offices")&.map do |office|
format_office(office)
end || [],
},
"sponsors" => content_item.dig("links", "sponsoring_organisations")&.map do |sponsor|
format_sponsor(sponsor)
end || [],
}
end

def format_office(office)
return {} unless office

contact = office.dig("links", "contact", 0)

{
"title" => office["title"],
"format" => "World Office",
"updated_at" => office["public_updated_at"],
"web_url" => office["web_url"],
"details" => {
"email" => contact&.dig("details", "email_addresses"),
"description" => contact&.dig("details", "description"),
"contact_form_url" => contact&.dig("details", "contact_form_links"),
"access_and_opening_times" => office.dig("details", "access_and_opening_times"),
"type" => office.dig("details", "type"),
},
"address" => {
"adr" => {
"fn" => contact&.dig("details", "post_addresses", 0, "title"),
"street-address" => contact&.dig("details", "post_addresses", 0, "street_address"),
"postal-code" => contact&.dig("details", "post_addresses", 0, "postal_code"),
"locality" => contact&.dig("details", "post_addresses", 0, "locality"),
"region" => contact&.dig("details", "post_addresses", 0, "region"),
"country-name" => contact&.dig("details", "post_addresses", 0, "world_location"),
},
},
"contact_numbers" => contact&.dig("details", "phone_numbers")&.map do |phone_number|
{
"label" => phone_number["title"],
"number" => phone_number["number"],
}
end,
"services" => contact&.dig("details", "services")&.map do |service|
{
title: service["title"],
type: service["type"],
}
end,
}
end

def format_sponsor(sponsor)
{
"title" => sponsor["title"],
"web_url" => sponsor["web_url"],
"details" => {
"acronym" => sponsor.dig("details", "acronym"),
},
}
end
end
Loading

0 comments on commit b2608f2

Please sign in to comment.