Browse Source

location_admin_district for location

pull/769/head
JG 3 years ago
parent
commit
32b0248601
  1. 1
      app/models/location.rb
  2. 17
      app/services/postcode_service.rb
  3. 2
      app/views/locations/index.html.erb
  4. 2
      app/views/schemes/check_answers.html.erb
  5. 4
      db/schema.rb
  6. 4
      spec/features/schemes_spec.rb
  7. 2
      spec/request_helper.rb

1
app/models/location.rb

@ -50,5 +50,6 @@ private
def infer_la!
self.location_code = PIO.infer_la(postcode)
self.location_admin_district = PIO.infer_admin_district(postcode)
end
end

17
app/services/postcode_service.rb

@ -20,6 +20,23 @@ class PostcodeService
end
end
def infer_admin_district(postcode)
# Avoid network calls when postcode is invalid
return unless postcode.match(POSTCODE_REGEXP)
postcode_lookup = nil
begin
# URI encoding only supports ASCII characters
ascii_postcode = self.class.clean(postcode)
Timeout.timeout(5) { postcode_lookup = @pio.lookup(ascii_postcode) }
rescue Timeout::Error
Rails.logger.warn("Postcodes.io lookup timed out")
end
if postcode_lookup && postcode_lookup.info.present?
postcode_lookup.admin_district
end
end
def self.clean(postcode)
postcode.encode("ASCII", "UTF-8", invalid: :replace, undef: :replace, replace: "").delete(" ").upcase
end

2
app/views/locations/index.html.erb

@ -46,7 +46,7 @@
<% row.cell(text: location.units) %>
<% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>")) %>
<% row.cell(text: location.mobility_type) %>
<% row.cell(text: location.location_code) %>
<% row.cell(text: location.location_admin_district) %>
<% end %>
<% end %>
<% end %>

2
app/views/schemes/check_answers.html.erb

@ -106,7 +106,7 @@
<% row.cell(text: location.units) %>
<% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>")) %>
<% row.cell(text: location.mobility_type) %>
<% row.cell(text: location.location_code) %>
<% row.cell(text: location.location_admin_district) %>
<% end %>
<% end %>
<% end %>

4
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_07_20_111635) do
ActiveRecord::Schema[7.0].define(version: 2022_07_22_083835) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -250,6 +250,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_20_111635) do
t.integer "old_visible_id"
t.string "mobility_type"
t.datetime "startdate", precision: nil
t.string "location_admin_district"
t.index ["old_id"], name: "index_locations_on_old_id", unique: true
t.index ["scheme_id"], name: "index_locations_on_scheme_id"
end
@ -320,6 +321,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_20_111635) do
t.string "old_id"
t.integer "old_visible_id"
t.integer "total_units"
t.boolean "confirmed"
t.index ["managing_organisation_id"], name: "index_schemes_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id"
end

4
spec/features/schemes_spec.rb

@ -199,7 +199,7 @@ RSpec.describe "Schemes scheme Features" do
expect(page).to have_content(location.units)
expect(page).to have_content(location.type_of_unit)
expect(page).to have_content(location.mobility_type)
expect(page).to have_content(location.location_code)
expect(page).to have_content(location.location_admin_district)
end
end
end
@ -389,7 +389,7 @@ RSpec.describe "Schemes scheme Features" do
expect(page).to have_content "Self-contained house"
expect(page).to have_content "None"
expect(page).to have_content "Local authority"
expect(page).to have_content "E09000033"
expect(page).to have_content "Westminster"
end
it "displays information about another location" do

2
spec/request_helper.rb

@ -7,7 +7,7 @@ module RequestHelper
.to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {})
WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA11AA")
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\",\"nuts\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {})
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {})
WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/email/)
.to_return(status: 200, body: "", headers: {})

Loading…
Cancel
Save