Skip to content

Commit

Permalink
Change source for GdsApi.worldwide.world_location method
Browse files Browse the repository at this point in the history
This method currently returns data retrieved from the Whitehall API,
which is being deprecated.

Therefore updating the method to get the data from the World Content
Item.

We need to transform the response to ensure the output remains backwards
compatible with previous versions of this gem.

====

Example:

Response using Whitehall API

```
=> {"id"=>"https://www.gov.uk/api/world-locations/france",
 "title"=>"France",
 "format"=>"World location",
 "updated_at"=>"2017-08-28T09:40:55.000+01:00",
 "web_url"=>"https://www.gov.uk/world/france",
 "analytics_identifier"=>"WL48",
 "details"=>{"slug"=>"france", "iso2"=>"FR"},
 "organisations"=>{"id"=>"https://www.gov.uk/api/world-locations/france/organisations", "web_url"=>"https://www.gov.uk/world/france#organisations"},
 "content_id"=>"5e9f00ce-7706-11e4-a3cb-005056011aef",
 "_response_info"=>{"status"=>"ok", "links"=>[{"href"=>"https://www.gov.uk/api/world-locations/france", "rel"=>"self"}]}}
```

Response using World Content Item

```
 => {"id"=>"https://www.gov.uk/world/france",
  "title"=>"France",
  "format"=>"World location",
  "updated_at"=>"2017-08-28T09:40:55.000+01:00",
  "web_url"=>"https://www.gov.uk/world/france",
  "analytics_identifier"=>"WL48",
  "details"=>{"slug"=>"france", "iso2"=>"FR"},
  "organisations"=>{"id"=>"https://www.gov.uk/world/france#organisations", "web_url"=>"https://www.gov.uk/world/france#organisations"},
  "content_id"=>"5e9f00ce-7706-11e4-a3cb-005056011aef"}
```
  • Loading branch information
brucebolt committed Jul 18, 2023
1 parent ea01cd9 commit ca6aa61
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 84 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Change source for `GdsApi.worldwide.world_location` method (the response is backwards compatible).

# 88.2.0

* Add `whitehall_media` method to Asset Manager.
Expand Down
86 changes: 41 additions & 45 deletions lib/gds_api/test_helpers/worldwide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,48 @@ module Worldwide
# This also sets up the individual endpoints for each slug
# by calling stub_worldwide_api_has_location below
def stub_worldwide_api_has_locations(location_slugs)
international_delegation_slugs = location_slugs.select do |slug|
slug =~ /(delegation|mission)/
end

international_delegations = international_delegation_slugs.map do |slug|
{
"active": true,
"analytics_identifier": "WL1",
"content_id": "content_id_for_#{slug}",
"iso2": slug[0..1].upcase,
"name": titleize_slug(slug, title_case: true),
"slug": slug,
"updated_at": "2013-03-25T13:06:42+00:00",
}
end

world_locations = (location_slugs - international_delegation_slugs).map do |slug|
{
"active": true,
"analytics_identifier": "WL1",
"content_id": "content_id_for_#{slug}",
"iso2": slug[0..1].upcase,
"name": titleize_slug(slug, title_case: true),
"slug": slug,
"updated_at": "2013-03-25T13:06:42+00:00",
}
end

content_item = {
"details": {
"international_delegation": international_delegations,
"world_locations": world_locations,
},
}

stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/content/world")
.to_return(status: 200, body: content_item.to_json)

location_slugs.each { |s| stub_worldwide_api_has_location(s) }
pages = []
location_slugs.each_slice(20) do |slugs|
pages << slugs.map { |s| world_location_details_for_slug(s) }
pages << slugs.map { |s| stub_worldwide_api_has_locations(s) }
end

pages.each_with_index do |page, i|
Expand Down Expand Up @@ -51,54 +89,12 @@ def stub_worldwide_api_has_locations(location_slugs)
end
end

def stub_worldwide_api_has_selection_of_locations
stub_worldwide_api_has_locations %w[
afghanistan
angola
australia
bahamas
belarus
brazil
brunei
cambodia
chad
croatia
denmark
eritrea
france
ghana
iceland
japan
laos
luxembourg
malta
micronesia
mozambique
nicaragua
panama
portugal
sao-tome-and-principe
singapore
south-korea
sri-lanka
uk-delegation-to-council-of-europe
uk-delegation-to-organization-for-security-and-co-operation-in-europe
united-kingdom
venezuela
vietnam
]
end

