Browse Source

CLDC-3508 Search both datasets for UPRN (#2479)

* Search both datasets for UPRN

* Update presenter

* Update request stubs
pull/2491/head
kosiakkatrina 7 months ago committed by GitHub
parent
commit
4c0c0d15eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      app/services/uprn_client.rb
  2. 21
      app/services/uprn_data_presenter.rb
  3. 10
      spec/request_helper.rb
  4. 2
      spec/services/exports/lettings_log_export_service_spec.rb
  5. 26
      spec/services/uprn_client_spec.rb
  6. 47
      spec/services/uprn_data_presenter_spec.rb

3
app/services/uprn_client.rb

@ -20,7 +20,7 @@ class UprnClient
end end
def result def result
@result ||= JSON.parse(response.body).dig("results", 0, "DPA") @result ||= JSON.parse(response.body).dig("results", 0, "DPA") || JSON.parse(response.body).dig("results", 0, "LPI")
end end
private private
@ -39,6 +39,7 @@ private
params = { params = {
uprn:, uprn:,
key: ENV["OS_DATA_KEY"], key: ENV["OS_DATA_KEY"],
dataset: %w[DPA LPI].join(","),
} }
uri.query = URI.encode_www_form(params) uri.query = URI.encode_www_form(params)
uri.to_s uri.to_s

21
app/services/uprn_data_presenter.rb

@ -8,10 +8,22 @@ class UprnDataPresenter
end end
def postcode def postcode
data["POSTCODE"] result_from_lpi? ? data["POSTCODE_LOCATOR"] : data["POSTCODE"]
end end
def address_line1 def address_line1
if result_from_lpi?
data.values_at(
"ORGANISATION",
"SAO_TEXT",
"PAO_START_NUMBER",
"STREET_DESCRIPTION",
"LOCALITY_NAME",
"ADMINISTRATIVE_AREA",
).compact
.join(", ")
.titleize
else
data.values_at( data.values_at(
"PO_BOX_NUMBER", "PO_BOX_NUMBER",
"ORGANISATION_NAME", "ORGANISATION_NAME",
@ -25,6 +37,7 @@ class UprnDataPresenter
.join(", ") .join(", ")
.titleize .titleize
end end
end
def address_line2 def address_line2
data.values_at( data.values_at(
@ -36,6 +49,10 @@ class UprnDataPresenter
end end
def town_or_city def town_or_city
data["POST_TOWN"].titleize result_from_lpi? ? data["TOWN_NAME"].titleize : data["POST_TOWN"].titleize
end
def result_from_lpi?
data["LPI_KEY"].present?
end end
end end

10
spec/request_helper.rb

@ -34,7 +34,7 @@ module RequestHelper
], ],
}.to_json }.to_json
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=1") WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=1")
.to_return(status: 200, body:, headers: {}) .to_return(status: 200, body:, headers: {})
body = { body = {
@ -50,7 +50,7 @@ module RequestHelper
], ],
}.to_json }.to_json
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key&uprn=121") WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key&uprn=121")
.to_return(status: 200, body:, headers: {}) .to_return(status: 200, body:, headers: {})
body = { body = {
@ -65,7 +65,7 @@ module RequestHelper
], ],
}.to_json }.to_json
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=123") WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=123")
.to_return(status: 200, body:, headers: {}) .to_return(status: 200, body:, headers: {})
body = { body = {
@ -80,10 +80,10 @@ module RequestHelper
], ],
}.to_json }.to_json
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=12") WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=12")
.to_return(status: 200, body:, headers: {}) .to_return(status: 200, body:, headers: {})
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=1234567890123") WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=1234567890123")
.to_return(status: 404, body: "", headers: {}) .to_return(status: 404, body: "", headers: {})
template = Addressable::Template.new "https://api.os.uk/search/places/v1/find?key=OS_DATA_KEY&maxresults=10&minmatch=0.4&query={+address_query}" template = Addressable::Template.new "https://api.os.uk/search/places/v1/find?key=OS_DATA_KEY&maxresults=10&minmatch=0.4&query={+address_query}"

