Skip to content

Commit

Permalink
Change source for GdsApi.worldwide.world_locations 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.

This is a breaking change as we have removed the pagination from the
response. This is to avoid multiple requests to the content item, when
all the results are included anyway.
  • Loading branch information
brucebolt committed Jul 18, 2023
1 parent ca6aa61 commit 921942f
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 135 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

* BREAKING: Change source for `GdsApi.worldwide.world_locations` method and remove pagination.
* Change source for `GdsApi.worldwide.world_location` method (the response is backwards compatible).

# 88.2.0
Expand Down
74 changes: 2 additions & 72 deletions lib/gds_api/test_helpers/worldwide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ module Worldwide

WORLDWIDE_API_ENDPOINT = Plek.new.website_root

# Sets up the index endpoints for the given country slugs
# The stubs are setup to paginate in chunks of 20
#
# 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)/
Expand Down Expand Up @@ -51,48 +46,10 @@ def stub_worldwide_api_has_locations(location_slugs)

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| stub_worldwide_api_has_locations(s) }
end

pages.each_with_index do |page, i|
page_details = plural_response_base.merge(
"results" => page,
"total" => location_slugs.size,
"pages" => pages.size,
"current_page" => i + 1,
"page_size" => 20,
"start_index" => i * 20 + 1,
)

links = { self: "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 1}" }
links[:next] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 2}" if pages[i + 1]
links[:previous] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i}" unless i.zero?
page_details["_response_info"]["links"] = []
link_headers = []
links.each do |rel, href|
page_details["_response_info"]["links"] << { "rel" => rel, "href" => href }
link_headers << "<#{href}>; rel=\"#{rel}\""
end

stub_request(:get, links[:self])
.to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") })

next unless i.zero?

# First page exists at URL with and without page param
stub_request(:get, links[:self].sub(/\?page=1/, ""))
.to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") })
end
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)
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)
Expand All @@ -108,33 +65,6 @@ def stub_worldwide_api_has_no_organisations_for_location(location_slug)
stub_request(:get, url)
.to_return(status: 200, body: details.to_json, headers: { "Link" => "<#{url}; rel\"self\"" })
end

def world_location_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 stub_worldwide_api_has_locations(slug)
{
"id" => "https://www.gov.uk/api/world-locations/#{slug}",
"title" => titleize_slug(slug, title_case: true),
"format" => (slug =~ /(delegation|mission)/ ? "International delegation" : "World location"),
"updated_at" => "2013-03-25T13:06:42+00:00",
"web_url" => "https://www.gov.uk/government/world/#{slug}",
"details" => {
"slug" => slug,
"iso2" => slug[0..1].upcase,
},
"organisations" => {
"id" => "https://www.gov.uk/api/world-locations/#{slug}/organisations",
"web_url" => "https://www.gov.uk/government/world/#{slug}#organisations",
},
"content_id" => "content_id_for_#{slug}",
}
end
end
end
end
2 changes: 1 addition & 1 deletion lib/gds_api/worldwide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class GdsApi::Worldwide < GdsApi::Base
def world_locations
get_list("#{base_url}/world-locations")
all_world_locations
end

def world_location(location_slug)
Expand Down
42 changes: 0 additions & 42 deletions test/pacts/worldwide_api_pact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,6 @@

let(:api_client) { GdsApi::Worldwide.new(whitehall_api_host) }

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

api_client.world_locations
end
end

describe "#world_location" do
describe "#organisations_for_world_location" do
it "responds with 200 and all worldwide organisations" do
Expand Down
21 changes: 1 addition & 20 deletions test/worldwide_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,7 @@
stub_worldwide_api_has_locations(country_slugs)

response = @api.world_locations
assert_equal(country_slugs, response.map { |r| r["details"]["slug"] })
assert_equal "Rohan", response["results"][2]["title"]
end

it "should handle the pagination" do
country_slugs = (1..50).map { |n| "country-#{n}" }
stub_worldwide_api_has_locations(country_slugs)

response = @api.world_locations
assert_equal(
country_slugs,
response.with_subsequent_pages.map { |r| r["details"]["slug"] },
)
end

it "should raise error if endpoint 404s" do
stub_request(:get, "#{@base_api_url}/api/world-locations").to_return(status: 404)
assert_raises GdsApi::HTTPNotFound do
@api.world_locations
end
assert_equal(country_slugs, response.map { |r| r.dig("details", "slug") })
end
end

Expand Down

0 comments on commit 921942f

Please sign in to comment.