Skip to content

Commit

Permalink
Refer to PlacesManager internally
Browse files Browse the repository at this point in the history
  • Loading branch information
KludgeKML committed May 13, 2024
1 parent b06ddc6 commit d4c2fa8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
40 changes: 20 additions & 20 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::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
4 changes: 2 additions & 2 deletions test/functional/place_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PlaceControllerTest < ActionController::TestCase
"url" => "http://www.example.com/london_ips_office",
}], "slug", valid_postcode, 10, nil)
query_hash = { "postcode" => invalid_postcode, "limit" => Frontend::PLACES_MANAGER_QUERY_LIMIT }
return_data = { "error" => ImminenceResponse::INVALID_POSTCODE }
return_data = { "error" => PlacesManagerResponse::INVALID_POSTCODE }
stub_places_manager_places_request("slug", query_hash, return_data, 400)
end

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

0 comments on commit d4c2fa8

Please sign in to comment.