Browse Source

feat: mvp

CLDC-3233-address-uprn-lookup-spike
natdeanlewissoftwire 11 months ago
parent
commit
7bd93295f9
  1. 27
      app/controllers/uprn_address_spike_controller.rb
  2. 51
      app/services/address_client.rb
  3. 25
      app/services/address_data_presenter.rb
  4. 39
      app/views/content/uprn_address_spike.html.erb
  5. 1
      config/routes.rb

27
app/controllers/uprn_address_spike_controller.rb

@ -0,0 +1,27 @@
class UprnAddressSpikeController < ApplicationController
def show
if params[:uprn] || params[:address]
if params[:uprn]
uprn = params[:uprn]
service = UprnClient.new(uprn)
service.call
if service.error.present?
@error = "no match"
else
@address_returned = UprnDataPresenter.new(service.result)
end
elsif params[:address]
address = params[:address]
service = AddressClient.new(address)
service.call
if service.error.present?
@error = "no match"
else
@addresses_returned = service.result&.map { |r| AddressDataPresenter.new(r) }
end
end
end
render "content/uprn_address_spike"
end
end

51
app/services/address_client.rb

@ -0,0 +1,51 @@
require "net/http"
class AddressClient
attr_reader :address
attr_accessor :error
ADDRESS = "api.os.uk".freeze
PATH = "/search/places/v1/find".freeze
def initialize(address)
@address = address
end
def call
unless response.is_a?(Net::HTTPSuccess) && result.present?
@error = "Address is not recognised. Check the address, or enter the UPRN"
end
rescue JSON::ParserError
@error = "Address is not recognised. Check the address, or enter the UPRN"
end
def result
@result ||= JSON.parse(response.body)["results"]&.map { |address| address["DPA"] }
end
private
def http_client
client = Net::HTTP.new(ADDRESS, 443)
client.use_ssl = true
client.verify_mode = OpenSSL::SSL::VERIFY_PEER
client.max_retries = 3
client.read_timeout = 10 # seconds
client
end
def endpoint_uri
uri = URI(PATH)
params = {
query: address,
key: ENV["OS_DATA_KEY"],
matchprecision: 3,
}
uri.query = URI.encode_www_form(params)
uri.to_s
end
def response
@response ||= http_client.request_get(endpoint_uri)
end
end

25
app/services/address_data_presenter.rb

@ -0,0 +1,25 @@
require "net/http"
class AddressDataPresenter
attr_reader :data
def initialize(data)
@data = data
end
def uprn
data["UPRN"]
end
def address
data["ADDRESS"]
end
def match
data["MATCH"]
end
def match_description
data["MATCH_DESCRIPTION"]
end
end

39
app/views/content/uprn_address_spike.html.erb

@ -0,0 +1,39 @@
<form id="uprnForm">
<label for="textInput">Enter UPRN:</label>
<input type="text" name="uprn">
<button type="submit">Submit</button>
</form>
<form id="addressForm">
<label for="textInput">Or enter address:</label>
<input type="text" name="address">
<button type="submit">Submit</button>
</form>
<% if params[:uprn] %>
<h1>UPRN given:</h1>
<p><%= params[:uprn] %></p>
<% if @error.present? %>
<%= @error %>
<% elsif @address_returned.present? %>
<h1>Output:</h1>
<p><%= @address_returned.address_line1 %></p>
<p><%= @address_returned.address_line2 %></p>
<p><%= @address_returned.town_or_city %></p>
<p><%= @address_returned.postcode %></p>
<% end %>
<% elsif params[:address] %>
<h1>Address given:</h1>
<p><%= params[:address] %></p>
<% if @error.present? %>
<%= @error %>
<% elsif @addresses_returned.present? %>
<h1>Output:</h1>
<% @addresses_returned.each do |address_returned| %>
<p>Address: <%= address_returned.address %></p>
<p>UPRN: <%= address_returned.uprn %></p>
<p>Confidence: <%= address_returned.match%> (<%= address_returned.match_description %>)</p>
<br>
<% end %>
<% end %>
<% end %>

1
config/routes.rb

@ -1,5 +1,6 @@
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
Rails.application.routes.draw do
get "uprn-address-spike", to: "uprn_address_spike#show"
mount_sidekiq = -> { mount Sidekiq::Web => "/sidekiq" }
authenticate(:user, :support?.to_proc, &mount_sidekiq)

Loading…
Cancel
Save