Browse Source

Use postcodes io gem

pull/165/head
Kat 4 years ago
parent
commit
0f0b90f445
  1. 1
      Gemfile
  2. 4
      Gemfile.lock
  3. 12
      app/models/case_log.rb
  4. 4
      spec/features/form/page_routing_spec.rb
  5. 4
      spec/models/case_log_spec.rb
  6. 4
      spec/request_helper.rb

1
Gemfile

@ -37,6 +37,7 @@ gem "devise", github: "ghiculescu/devise", branch: "error-code-422"
# UK postcode parsing and validation # UK postcode parsing and validation
gem "uk_postcode" gem "uk_postcode"
# Use Ruby objects to build reusable markup. A React inspired evolution of the presenter pattern # Use Ruby objects to build reusable markup. A React inspired evolution of the presenter pattern
gem "postcodes_io"
gem "view_component" gem "view_component"
group :development, :test do group :development, :test do

4
Gemfile.lock

@ -171,6 +171,7 @@ GEM
dotenv (= 2.7.6) dotenv (= 2.7.6)
railties (>= 3.2) railties (>= 3.2)
erubi (1.10.0) erubi (1.10.0)
excon (0.89.0)
factory_bot (6.2.0) factory_bot (6.2.0)
activesupport (>= 5.0.0) activesupport (>= 5.0.0)
factory_bot_rails (6.2.0) factory_bot_rails (6.2.0)
@ -255,6 +256,8 @@ GEM
parser (3.0.3.1) parser (3.0.3.1)
ast (~> 2.4.1) ast (~> 2.4.1)
pg (1.2.3) pg (1.2.3)
postcodes_io (0.4.0)
excon (~> 0.39)
pry (0.13.1) pry (0.13.1)
coderay (~> 1.1) coderay (~> 1.1)
method_source (~> 1.0) method_source (~> 1.0)
@ -432,6 +435,7 @@ DEPENDENCIES
listen (~> 3.3) listen (~> 3.3)
overcommit (>= 0.37.0) overcommit (>= 0.37.0)
pg (~> 1.1) pg (~> 1.1)
postcodes_io
pry-byebug pry-byebug
puma (~> 5.0) puma (~> 5.0)
rack-mini-profiler (~> 2.0) rack-mini-profiler (~> 2.0)

12
app/models/case_log.rb

@ -1,6 +1,7 @@
require "uri" require "uri"
require "net/http" require "net/http"
require "json" require "json"
require "postcodes_io"
class CaseLogValidator < ActiveModel::Validator class CaseLogValidator < ActiveModel::Validator
# Validations methods need to be called 'validate_' to run on model save # Validations methods need to be called 'validate_' to run on model save
@ -163,6 +164,8 @@ class CaseLog < ApplicationRecord
private private
PIO = Postcodes::IO.new
def update_status! def update_status!
self.status = if all_fields_completed? && errors.empty? self.status = if all_fields_completed? && errors.empty?
"completed" "completed"
@ -202,13 +205,12 @@ private
end end
def get_la(postcode) def get_la(postcode)
uri = URI("https://api.os.uk/search/places/v1/postcode?key=#{ENV['OS_PLACES_API_KEY']}&postcode=#{postcode}&dataset=LPI") postcode_lookup = PIO.lookup(postcode)
res = Net::HTTP.get_response(uri) unless postcode_lookup.info.nil?
response_body = JSON.parse(res.body)
if res.is_a?(Net::HTTPSuccess) && (response_body["header"]["totalresults"]).to_i.positive?
self.is_la_inferred = true self.is_la_inferred = true
response_body["results"][0]["LPI"]["ADMINISTRATIVE_AREA"].downcase.capitalize return postcode_lookup.admin_district
end end
self.la = nil
self.is_la_inferred = false self.is_la_inferred = false
end end

4
spec/features/form/page_routing_spec.rb

@ -63,8 +63,8 @@ RSpec.describe "Form Page Routing" do
end end
it "does not show question if the answer could be inferred" do it "does not show question if the answer could be inferred" do
stub_request(:get, /api.os.uk/) stub_request(:get, /api.postcodes.io/)
.to_return(status: 200, body: "{\"header\": {\"totalresults\": \"1\"}, \"results\": [{\"LPI\": {\"ADMINISTRATIVE_AREA\": \"MANCHESTER\"}}]}", headers: {}) .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\"}}", headers: {})
visit("/case-logs/#{id}/property-postcode") visit("/case-logs/#{id}/property-postcode")
fill_in("case-log-property-postcode-field", with: "P0 5ST") fill_in("case-log-property-postcode-field", with: "P0 5ST")

4
spec/models/case_log_spec.rb

@ -1023,8 +1023,8 @@ RSpec.describe Form, type: :model do
context "addresses" do context "addresses" do
before do before do
stub_request(:get, /api.os.uk/) stub_request(:get, /api.postcodes.io/)
.to_return(status: 200, body: "{\"header\": {\"totalresults\": \"1\"}, \"results\": [{\"LPI\": {\"ADMINISTRATIVE_AREA\": \"MANCHESTER\"}}]}", headers: {}) .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\"}}", headers: {})
end end
let!(:address_case_log) do let!(:address_case_log) do

4
spec/request_helper.rb

@ -3,7 +3,7 @@ require "webmock/rspec"
module RequestHelper module RequestHelper
def self.stub_http_requests def self.stub_http_requests
WebMock.disable_net_connect!(allow_localhost: true) WebMock.disable_net_connect!(allow_localhost: true)
WebMock.stub_request(:get, /api.os.uk/) WebMock.stub_request(:get, /api.postcodes.io/)
.to_return(status: 200, body: "{\"header\": {\"totalresults\": \"0\"}}", headers: {}) .to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {})
end end
end end

Loading…
Cancel
Save