Browse Source

REfactor Place aggregate to use the new address table for geocoding

feature/region-slug-gone
Bèr Kessels 5 months ago
parent
commit
1f93f3f993

+ 13
- 16
app/aggregates/place.rb View File

@@ -24,7 +24,6 @@ module Hours
end

attr_writer :address_builder, :place_id_builder, :region_builder
attr_reader :place_id, :region_slug

# These apply methods are the hook that this aggregate uses to update
# its internal state from events.
@@ -36,10 +35,9 @@ module Hours
raise DuplicateError, "Place #{id.inspect} already exists" if added?

@payload = Hashie.symbolize_keys(payload)
@place_id = place_id_builder.id
@region_slug = region_builder.region_slug
fill_payload

apply_event(PlaceAdded, aggregate_id: id, body: event_body)
apply_event(PlaceAdded, aggregate_id: id, body: @payload)
self
end

@@ -49,21 +47,20 @@ module Hours
@aggregate_id
end

def event_body
@payload.merge(
properties: properties.merge(
place_id: place_id,
region_slug: region_slug,
'addr:city': address_builder.city,
'addr:postcode': address_builder.postcode,
'addr:housenumber': address_builder.number,
'addr:street': address_builder.street
)
def fill_payload
merge_properties(place_id: place_id_builder.id)
merge_properties(
'addr:city': address_builder.city,
'addr:postcode': address_builder.postcode,
'addr:housenumber': address_builder.number,
'addr:street': address_builder.street
)
merge_properties(region_slug: region_builder.region_slug)
end

def properties
@payload.fetch(:properties, {})
def merge_properties(append_properties)
@payload[:properties] ||= {}
@payload[:properties].merge!(append_properties)
end

def place_id_builder

+ 1
- 23
app/aggregates/place/region.rb View File

@@ -1,14 +1,11 @@
# frozen_string_literal: true

require 'offline_geocoder'
require 'babosa'

module Hours
module Aggregates
# Generates a normalized, distinguishable region string.
class Region
attr_writer :offline_geocoder

def initialize(geojson)
@geojson = Hashie.symbolize_keys(geojson)
end
@@ -18,26 +15,7 @@ module Hours
end

def city
@geojson.fetch(:properties, {})
.fetch(:'addr:city', region_from_geom)
end

private

def lat
@geojson.fetch(:geometry, {}).fetch(:coordinates)[1]
end

def lon
@geojson.fetch(:geometry, {}).fetch(:coordinates)[0]
end

def region_from_geom
offline_geocoder.search(lat, lon)[:name]
end

def offline_geocoder
@offline_geocoder ||= OfflineGeocoder.new
@geojson.fetch(:properties, {}).fetch(:'addr:city', '') || ''
end
end
end

+ 1
- 0
app/projections/query.rb View File

@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'pagy'
require 'offline_geocoder'

module Hours
module Projections

+ 3
- 35
test/aggregates/place/region_test.rb View File

@@ -4,8 +4,6 @@ require 'test_helper'
require_relative Hours.base_path.join 'app/aggregates/place/region.rb'

describe Hours::Aggregates::Region do
let(:offline_geocoder) { Minitest::Mock.new }

subject do
Hours::Aggregates::Region.new(geojson)
end
@@ -38,39 +36,9 @@ describe Hours::Aggregates::Region do
end

describe 'without addr:city' do
let(:geojson) { json_fixtures('input/marienburg_bicycle.json') }

let(:lat) { geojson[:geometry][:coordinates][1] }
let(:lon) { geojson[:geometry][:coordinates][0] }

let(:bayswater) do
{
lat: 51.51116,
lon: -0.18426,
name: 'Bayswater',
admin1: 'England',
admin2: 'Greater London',
cc: 'GB',
country: 'United Kingdom'
}
end

it 'generates from offline_geocoder' do
assert_nil geojson[:properties][:'addr:city']
subject.offline_geocoder = offline_geocoder
offline_geocoder.expect(:search, bayswater, [lat, lon])

assert_equal 'bayswater', subject.region_slug
assert_mock offline_geocoder
end

it 'sets the city from offline_geocoder' do
assert_nil geojson[:properties][:'addr:city']
subject.offline_geocoder = offline_geocoder
offline_geocoder.expect(:search, bayswater, [lat, lon])

assert_equal 'Bayswater', subject.city
assert_mock offline_geocoder
let(:geojson) { json_fixtures('input/addressless.json') }
it 'is empty' do
assert_empty subject.region_slug
end
end
end

+ 9
- 4
test/aggregates/place_test.rb View File

@@ -39,13 +39,13 @@ describe Hours::Aggregates::Place do

it 'builds a place_id' do
place = subject.add(payload)
assert_equal place_id, place.place_id
assert_equal place_id, body_properties(place)['place_id']
assert_mock place_id_builder
end

it 'builds a region_slug' do
place = subject.add(payload)
assert_equal region_slug, place.region_slug
assert_equal region_slug, body_properties(place)['region_slug']
assert_mock region_builder
end

@@ -55,10 +55,15 @@ describe Hours::Aggregates::Place do
it 'sets the addr:city from address_builder' do
place = subject.add(payload)
# The capitalized versions are what is mocked
assert_equal 'City',
place.changes.first.body['properties']['addr:city']
assert_equal 'City', body_properties(place)['addr:city']
assert_mock address_builder
end
end
end

private

def body_properties(place)
place.changes.first&.body&.fetch('properties', {})
end
end

+ 4
- 0
test/fixtures/address_sample.csv View File

@@ -133,3 +133,7 @@ b4b2a739fca5c00e,26,Stationsplein,,Nijmegen,,Gelderland,6512AB,SRID=4326;POINT(5
b4ac426074553d7c,2,Op het Toneel,,Arnhem,,Gelderland,6836ND,SRID=4326;POINT(5.8939416 51.9418593)
adb19e7d67038baa,3,Op het Toneel,,Arnhem,,Gelderland,6836ND,SRID=4326;POINT(5.8941034 51.9415266)
2b305bcfe358a429,4,Op het Toneel,,Arnhem,,Gelderland,6836ND,SRID=4326;POINT(5.8940051 51.9416768)
ef89c57948db291d,3,Arsenaalgas,,Nijmegen,,Gelderland,6511PB,SRID=4326;POINT(5.8664921 51.8448285)
847f8daacc86291f,7A,Arsenaalgas,,Nijmegen,,Gelderland,6511PB,SRID=4326;POINT(5.8661005 51.8446562)
a2fc8c0a642409e8,7,Arsenaalgas,,Nijmegen,,Gelderland,6511PB,SRID=4326;POINT(5.8658718 51.8447315)
eaa13cc029788f00,8,Arsenaalgas,,Nijmegen,,Gelderland,6511PE,SRID=4326;POINT(5.865782 51.8447778)

+ 3
- 1
test/integration/api/view_places_test.rb View File

@@ -102,7 +102,9 @@ describe 'api view places' do
describe 'GET places/:place_id' do
it 'forwards client with 303 to place by UUID' do
place = Hours::Aggregates::Place.new(hm_id, [])
place_id = place.add(json_fixtures('input/hm_burchtstraat.json')).place_id
# Extract the place_id from the place aggregate
changes = place.add(json_fixtures('input/hm_burchtstraat.json')).changes
place_id = changes.first.body['properties']['place_id']

get "/places/#{place_id}"
assert_status(303)

Loading…
Cancel
Save