Skip to content

Commit

Permalink
Simplify Datatoursime
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Dec 2, 2024
1 parent 022034c commit 43f5a31
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 36 deletions.
22 changes: 7 additions & 15 deletions datasources/connectors/datatourisme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@ def setup(kiba)
kiba.source(MetadataSource, @job_id, @job_id, nil, MetadataSource::Settings.from_hash({
'schema' => [
'datasources/schemas/tags/base.schema.json',
'datasources/schemas/tags/hosting.schema.json',
'datasources/schemas/tags/restaurant.schema.json',
'datasources/schemas/tags/osm.schema.json',
],
'i18n' => [
'datasources/schemas/tags/base.i18n.json',
'datasources/schemas/tags/hosting.i18n.json',
'datasources/schemas/tags/restaurant.i18n.json',
'datasources/schemas/tags/osm.i18n.json',
]
}))

DatatourismeSource.fetch("#{@settings['flow_key']}/#{@settings['key']}")
.select { |data| @source_filter.nil? || data.dig('type', 'value').start_with?(@source_filter) }
.group_by { |h| h.dig('type', 'value') }
.map do |key, data|
destination_id = "#{@job_id}-#{key.split('#').last}"
name = { 'fr' => 'Datatourisme' }

kiba.source(
self.class.source_class,
@job_id,
destination_id,
name,
self.class.source_class.const_get(:Settings).from_hash(@settings.merge({ 'destination_id' => destination_id, 'datas' => data })),
)
end
super
end
end
39 changes: 39 additions & 0 deletions datasources/schemas/tags/osm.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@
}
}
},
"information": {
"@default": {
"fr": "information"
},
"values": {
"office": {
"@default:full": {
"fr": "bureau d'information"
}
}
}
},
"natural": {
"@default": {
"fr": "nature"
Expand Down Expand Up @@ -95,6 +107,33 @@
"@default:full": {
"fr": "station services"
}
},
"church": {
"@default:full": {
"fr": "église"
}
},
"camping": {
"@default:full": {
"fr": "camping"
}
},
"picnic_site": {
"@default:full": {
"fr": "aire de pique-nique"
}
}
}
},
"religion": {
"@default": {
"fr": "religion"
},
"values": {
"christian": {
"@default:full": {
"fr": "chrétien"
}
}
}
},
Expand Down
15 changes: 14 additions & 1 deletion datasources/schemas/tags/osm.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"information"
]
},
"information": {
"enum": [
"office"
]
},
"natural": {
"enum": [
"bay",
Expand All @@ -38,7 +43,15 @@
},
"amenity": {
"enum": [
"fuel"
"fuel",
"church",
"camping",
"picnic_site"
]
},
"religion": {
"enum": [
"christian"
]
},
"shop": {
Expand Down
36 changes: 19 additions & 17 deletions datasources/sources/datatourisme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

class DatatourismeSource < Source
class Settings < Source::SourceSettings
const :key, String, name: 'key' # API key
const :flow_key, String, name: 'flow_key' # Flow key
const :destination_id, T.nilable(String), name: 'destination_id' # Destination ID
const :datas, T.nilable(T::Array[T::Hash[String, T.untyped]]), name: 'datas' # Datas
const :app_key, String
const :source_id, String
end

extend T::Generic
Expand All @@ -27,7 +25,7 @@ def self.fetch(path)
url = "https://diffuseur.datatourisme.fr/webservice/#{path}"
response = HTTP.follow.get(url)

return [url, response].inspect unless response.status.success?
raise [url, response].inspect unless response.status.success?

Set.new(JSON.parse(
decompress_gzip(response.body.to_s)
Expand All @@ -39,32 +37,36 @@ def self.decompress_gzip(data)
end

def each
if ENV['NO_DATA']
[]
else
super(@settings.datas)
end
super(ENV['NO_DATA'] ? [] : self.class.fetch("#{@settings.source_id}/#{@settings.app_key}"))
end

def map_updated_at(feat)
feat.dig('updated_at', 'value')
end

def map_source(feat)
def map_destination_id(feat)
feat.dig('type', 'value').split('#').last
end

def map_destination_id(_feat)
@settings.destination_id
end

def map_geometry(feat)
{
type: 'Point',
coordinates: [feat.dig('Longitude', 'value')&.to_f, feat.dig('Latitude', 'value')&.to_f],
}
end

TYPE = HashExcep[{
# 'Place' => {},
'Camping' => { amenity: 'camping' },
'Church' => { amenity: 'place_of_worship', religion: 'christian' },
'Restaurant' => { amenity: 'restaurant' },
'LocalTouristOffice' => { tourism: 'information', information: 'office' },
'Museum' => { tourism: 'museum' },
'PointOfView' => { tourism: 'viewpoint' },
'PicnicArea' => { amenity: 'picnic_site' },
'WineCellar' => { tourism: 'wine cellar' },
}]

def map_tags(feat)
{
'name' => {
Expand All @@ -82,15 +84,15 @@ def map_tags(feat)
'wheelchair' => [feat.dig('wheelchair', 'value')].compact,
'image' => [feat.dig('image', 'value')].compact,
'description' => [feat.dig('description', 'value')].compact,
}
}.merge(TYPE[feat['type']['value'].split('#').last])
end

def map_id(feat)
feat.dig('identifier', 'value')
end
end

# requête SPARQL pour récupérer les données de Datatourisme
# SPARQL query
_sparql = <<~SPARQL
PREFIX : <https://www.datatourisme.fr/ontology/core#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
Expand Down
6 changes: 3 additions & 3 deletions tests/datatourisme/datatourisme_settings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

class TestDatatourismeSettings < Test::Unit::TestCase
def test_datatourisme_expected_settings
settings = DatatourismeSource::Settings.new({ key: 'key', flow_key: 'id', destination_id: 'id', datas: [] })
settings = DatatourismeSource::Settings.new({ app_key: 'app_key', source_id: 'id' })
instance_vars = settings.instance_variables.map(&:to_s)

expected_vars = %w[@key @flow_key]
expected_vars = %w[@app_key @source_id]
assert((expected_vars - instance_vars).empty?)
end

def test_datatourisme_with_unexpected_settings
assert_raise(ArgumentError) do
DatatourismeSource::Settings.new({ key: 'key', flow_key: 'id', unexpected: 'unexpected' })
DatatourismeSource::Settings.new({ app_key: 'app_key', source_id: 'id', unexpected: 'unexpected' })
end
end

Expand Down

0 comments on commit 43f5a31

Please sign in to comment.