From cd12f6da8be4ef4296caf096a16a918ce2344d50 Mon Sep 17 00:00:00 2001 From: Jack S Date: Wed, 15 Mar 2023 11:05:56 +0000 Subject: [PATCH] Add UPRN Presenter --- app/services/uprn_data_presenter.rb | 41 ++++++++++++++ spec/services/uprn_data_presenter_spec.rb | 66 +++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 app/services/uprn_data_presenter.rb create mode 100644 spec/services/uprn_data_presenter_spec.rb diff --git a/app/services/uprn_data_presenter.rb b/app/services/uprn_data_presenter.rb new file mode 100644 index 000000000..5602cbd28 --- /dev/null +++ b/app/services/uprn_data_presenter.rb @@ -0,0 +1,41 @@ +require "net/http" + +class UprnDataPresenter + attr_reader :data + + def initialize(data) + @data = data + end + + def postcode + data["POSTCODE"] + end + + def address_line1 + 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 + + def address_line2 + data.values_at( + "DOUBLE_DEPENDENT_LOCALITY", "DEPENDENT_LOCALITY" + ).compact + .join(", ") + .titleize + .presence + end + + def town_or_city + data["POST_TOWN"].titleize + end +end diff --git a/spec/services/uprn_data_presenter_spec.rb b/spec/services/uprn_data_presenter_spec.rb new file mode 100644 index 000000000..16ae9318f --- /dev/null +++ b/spec/services/uprn_data_presenter_spec.rb @@ -0,0 +1,66 @@ +require "rails_helper" + +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) } + + 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("0, Building Name, Thoroughfare") + end + end + + describe "#address_line2" do + it "returns address_line2" do + 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