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
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

4
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)

12
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

4
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")

4
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

4
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

Loading…
Cancel
Save