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. 45
      app/services/uprn_data_presenter.rb
  3. 10
      spec/request_helper.rb
  4. 2
      spec/services/exports/lettings_log_export_service_spec.rb
  5. 36
      spec/services/uprn_client_spec.rb
  6. 133
      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

45
app/services/uprn_data_presenter.rb

@ -8,22 +8,35 @@ 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
data.values_at( if result_from_lpi?
"PO_BOX_NUMBER", data.values_at(
"ORGANISATION_NAME", "ORGANISATION",
"DEPARTMENT_NAME", "SAO_TEXT",
"SUB_BUILDING_NAME", "PAO_START_NUMBER",
"BUILDING_NAME", "STREET_DESCRIPTION",
"BUILDING_NUMBER", "LOCALITY_NAME",
"DEPENDENT_THOROUGHFARE_NAME", "ADMINISTRATIVE_AREA",
"THOROUGHFARE_NAME", ).compact
).compact .join(", ")
.join(", ") .titleize
.titleize else
data.values_at(
"PO_BOX_NUMBER",
"ORGANISATION_NAME",
"DEPARTMENT_NAME",
"SUB_BUILDING_NAME",
"BUILDING_NAME",
"BUILDING_NUMBER",
"DEPENDENT_THOROUGHFARE_NAME",
"THOROUGHFARE_NAME",
).compact
.join(", ")
.titleize
end
end end
def address_line2 def address_line2
@ -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",

36
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,15 +37,37 @@ describe UprnClient do
end end
end end
context "when results empty" do context "when DPA results empty" do
before do context "and LPI result is present" do
stub_api_request(body: {}.to_json) let(:valid_lpi_response) do
{ results: [{ LPI: { postcode_locator: "LPI postcode", LPI_KEY: "123" } }] }.to_json
end
client.call 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 end
it "returns error" do context "and LPI results empty" do
expect(client.error).to eq("UPRN is not recognised. Check the number, or enter the address") before do
stub_api_request(body: {}.to_json)
client.call
end
it "returns error" do
expect(client.error).to eq("UPRN is not recognised. Check the number, or enter the address")
end
end end
end end

133
spec/services/uprn_data_presenter_spec.rb

@ -1,66 +1,109 @@
require "rails_helper" require "rails_helper"
describe UprnDataPresenter do describe UprnDataPresenter do
let(:data) do
JSON.parse(
'{
"UPRN": "UPRN",
"UDPRN": "UDPRN",
"ADDRESS": "full address",
"SUB_BUILDING_NAME": "0",
"BUILDING_NAME": "building name",
"THOROUGHFARE_NAME": "thoroughfare",
"POST_TOWN": "posttown",
"POSTCODE": "postcode",
"STATUS": "APPROVED",
"DOUBLE_DEPENDENT_LOCALITY": "double dependent locality",
"DEPENDENT_LOCALITY": "dependent locality",
"CLASSIFICATION_CODE": "classification code",
"LOCAL_CUSTODIAN_CODE_DESCRIPTION": "LONDON BOROUGH OF HARINGEY",
"BLPU_STATE_CODE": "2",
"BLPU_STATE_CODE_DESCRIPTION": "In use",
"LAST_UPDATE_DATE": "31/07/2020",
"ENTRY_DATE": "30/01/2015",
"BLPU_STATE_DATE": "30/01/2015",
"LANGUAGE": "EN",
"MATCH_DESCRIPTION": "EXACT"
}',
)
end
let(:presenter) { described_class.new(data) } let(:presenter) { described_class.new(data) }
describe "#postcode" do describe "DPA data" do
it "returns postcode" do let(:data) do
expect(presenter.postcode).to eq("postcode") JSON.parse(
'{
"UPRN": "UPRN",
"UDPRN": "UDPRN",
"ADDRESS": "full address",
"SUB_BUILDING_NAME": "0",
"BUILDING_NAME": "building name",
"THOROUGHFARE_NAME": "thoroughfare",
"POST_TOWN": "posttown",
"POSTCODE": "postcode",
"STATUS": "APPROVED",
"DOUBLE_DEPENDENT_LOCALITY": "double dependent locality",
"DEPENDENT_LOCALITY": "dependent locality",
"CLASSIFICATION_CODE": "classification code",
"LOCAL_CUSTODIAN_CODE_DESCRIPTION": "LONDON BOROUGH OF HARINGEY",
"BLPU_STATE_CODE": "2",
"BLPU_STATE_CODE_DESCRIPTION": "In use",
"LAST_UPDATE_DATE": "31/07/2020",
"ENTRY_DATE": "30/01/2015",
"BLPU_STATE_DATE": "30/01/2015",
"LANGUAGE": "EN",
"MATCH_DESCRIPTION": "EXACT"
}',
)
end end
end
describe "#address_line1" do describe "#postcode" do
it "returns address_line1" do it "returns postcode" do
expect(presenter.address_line1).to eq("0, Building Name, Thoroughfare") expect(presenter.postcode).to eq("postcode")
end
end end
end
describe "#address_line2" do describe "#address_line1" do
it "returns address_line2" do it "returns address_line1" do
expect(presenter.address_line2).to eq("Double Dependent Locality, Dependent Locality") expect(presenter.address_line1).to eq("0, Building Name, Thoroughfare")
end
end end
end
describe "#town_or_city" do describe "#address_line2" do
it "returns town_or_city" do it "returns address_line2" do
expect(presenter.town_or_city).to eq("Posttown") expect(presenter.address_line2).to eq("Double Dependent Locality, Dependent Locality")
end
end
describe "#town_or_city" do
it "returns town_or_city" do
expect(presenter.town_or_city).to eq("Posttown")
end
end
context "when address_line2 fields are missing" do
let(:data) { {} }
describe "#address_line2" do
it "returns nil" do
expect(presenter.address_line2).to be_nil
end
end
end end
end end
context "when address_line2 fields are missing" do describe "LPI data" do
let(:data) { {} } 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 describe "#address_line2" do
it "returns nil" do it "returns address_line2" do
expect(presenter.address_line2).to be_nil expect(presenter.address_line2).to be_nil
end end
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 end

Loading…
Cancel
Save