From 0f0b90f4455ccc15bb4d497f5a367bb4ef5e5f5d Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 14 Dec 2021 10:41:47 +0000 Subject: [PATCH] Use postcodes io gem --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/models/case_log.rb | 12 +++++++----- spec/features/form/page_routing_spec.rb | 4 ++-- spec/models/case_log_spec.rb | 4 ++-- spec/request_helper.rb | 4 ++-- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index c707c3ef5..2e94d7e75 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,7 @@ gem "devise", github: "ghiculescu/devise", branch: "error-code-422" # UK postcode parsing and validation gem "uk_postcode" # Use Ruby objects to build reusable markup. A React inspired evolution of the presenter pattern +gem "postcodes_io" gem "view_component" group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index 6ef45d384..cad5c5eb5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -171,6 +171,7 @@ GEM dotenv (= 2.7.6) railties (>= 3.2) erubi (1.10.0) + excon (0.89.0) factory_bot (6.2.0) activesupport (>= 5.0.0) factory_bot_rails (6.2.0) @@ -255,6 +256,8 @@ GEM parser (3.0.3.1) ast (~> 2.4.1) pg (1.2.3) + postcodes_io (0.4.0) + excon (~> 0.39) pry (0.13.1) coderay (~> 1.1) method_source (~> 1.0) @@ -432,6 +435,7 @@ DEPENDENCIES listen (~> 3.3) overcommit (>= 0.37.0) pg (~> 1.1) + postcodes_io pry-byebug puma (~> 5.0) rack-mini-profiler (~> 2.0) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 4df7b120e..e30216f32 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,6 +1,7 @@ require "uri" require "net/http" require "json" +require "postcodes_io" class CaseLogValidator < ActiveModel::Validator # Validations methods need to be called 'validate_' to run on model save @@ -163,6 +164,8 @@ class CaseLog < ApplicationRecord private + PIO = Postcodes::IO.new + def update_status! self.status = if all_fields_completed? && errors.empty? "completed" @@ -202,13 +205,12 @@ private end def get_la(postcode) - uri = URI("https://api.os.uk/search/places/v1/postcode?key=#{ENV['OS_PLACES_API_KEY']}&postcode=#{postcode}&dataset=LPI") - res = Net::HTTP.get_response(uri) - response_body = JSON.parse(res.body) - if res.is_a?(Net::HTTPSuccess) && (response_body["header"]["totalresults"]).to_i.positive? + postcode_lookup = PIO.lookup(postcode) + unless postcode_lookup.info.nil? self.is_la_inferred = true - response_body["results"][0]["LPI"]["ADMINISTRATIVE_AREA"].downcase.capitalize + return postcode_lookup.admin_district end + self.la = nil self.is_la_inferred = false end diff --git a/spec/features/form/page_routing_spec.rb b/spec/features/form/page_routing_spec.rb index 2d566f231..063583d10 100644 --- a/spec/features/form/page_routing_spec.rb +++ b/spec/features/form/page_routing_spec.rb @@ -63,8 +63,8 @@ RSpec.describe "Form Page Routing" do end it "does not show question if the answer could be inferred" do - stub_request(:get, /api.os.uk/) - .to_return(status: 200, body: "{\"header\": {\"totalresults\": \"1\"}, \"results\": [{\"LPI\": {\"ADMINISTRATIVE_AREA\": \"MANCHESTER\"}}]}", headers: {}) + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\"}}", headers: {}) visit("/case-logs/#{id}/property-postcode") fill_in("case-log-property-postcode-field", with: "P0 5ST") diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 084a5d951..522e1452a 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1023,8 +1023,8 @@ RSpec.describe Form, type: :model do context "addresses" do before do - stub_request(:get, /api.os.uk/) - .to_return(status: 200, body: "{\"header\": {\"totalresults\": \"1\"}, \"results\": [{\"LPI\": {\"ADMINISTRATIVE_AREA\": \"MANCHESTER\"}}]}", headers: {}) + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\"}}", headers: {}) end let!(:address_case_log) do diff --git a/spec/request_helper.rb b/spec/request_helper.rb index 2ab6169bf..c939e7afa 100644 --- a/spec/request_helper.rb +++ b/spec/request_helper.rb @@ -3,7 +3,7 @@ require "webmock/rspec" module RequestHelper def self.stub_http_requests WebMock.disable_net_connect!(allow_localhost: true) - WebMock.stub_request(:get, /api.os.uk/) - .to_return(status: 200, body: "{\"header\": {\"totalresults\": \"0\"}}", headers: {}) + WebMock.stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {}) end end