Skip to content

Commit

Permalink
Merge pull request #4058 from alphagov/gds-api-adapters-96.0.0-places…
Browse files Browse the repository at this point in the history
…-manager

Gds api adapters 96.0.0 Places Manager breaking changes
  • Loading branch information
KludgeKML committed May 13, 2024
2 parents e94904c + d4c2fa8 commit da07725
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ GEM
parslet
ffi (1.16.3)
find_a_port (1.0.1)
gds-api-adapters (95.1.0)
gds-api-adapters (96.0.0)
addressable
link_header
null_logger
Expand Down Expand Up @@ -236,7 +236,7 @@ GEM
method_source (1.0.0)
mime-types (3.5.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2024.0305)
mime-types-data (3.2024.0507)
mini_mime (1.1.5)
mini_portile2 (2.8.6)
minitest (5.22.3)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Frontend is a Ruby on Rails application and should follow [our Rails app convent
- [alphagov/static](https://github.com/alphagov/static) - provides shared templates, styles, and JavaScript
- [alphagov/content-store](https://github.com/alphagov/content-store) - provides raw data for rendering formats
- [alphagov/locations-api](https://github.com/alphagov/locations-api) - provides postcode lookups
- [alphagov/imminence](https://github.com/alphagov/imminence) - provides places lookups (e.g. for find-my-nearest)
- [alphagov/imminence](https://github.com/alphagov/imminence) - (Now called Places Manager) provides places lookups (e.g. for find-my-nearest)
- [alphagov/publishing-api](https://github.com/alphagov/publishing-api) - this app sends data to the content-store

### Running the application
Expand Down
42 changes: 21 additions & 21 deletions app/controllers/place_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def show
def find
@location_error = location_error

if postcode_provided? && imminence_response.addresses_returned?
@options = imminence_response.addresses.each.map do |address|
if postcode_provided? && places_manager_response.addresses_returned?
@options = places_manager_response.addresses.each.map do |address|
{ text: address["address"], value: address["local_authority_slug"] }
end
@change_path = place_path(slug: params[:slug])
Expand All @@ -32,8 +32,8 @@ def find
helper_method :location_error

def publication
@publication ||= if postcode_provided? && imminence_response.places_found?
PlacePresenter.new(content_item_hash, imminence_response.places)
@publication ||= if postcode_provided? && places_manager_response.places_found?
PlacePresenter.new(content_item_hash, places_manager_response.places)
else
PlacePresenter.new(content_item_hash)
end
Expand All @@ -53,41 +53,41 @@ def local_authority_slug

def location_error
return LocationError.new(INVALID_POSTCODE) unless postcode_provided?
return LocationError.new(INVALID_POSTCODE) if imminence_response.invalid_postcode?
return LocationError.new(INVALID_POSTCODE) if places_manager_response.invalid_postcode?

LocationError.new(NO_LOCATION) if imminence_response.places_not_found?
LocationError.new(NO_LOCATION) if places_manager_response.places_not_found?
end

def imminence_response
@imminence_response ||= places_from_imminence
def places_manager_response
@places_manager_response ||= places_from_places_manager
end

def places_from_imminence
imminence_response = Frontend.imminence_api.places_for_postcode(
def places_from_places_manager
places_manager_response = Frontend.places_manager_api.places_for_postcode(
content_item_hash["details"]["place_type"],
postcode,
Frontend::IMMINENCE_QUERY_LIMIT,
Frontend::PLACES_MANAGER_QUERY_LIMIT,
local_authority_slug,
)
imminence_response_from_data(postcode, imminence_response, nil)
places_manager_response_from_data(postcode, places_manager_response, nil)
rescue GdsApi::HTTPErrorResponse => e
raise e unless ImminenceResponse.handled_error?(e)
raise e unless PlacesManagerResponse.handled_error?(e)

imminence_response_from_data(postcode, nil, e)
places_manager_response_from_data(postcode, nil, e)
end

def imminence_response_from_data(postcode, imminence_response, error)
return ImminenceResponse.new(postcode, [], [], error) if error
def places_manager_response_from_data(postcode, places_manager_response, error)
return PlacesManagerResponse.new(postcode, [], [], error) if error

imminence_data = imminence_response.to_hash
places_manager_data = places_manager_response.to_hash
places = []
addresses = []

if imminence_data["status"] == "ok"
places = imminence_data["places"]
if places_manager_data["status"] == "ok"
places = places_manager_data["places"]
else
addresses = imminence_data["addresses"]
addresses = places_manager_data["addresses"]
end
ImminenceResponse.new(postcode, places, addresses, error)
PlacesManagerResponse.new(postcode, places, addresses, error)
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ImminenceResponse
class PlacesManagerResponse
INVALID_POSTCODE = "invalidPostcodeError".freeze
NO_LOCATION = "validPostcodeNoLocation".freeze

Expand Down
5 changes: 0 additions & 5 deletions config/initializers/imminence.rb

This file was deleted.

5 changes: 5 additions & 0 deletions config/initializers/places_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "gds_api/places_manager"

Frontend.places_manager_api = GdsApi::PlacesManager.new(Plek.new.find("places-manager"))

Frontend::PLACES_MANAGER_QUERY_LIMIT = 10
2 changes: 1 addition & 1 deletion lib/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Frontend
mattr_accessor :organisations_search_client
mattr_accessor :search_client
mattr_accessor :locations_api
mattr_accessor :imminence_api
mattr_accessor :places_manager_api
mattr_accessor :local_links_manager_api
mattr_accessor :govuk_website_root
end
2 changes: 1 addition & 1 deletion startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if [[ $1 == "--live" ]] ; then
PLEK_SERVICE_LICENSIFY_URI=${PLEK_SERVICE_LICENSIFY_URI-https://licensify.publishing.service.gov.uk} \
PLEK_SERVICE_CONTENT_STORE_URI=${PLEK_SERVICE_CONTENT_STORE_URI-https://www.gov.uk/api} \
PLEK_SERVICE_STATIC_URI=${PLEK_SERVICE_STATIC_URI-https://assets.publishing.service.gov.uk} \
PLEK_SERVICE_IMMINENCE_URI=${PLEK_SERVICE_IMMINENCE_URI-https://imminence.publishing.service.gov.uk} \
PLEK_SERVICE_PLACES_MANAGER_URI=${PLEK_SERVICE_PLACES_MANAGER_URI-https://places-manager.publishing.service.gov.uk} \
./bin/dev
else
echo "ERROR: other startup modes are not supported"
Expand Down
14 changes: 7 additions & 7 deletions test/functional/place_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require "test_helper"
require "gds_api/test_helpers/imminence"
require "gds_api/test_helpers/places_manager"

class PlaceControllerTest < ActionController::TestCase
include GdsApi::TestHelpers::Imminence
include GdsApi::TestHelpers::PlacesManager

valid_postcode = "SW1A 2AA"
invalid_postcode = "1234 2AA"

setup do
content_store_has_random_item(base_path: "/slug", schema: "place", details: { "place_type" => "slug" })
stub_imminence_has_places_for_postcode([{
stub_places_manager_has_places_for_postcode([{
"access_notes" => "The London Passport Office is fully accessible to wheelchair users. ",
"address1" => nil,
"address2" => "89 Eccleston Square",
Expand All @@ -27,9 +27,9 @@ class PlaceControllerTest < ActionController::TestCase
"town" => "London",
"url" => "http://www.example.com/london_ips_office",
}], "slug", valid_postcode, 10, nil)
query_hash = { "postcode" => invalid_postcode, "limit" => Frontend::IMMINENCE_QUERY_LIMIT }
return_data = { "error" => ImminenceResponse::INVALID_POSTCODE }
stub_imminence_places_request("slug", query_hash, return_data, 400)
query_hash = { "postcode" => invalid_postcode, "limit" => Frontend::PLACES_MANAGER_QUERY_LIMIT }
return_data = { "error" => PlacesManagerResponse::INVALID_POSTCODE }
stub_places_manager_places_request("slug", query_hash, return_data, 400)
end

context "GET show" do
Expand Down Expand Up @@ -59,7 +59,7 @@ class PlaceControllerTest < ActionController::TestCase
context "with invalid postcode" do
should "show location error" do
post :find, params: { slug: "slug", postcode: invalid_postcode }
assert_equal @controller.view_assigns["location_error"].postcode_error, LocationError.new(ImminenceResponse::INVALID_POSTCODE).postcode_error
assert_equal @controller.view_assigns["location_error"].postcode_error, LocationError.new(PlacesManagerResponse::INVALID_POSTCODE).postcode_error
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions test/integration/place_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "integration_test_helper"
require "gds_api/test_helpers/imminence"
require "gds_api/test_helpers/places_manager"

class PlacesTest < ActionDispatch::IntegrationTest
include GdsApi::TestHelpers::Imminence
include GdsApi::TestHelpers::PlacesManager

setup do
@payload = {
Expand Down Expand Up @@ -119,7 +119,7 @@ class PlacesTest < ActionDispatch::IntegrationTest

context "given a valid postcode" do
setup do
stub_imminence_has_places_for_postcode(@places, "find-passport-offices", "SW1A 1AA", Frontend::IMMINENCE_QUERY_LIMIT, nil)
stub_places_manager_has_places_for_postcode(@places, "find-passport-offices", "SW1A 1AA", Frontend::PLACES_MANAGER_QUERY_LIMIT, nil)

visit "/passport-interview-office"
fill_in "Enter a postcode", with: "SW1A 1AA"
Expand Down Expand Up @@ -206,7 +206,7 @@ class PlacesTest < ActionDispatch::IntegrationTest
setup do
@places = []

stub_imminence_has_places_for_postcode(@places, "find-passport-offices", "SW1A 1AA", Frontend::IMMINENCE_QUERY_LIMIT, nil)
stub_places_manager_has_places_for_postcode(@places, "find-passport-offices", "SW1A 1AA", Frontend::PLACES_MANAGER_QUERY_LIMIT, nil)

visit "/passport-interview-office"
fill_in "Enter a postcode", with: "SW1A 1AA"
Expand Down Expand Up @@ -264,9 +264,9 @@ class PlacesTest < ActionDispatch::IntegrationTest

context "given an invalid postcode" do
setup do
query_hash = { "postcode" => "BAD POSTCODE", "limit" => Frontend::IMMINENCE_QUERY_LIMIT }
query_hash = { "postcode" => "BAD POSTCODE", "limit" => Frontend::PLACES_MANAGER_QUERY_LIMIT }
return_data = { "error" => "invalidPostcodeError" }
stub_imminence_places_request("find-passport-offices", query_hash, return_data, 400)
stub_places_manager_places_request("find-passport-offices", query_hash, return_data, 400)

visit "/passport-interview-office"
fill_in "Enter a postcode", with: "BAD POSTCODE"
Expand All @@ -292,10 +292,10 @@ class PlacesTest < ActionDispatch::IntegrationTest

context "given a valid postcode with no locations returned" do
setup do
query_hash = { "postcode" => "JE4 5TP", "limit" => Frontend::IMMINENCE_QUERY_LIMIT }
query_hash = { "postcode" => "JE4 5TP", "limit" => Frontend::PLACES_MANAGER_QUERY_LIMIT }
return_data = { "error" => "validPostcodeNoLocation" }

stub_imminence_places_request("find-passport-offices", query_hash, return_data, 400)
stub_places_manager_places_request("find-passport-offices", query_hash, return_data, 400)

visit "/passport-interview-office"
fill_in "Enter a postcode", with: "JE4 5TP"
Expand All @@ -322,7 +322,7 @@ class PlacesTest < ActionDispatch::IntegrationTest
{ "address" => "House 3", "local_authority_slug" => "ceechester" },
]

stub_imminence_has_multiple_authorities_for_postcode(addresses, "find-passport-offices", "CH25 9BJ", Frontend::IMMINENCE_QUERY_LIMIT)
stub_places_manager_has_multiple_authorities_for_postcode(addresses, "find-passport-offices", "CH25 9BJ", Frontend::PLACES_MANAGER_QUERY_LIMIT)

visit "/passport-interview-office"
fill_in "Enter a postcode", with: "CH25 9BJ"
Expand All @@ -334,10 +334,10 @@ class PlacesTest < ActionDispatch::IntegrationTest
end
end

context "given an internal error response from imminence" do
context "given an internal error response from places manager" do
setup do
query_hash = { "postcode" => "JE4 5TP", "limit" => Frontend::IMMINENCE_QUERY_LIMIT }
stub_imminence_places_request("find-passport-offices", query_hash, {}, 500)
query_hash = { "postcode" => "JE4 5TP", "limit" => Frontend::PLACES_MANAGER_QUERY_LIMIT }
stub_places_manager_places_request("find-passport-offices", query_hash, {}, 500)

visit "/passport-interview-office"
fill_in "Enter a postcode", with: "JE4 5TP"
Expand Down

0 comments on commit da07725

Please sign in to comment.