Skip to content

Commit

Permalink
Simplify OpenAgenda
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Dec 2, 2024
1 parent db4f392 commit 9e7f7ff
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 70 deletions.
40 changes: 7 additions & 33 deletions datasources/connectors/open_agenda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,22 @@
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]' => Time.now.utc.to_date,
})

events.map do |event|
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
super
end
end
30 changes: 12 additions & 18 deletions datasources/sources/open_agenda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
require_relative 'source'
require_relative 'open_agenda_mixin'

# https://developers.openagenda.com/10-lecture/

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 All @@ -46,9 +47,7 @@ def self.build_url(path, query)
"https://api.openagenda.com/v2/#{path}?#{query_string}"
end

def self.fetch(path, query, key = 'events', size = 100, **kwargs)
max_retry = kwargs[:max_retry] || 10
sleeping_time = kwargs[:sleeping_time] || 0.3
def self.fetch(path, query, key = 'events', size = 100, max_retry: 10, sleeping_time: 0.3)
results = T.let(Set.new, T::Set[T.untyped])
retries = T.let(0, Integer)

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
22 changes: 4 additions & 18 deletions tests/open_agenda/open_agenda_fetch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,16 @@ class TestOpenAgendaFetch < Test::Unit::TestCase
@@api_key = '4d2d1e06a4044b4485b798a9efce4c9c'
@@agenda_uid = 83_339_747

def test_open_agenda_fetch_all_agendas_in_occitanie
total = HTTP.follow.get("https://api.openagenda.com/v2/agendas?key=#{@@api_key}&search=Occitanie&official=1").parse['total']
agendas = OpenAgendaSource.fetch('agendas', { key: @@api_key, search: 'Occitanie', official: 1 }, 'agendas')

assert_equal(total, agendas.size)
end

def test_open_agenda_fetch_all_events_in_occitanie
total = HTTP.follow.get("https://api.openagenda.com/v2/agendas/#{@@agenda_uid}/events?key=#{@@api_key}").parse['total']
events = OpenAgendaSource.fetch("agendas/#{@@agenda_uid}/events", { key: @@api_key }, 'events', 300)

assert_equal(total, events.size)
end

def test_open_agenda_fetch_all_agendas_in_aquitaine
total = HTTP.follow.get("https://api.openagenda.com/v2/agendas?key=#{@@api_key}&search=Aquitaine&official=1").parse['total']
agendas = OpenAgendaSource.fetch('agendas', { key: @@api_key, search: 'Aquitaine', official: 1 }, 'agendas')
def test_open_agenda_fetch_all_agenda_event
total = HTTP.follow.get("https://api.openagenda.com/v2/agendas/#{@@agenda_uid}/events?key=#{@@api_key}&size=1&timings[gte]=#{Time.now.utc.to_date}").parse['total']
agendas = OpenAgendaSource.fetch("agendas/#{@@agenda_uid}/events", { key: @@api_key, 'timings[gte]' => Time.now.utc.to_date })

assert_equal(total, agendas.size)
end

def test_open_agenda_fetch_raise_error_on_incomplete_data
assert_raise do
OpenAgendaSource.fetch('agendas', { key: @@api_key, search: 'Normandie', official: 1 }, 'agendas', max_retry: 0, sleeping_time: 0.00)
OpenAgendaSource.fetch('agendas/-1/events', { key: @@api_key, 'timings[gte]' => Time.now.utc.to_date }, max_retry: 0, sleeping_time: 0.00)
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 9e7f7ff

Please sign in to comment.