Skip to content

Commit

Permalink
Merge pull request #1204 from alphagov/update-world-organisation-method
Browse files Browse the repository at this point in the history
Change data source for `organisations_for_world_location` method
  • Loading branch information
brucebolt authored Jul 31, 2023
2 parents 8eaecf1 + 2ccad30 commit 3e5616d
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 588 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ jobs:
with:
pact_artifact: pacts

whitehall_pact:
needs: generate_pacts
uses: alphagov/whitehall/.github/workflows/pact-verify.yml@main
with:
pact_artifact: pacts

publish_pacts:
needs:
- account_api_pact
Expand All @@ -119,7 +113,6 @@ jobs:
- link_checker_api_pact
- locations_api_pact
- publishing_api_pact
- whitehall_pact
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
steps:
Expand Down
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
2 changes: 1 addition & 1 deletion lib/gds_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GdsApi
VERSION = "89.0.0".freeze
VERSION = "90.0.0".freeze
end
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 3e5616d

Please sign in to comment.