def stub_worldwide_api_has_location(location_slug, details = nil)
details ||= world_location_for_slug(location_slug)
stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}")
.to_return(status: 200, body: details.to_json)
end

def stub_worldwide_api_does_not_have_location(location_slug)
stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}").to_return(status: 404)
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"
Expand All @@ -114,14 +110,14 @@ def stub_worldwide_api_has_no_organisations_for_location(location_slug)
end

def world_location_for_slug(slug)
singular_response_base.merge(world_location_details_for_slug(slug))
singular_response_base.merge(stub_worldwide_api_has_locations(slug))
end

# Constructs a sample world_location
#
# if the slug contains 'delegation' or 'mission' the format will be set to 'International delegation'
# othersiwe it will be set to 'World location'
def world_location_details_for_slug(slug)
def stub_worldwide_api_has_locations(slug)
{
"id" => "https://www.gov.uk/api/world-locations/#{slug}",
"title" => titleize_slug(slug, title_case: true),
Expand Down
39 changes: 38 additions & 1 deletion lib/gds_api/worldwide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ def world_locations
end

def world_location(location_slug)
get_json("#{base_url}/world-locations/#{location_slug}")
world_location = all_world_locations.find do |location|
location.dig("details", "slug") == location_slug
end

raise GdsApi::HTTPNotFound, 404 unless world_location

world_location
end

def organisations_for_world_location(location_slug)
Expand All @@ -18,4 +24,35 @@ def organisations_for_world_location(location_slug)
def base_url
"#{endpoint}/api"
end

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

world_locations = format_locations(content_item.dig("details", "world_locations"), "World location")
international_delegations = format_locations(content_item.dig("details", "international_delegations"), "International delegation")

Array(world_locations) + Array(international_delegations)
end

def format_locations(locations, type)
locations&.map do |location|
{
"id" => "#{Plek.new.website_root}/world/#{location['slug']}",
"title" => location["name"],
"format" => type,
"updated_at" => location["updated_at"],
"web_url" => "#{Plek.new.website_root}/world/#{location['slug']}",
"analytics_identifier" => location["analytics_identifier"],
"details" => {
"slug" => location["slug"],
"iso2" => location["iso2"],
},
"organisations" => {
"id" => "#{Plek.new.website_root}/world/#{location['slug']}#organisations",
"web_url" => "#{Plek.new.website_root}/world/#{location['slug']}#organisations",
},
"content_id" => location["content_id"],
}
end
end
end
36 changes: 0 additions & 36 deletions test/pacts/worldwide_api_pact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,6 @@
end

describe "#world_location" do
it "responds with 200 and requested world location" do
whitehall_api
.given("a world location exists")
.upon_receiving("a request to return a specific world location")
.with(
method: :get,
path: "/api/world-locations/france",
headers: GdsApi::JsonClient.default_request_headers,
)
.will_respond_with(
status: 200,
body: {
id: Pact.like("https://www.gov.uk/api/world-locations/france"),
title: "France",
format: "World location",
updated_at: Pact.like("2020-09-02T06:47:34.000+01:00"),
web_url: Pact.like("https://www.gov.uk/world/france"),
analytics_identifier: Pact.like("WL1"),
details: {
slug: "france",
iso2: nil,
},
organisations: Pact.like(
id: "https://www.gov.uk/api/world-locations/france/organisations",
web_url: "https://www.gov.uk/world/france#organisations",
),
content_id: Pact.like("5e9ecbce-7706-11e4-a3cb-005056011aef"),
},
headers: {
"Content-Type" => "application/json; charset=utf-8",
},
)

api_client.world_location("france")
end

describe "#organisations_for_world_location" do
it "responds with 200 and all worldwide organisations" do
whitehall_api
Expand Down
4 changes: 2 additions & 2 deletions test/worldwide_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@

describe "fetching a world location" do
it "should return the details" do
stub_worldwide_api_has_location("rohan")
stub_worldwide_api_has_locations(%w[rohan])

response = @api.world_location("rohan")
assert_equal "Rohan", response["title"]
end

it "raises for a non-existent location" do
stub_worldwide_api_does_not_have_location("non-existent")
stub_worldwide_api_has_locations(%w[rohan])

assert_raises(GdsApi::HTTPNotFound) do
@api.world_location("non-existent")
Expand Down

0 comments on commit ca6aa61

Please sign in to comment.