Skip to content

Commit

Permalink
Simplify Datatoursime
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Dec 1, 2024
1 parent b33899c commit 6fdedef
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 41 deletions.
19 changes: 1 addition & 18 deletions datasources/connectors/datatourisme.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true
# typed: true

require 'async'
require 'sorbet-runtime'

require_relative 'connector'
Expand All @@ -23,22 +22,6 @@ def setup(kiba)
]
}))

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|
Async do
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
end.each(&:wait)
super
end
end
22 changes: 6 additions & 16 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,25 +37,17 @@ 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',
Expand Down Expand Up @@ -90,7 +80,7 @@ def map_id(feat)
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
8 changes: 4 additions & 4 deletions tests/datatourisme/datatourimse_fetch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
require_relative '../../datasources/sources/datatourisme'

class TestDatatourismeFetch < Test::Unit::TestCase
@@api_key = ENV.fetch('DATATOURISME_API_KEY')
@@flow_key = ENV.fetch('DATATOURISME_FLOW_KEY')
@@app_key = ENV.fetch('DATATOURISME_API_KEY')
@@source_id = ENV.fetch('DATATOURISME_FLOW_KEY')

def setup
@url = "https://diffuseur.datatourisme.fr/webservice/#{@@flow_key}/#{@@api_key}"
@url = "https://diffuseur.datatourisme.fr/webservice/#{@@source_id}/#{@@app_key}"
end

def test_datatourisme_fetch_headers
Expand All @@ -22,7 +22,7 @@ def test_datatourisme_fetch_headers
end

def test_datatourisme_fetch_data
datas = DatatourismeSource.fetch("#{@@flow_key}/#{@@api_key}")
datas = DatatourismeSource.fetch("#{@@source_id}/#{@@app_key}")

assert_not_nil(datas)
assert_true(datas.is_a?(Array))
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 6fdedef

Please sign in to comment.