2
spec/services/exports/lettings_log_export_service_spec.rb

@ -181,7 +181,7 @@ RSpec.describe Exports::LettingsLogExportService do
before do before do
Timecop.freeze(Time.zone.local(2023, 4, 3)) Timecop.freeze(Time.zone.local(2023, 4, 3))
Singleton.__init__(FormHandler) Singleton.__init__(FormHandler)
stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=100023336956") stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=100023336956")
.to_return(status: 200, body: '{"status":200,"results":[{"DPA":{ .to_return(status: 200, body: '{"status":200,"results":[{"DPA":{
"PO_BOX_NUMBER": "fake", "PO_BOX_NUMBER": "fake",
"ORGANISATION_NAME": "org", "ORGANISATION_NAME": "org",

26
spec/services/uprn_client_spec.rb

@ -8,7 +8,7 @@ describe UprnClient do
end end
def stub_api_request(body:, status: 200) def stub_api_request(body:, status: 200)
stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=123") stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=123")
.to_return(status:, body:, headers: {}) .to_return(status:, body:, headers: {})
end end
@ -37,7 +37,28 @@ describe UprnClient do
end end
end end
context "when results empty" do context "when DPA results empty" do
context "and LPI result is present" do
let(:valid_lpi_response) do
{ results: [{ LPI: { postcode_locator: "LPI postcode", LPI_KEY: "123" } }] }.to_json
end
before do
stub_api_request(body: valid_lpi_response, status: 200)
client.call
end
it "returns result" do
expect(client.result).to eq({ "postcode_locator" => "LPI postcode", "LPI_KEY" => "123" })
end
it "returns no error" do
expect(client.error).to be_nil
end
end
context "and LPI results empty" do
before do before do
stub_api_request(body: {}.to_json) stub_api_request(body: {}.to_json)
@ -48,6 +69,7 @@ describe UprnClient do
expect(client.error).to eq("UPRN is not recognised. Check the number, or enter the address") expect(client.error).to eq("UPRN is not recognised. Check the number, or enter the address")
end end
end end
end
context "with results" do context "with results" do
before do before do

47
spec/services/uprn_data_presenter_spec.rb

@ -1,6 +1,9 @@
require "rails_helper" require "rails_helper"
describe UprnDataPresenter do describe UprnDataPresenter do
let(:presenter) { described_class.new(data) }
describe "DPA data" do
let(:data) do let(:data) do
JSON.parse( JSON.parse(
'{ '{
@ -28,8 +31,6 @@ describe UprnDataPresenter do
) )
end end
let(:presenter) { described_class.new(data) }
describe "#postcode" do describe "#postcode" do
it "returns postcode" do it "returns postcode" do
expect(presenter.postcode).to eq("postcode") expect(presenter.postcode).to eq("postcode")
@ -63,4 +64,46 @@ describe UprnDataPresenter do
end end
end end
end end
end
describe "LPI data" do
let(:data) do
JSON.parse(
'{
"UPRN": "UPRN",
"ADDRESS": "flat 1, 22, street name, posttown, postcode",
"SAO_TEXT": "flat 1",
"PAO_START_NUMBER": "22",
"STREET_DESCRIPTION": "street name",
"TOWN_NAME": "posttown",
"POSTCODE_LOCATOR": "postcode",
"LPI_KEY": "LPI_KEY"
}',
)
end
describe "#postcode" do
it "returns postcode" do
expect(presenter.postcode).to eq("postcode")
end
end
describe "#address_line1" do
it "returns address_line1" do
expect(presenter.address_line1).to eq("Flat 1, 22, Street Name")
end
end
describe "#address_line2" do
it "returns address_line2" do
expect(presenter.address_line2).to be_nil
end
end
describe "#town_or_city" do
it "returns town_or_city" do
expect(presenter.town_or_city).to eq("Posttown")
end
end
end
end end

Loading…
Cancel
Save