12 changed files with 290 additions and 2 deletions
@ -0,0 +1,18 @@ |
|||||||
|
class Form::Lettings::Pages::AddressMatcher < ::Form::Page |
||||||
|
def initialize(id, hsh, subsection) |
||||||
|
super |
||||||
|
@id = "address_matcher" |
||||||
|
@header = "Enter address details" |
||||||
|
@depends_on = [ |
||||||
|
{ "is_supported_housing?" => false, "uprn_known" => 0 }, |
||||||
|
{ "is_supported_housing?" => false, "uprn_confirmed" => 0 }, |
||||||
|
] |
||||||
|
end |
||||||
|
|
||||||
|
def questions |
||||||
|
@questions ||= [ |
||||||
|
Form::Lettings::Questions::AddressLine1ForAddressMatcher.new(nil, nil, self), |
||||||
|
Form::Lettings::Questions::PostcodeForAddressMatcher.new(nil, nil, self), |
||||||
|
] |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,17 @@ |
|||||||
|
class Form::Lettings::Pages::AddressSelection < ::Form::Page |
||||||
|
def initialize(id, hsh, subsection) |
||||||
|
super |
||||||
|
@id = "address_selection" |
||||||
|
@header = "We found some addresses that might be this property" |
||||||
|
end |
||||||
|
|
||||||
|
def questions |
||||||
|
@questions ||= [ |
||||||
|
Form::Lettings::Questions::AddressSelection.new(nil, nil, self), |
||||||
|
] |
||||||
|
end |
||||||
|
|
||||||
|
def routed_to?(log, _current_user = nil) |
||||||
|
log.uprn_known == 0 && log.address_line1.present? && log.postcode_full.present? |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,14 @@ |
|||||||
|
class Form::Lettings::Questions::AddressLine1ForAddressMatcher < ::Form::Question |
||||||
|
def initialize(id, hsh, page) |
||||||
|
super |
||||||
|
@id = "address_line1" |
||||||
|
@header = "Address line 1" |
||||||
|
@error_label = "Address line 1" |
||||||
|
@type = "text" |
||||||
|
@plain_label = true |
||||||
|
@check_answer_label = "Address line 1" |
||||||
|
@disable_clearing_if_not_routed_or_dynamic_answer_options = true |
||||||
|
@question_number = 12 |
||||||
|
@hide_question_number_on_page = true |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,60 @@ |
|||||||
|
class Form::Lettings::Questions::AddressSelection < ::Form::Question |
||||||
|
def initialize(id, hsh, page) |
||||||
|
super |
||||||
|
@id = "address_selection" |
||||||
|
@header = "Select the correct address" |
||||||
|
@type = "radio" |
||||||
|
@check_answer_label = "Select the correct address" |
||||||
|
@disable_clearing_if_not_routed_or_dynamic_answer_options = true # have just added this, check if it works! |
||||||
|
end |
||||||
|
|
||||||
|
def answer_options(log = nil, user = nil) |
||||||
|
answer_opts = { |
||||||
|
# "0" => { "value" => "address 0" }, |
||||||
|
# "1" => { "value" => "address 1" }, |
||||||
|
# "2" => { "value" => "address 2" }, |
||||||
|
# "3" => { "value" => "address 3" }, |
||||||
|
# "4" => { "value" => "address 4" }, |
||||||
|
# "5" => { "value" => "address 5" }, |
||||||
|
# "6" => { "value" => "address 6" }, |
||||||
|
# "7" => { "value" => "address 7" }, |
||||||
|
# "8" => { "value" => "address 8" }, |
||||||
|
# "9" => { "value" => "address 9" }, |
||||||
|
} |
||||||
|
return answer_opts unless ActiveRecord::Base.connected? |
||||||
|
return answer_opts unless log |
||||||
|
return answer_opts unless log.address_options |
||||||
|
|
||||||
|
values = [] |
||||||
|
log.address_options.each do |option| |
||||||
|
values.append(option) |
||||||
|
end |
||||||
|
|
||||||
|
{ |
||||||
|
"0" => { "value" => values[0] }, |
||||||
|
"1" => { "value" => values[1] }, |
||||||
|
"2" => { "value" => values[2] }, |
||||||
|
"3" => { "value" => values[3] }, |
||||||
|
"4" => { "value" => values[4] }, |
||||||
|
"5" => { "value" => values[5] }, |
||||||
|
"6" => { "value" => values[6] }, |
||||||
|
"7" => { "value" => values[7] }, |
||||||
|
"8" => { "value" => values[8] }, |
||||||
|
"9" => { "value" => values[9] }, |
||||||
|
}.freeze |
||||||
|
end |
||||||
|
|
||||||
|
def displayed_answer_options(log, user = nil) |
||||||
|
answer_options(log, user) |
||||||
|
end |
||||||
|
|
||||||
|
def hidden_in_check_answers?(log, _current_user = nil) |
||||||
|
log.uprn_known == 1 || log.uprn_confirmed == 1 |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def selected_answer_option_is_derived?(_log) |
||||||
|
true |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,25 @@ |
|||||||
|
class Form::Lettings::Questions::PostcodeForAddressMatcher < ::Form::Question |
||||||
|
def initialize(id, hsh, page) |
||||||
|
super |
||||||
|
@id = "postcode_full" |
||||||
|
@header = "Postcode" |
||||||
|
@type = "text" |
||||||
|
@width = 5 |
||||||
|
@inferred_check_answers_value = [{ |
||||||
|
"condition" => { |
||||||
|
"pcodenk" => 1, |
||||||
|
}, |
||||||
|
"value" => "Not known", |
||||||
|
}] |
||||||
|
@inferred_answers = { |
||||||
|
"la" => { |
||||||
|
"is_la_inferred" => true, |
||||||
|
}, |
||||||
|
} |
||||||
|
@plain_label = true |
||||||
|
@check_answer_label = "Postcode" |
||||||
|
@disable_clearing_if_not_routed_or_dynamic_answer_options = true |
||||||
|
@question_number = 12 |
||||||
|
@hide_question_number_on_page = true |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,52 @@ |
|||||||
|
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, |
||||||
|
maxresults: 10, |
||||||
|
} |
||||||
|
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,41 @@ |
|||||||
|
require "net/http" |
||||||
|
|
||||||
|
class AddressDataPresenter |
||||||
|
attr_reader :data |
||||||
|
|
||||||
|
def initialize(data) |
||||||
|
@data = data |
||||||
|
end |
||||||
|
|
||||||
|
def uprn |
||||||
|
data["UPRN"] |
||||||
|
end |
||||||
|
|
||||||
|
def address_line1 |
||||||
|
[data["BUILDING_NUMBER"], data["BUILDING_NAME"], data["THOROUGHFARE_NAME"]].compact.join(", ") |
||||||
|
end |
||||||
|
|
||||||
|
def address_line2 |
||||||
|
data["DEPENDENT_LOCALITY"] |
||||||
|
end |
||||||
|
|
||||||
|
def town_or_city |
||||||
|
data["POST_TOWN"] |
||||||
|
end |
||||||
|
|
||||||
|
def postcode |
||||||
|
data["POSTCODE"] |
||||||
|
end |
||||||
|
|
||||||
|
def address |
||||||
|
data["ADDRESS"] |
||||||
|
end |
||||||
|
|
||||||
|
# def match |
||||||
|
# data["MATCH"] |
||||||
|
# end |
||||||
|
# |
||||||
|
# def match_description |
||||||
|
# data["MATCH_DESCRIPTION"] |
||||||
|
# end |
||||||
|
end |
||||||
Loading…
Reference in new issue