Browse Source

Ensure models set attributes from OSM tags (geocoding.properties)

tags/0.3.6^2
Bèr Kessels 4 months ago
parent
commit
d713149e26
2 changed files with 28 additions and 3 deletions
  1. 7
    1
      app/models/base.rb
  2. 21
    2
      test/models/base_test.rb

+ 7
- 1
app/models/base.rb View File

@@ -16,7 +16,13 @@ module Hours

def self.from_geojson_feature(feature)
attrs = feature.properties.fetch('geocoding', {})
new(attrs, true)
tags = attrs.fetch('properties', [])

# Turn # [{ key => 'name', value => 'someval' }] into
# { 'name' => someval }
extra_props = Hash[tags.map { |pair| [pair['key'], pair['value']] }]

new(extra_props.merge(attrs), true)
end
end
end

+ 21
- 2
test/models/base_test.rb View File

@@ -5,7 +5,7 @@ require_relative Hours.base_path.join 'app/models/base.rb'

describe Hours::Models::Base do
class MockModel < Hours::Models::Base
attr_accessor :name
attr_accessor :name, :opening_hours, :website
end

describe '.initialize' do
@@ -24,7 +24,16 @@ describe Hours::Models::Base do
describe '.from_geojson_feature' do
let(:feature) do
OpenStruct.new(
properties: { 'geocoding' => { 'name' => 'Hi', 'fakemethod' => 'val' } }
properties: { 'geocoding' => {
'name' => 'Hi',
'fakemethod' => 'val',
'website' => 'example.org',
'properties' => [
{ 'key' => 'opening_hours', 'value' => '24/7' },
{ 'key' => 'shop:number', 'value' => '1337' },
{ 'key' => 'website', 'value' => 'example.com' }
]
} }
)
end

@@ -33,5 +42,15 @@ describe Hours::Models::Base do
# Does not raise NoMethodError
assert_equal 'Hi', subject.name
end

it 'sets attributes from geocoding.properties' do
subject = MockModel.from_geojson_feature(feature)
assert_equal '24/7', subject.opening_hours
end

it 'prefers main properties over geocoding.properties' do
subject = MockModel.from_geojson_feature(feature)
assert_equal 'example.org', subject.website
end
end
end

Loading…
Cancel
Save