Skip to content

Commit

Permalink
Simplify OpenAgenda
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Dec 1, 2024
1 parent 0ed826b commit b33899c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 52 deletions.
43 changes: 7 additions & 36 deletions datasources/connectors/open_agenda.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,29 @@
# frozen_string_literal: true
# typed: true

require 'async'
require 'sorbet-runtime'

require_relative 'connector'
require_relative '../sources/metadata'
require_relative '../sources/open_agenda'

class OpenAgenda < Connector
def self.source_class
OpenAgendaSource
end

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/event.schema.json',
],
'i18n' => [
'datasources/schemas/tags/base.i18n.json',
'datasources/schemas/tags/event.i18n.json',
]
}))

agenda_uid = @settings['agenda_uid'].to_s
if agenda_uid.empty?
agendas = OpenAgendaSource.fetch('agendas', {
key: @settings['key']
}, 'agendas')
agendas.map do |agenda|
agenda_uid = agenda['uid']
_call(kiba, agenda_uid)
end
else
_call(kiba, agenda_uid)
end
end

def _call(kiba, agenda_uid)
@settings['agenda_uid'] = agenda_uid
events = OpenAgendaSource.fetch("agendas/#{agenda_uid}/events", {
key: @settings['key'],
'timings[gte]' => Date.today,
})

events.map do |event|
Async do
destination_id = "#{agenda_uid}-#{event['uid']}-#{event['title']['fr']}"
name = event['title']

kiba.source(
OpenAgendaSource,
@job_id,
destination_id,
name,
OpenAgendaSource::Settings.from_hash(@settings.merge({ 'event_uid' => event['uid'].to_s, 'agenda_uid' => agenda_uid })),
)
end
end.each(&:wait)
super
end
end
24 changes: 9 additions & 15 deletions datasources/sources/open_agenda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@

class OpenAgendaSource < Source
# OpenAgendaSource::Settings
# url for lecture requires an API key, agenda UID
# url for reading requires an API key, agenda UID
include OpenAgendaMixin

class Settings < Source::SourceSettings
const :key, String, name: 'key' # API key
const :agenda_uid, T.nilable(String), name: 'agenda_uid' # Agenda UID
const :event_uid, T.nilable(String), name: 'event_uid' # Event UID
end

extend T::Generic
Expand Down Expand Up @@ -87,14 +86,6 @@ def self.fetch(path, query, key = 'events', size = 100, **kwargs)
results.to_a
end

def self.fetch_event(path, query)
url = T.let(build_url(path, query), T.nilable(String))
response = HTTP.follow.get(url)
raise [url, response].inspect unless response.status.success?

[JSON.parse(response.body)['event']]
end

def openning(periode)
return nil if periode.blank?

Expand Down Expand Up @@ -255,22 +246,25 @@ def map_native_properties(feat, properties)
hearing_impairment: hearing_impairment(feat),
psychic_impairment: psychic_impairment(feat),
agenda: {
id: @settings.agenda_uid.to_s,
id: @settings.agenda_uid,
name: jp_first(feat, 'originAgenda.title'),
},
long_description: jp_first(feat, 'longDescription'),
keywords: jp(feat, 'keywords'),
keywords: jp(feat, 'keywords.fr').flatten.compact_blank,
})
end

def each
if ENV['NO_DATA']
[]
else
event = self.class.fetch_event("agendas/#{@settings.agenda_uid}/events/#{@settings.event_uid}", {
key: @settings.key
events = self.class.fetch("agendas/#{@settings.agenda_uid}/events", {
key: @settings.key,
detailed: 1,
longDescriptionFormat: 'HTML',
'timings[gte]' => Time.now.utc.to_date,
})
super(event)
super(events)
end
end
end
2 changes: 1 addition & 1 deletion tests/open_agenda/open_agenda_settings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_open_agenda_expected_settings
settings = OpenAgendaSource::Settings.new({ key: 'key', agenda_uid: 'id' })
instance_vars = settings.instance_variables.map(&:to_s)

expected_vars = %w[@key @agenda_uid @event_uid]
expected_vars = %w[@key @agenda_uid]
assert((expected_vars - instance_vars).empty?)
end

Expand Down

0 comments on commit b33899c

Please sign in to comment.