natdeanlewissoftwire
11 months ago
5 changed files with 143 additions and 0 deletions
@ -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 |
@ -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 |
@ -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 |
@ -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 %> |
Loading…
Reference in new issue