From 0e234bc95e7c0af2df76c33458f9b91886b9745f Mon Sep 17 00:00:00 2001 From: Philip Kheav Date: Mon, 15 Mar 2021 17:38:12 +0000 Subject: [PATCH] fix(event scrape): simplify bookings parsing --- drivers/place/event_scrape.cr | 19 ++++++------------- drivers/place/event_scrape_spec.cr | 2 ++ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/drivers/place/event_scrape.cr b/drivers/place/event_scrape.cr index 2a34c78815..957174518b 100644 --- a/drivers/place/event_scrape.cr +++ b/drivers/place/event_scrape.cr @@ -6,15 +6,13 @@ class Place::EventScrape < PlaceOS::Driver default_settings({ zone_ids: ["placeos-zone-id"], - internal_domains: ["PlaceOS.com"], - poll_interval: 5 + internal_domains: ["PlaceOS.com"] }) accessor staff_api : StaffAPI_1 @zone_ids = [] of String @internal_domains = [] of String - @poll_interval : Time::Span = 5.minutes alias Event = PlaceCalendar::Event @@ -30,24 +28,19 @@ class Place::EventScrape < PlaceOS::Driver end def on_update - schedule.clear - @zone_ids = setting?(Array(String), :zone_ids) || [] of String @internal_domains = setting?(Array(String), :internal_domains) || [] of String - @poll_interval = (setting?(UInt32, :poll_interval) || 5).minutes end - def get_bookings + def todays_bookings response = { internal_domains: @internal_domains, systems: {} of String => SystemWithEvents } - logger.debug { "Getting bookings for zones" } - logger.debug { @zone_ids.inspect } - - start_epoch = Time.utc.at_beginning_of_day.to_unix - end_epoch = start_epoch + 86400 # seconds in a day + now = Time.local + start_epoch = now.at_beginning_of_day.to_unix + end_epoch = now.at_end_of_day.to_unix @zone_ids.each do |z_id| staff_api.systems(zone_id: z_id).get.as_a.each do |sys| @@ -70,7 +63,7 @@ class Place::EventScrape < PlaceOS::Driver booking_module = staff_api.modules_from_system(sys_id).get.as_a.find { |mod| mod["name"] == "Bookings" } # If the system has a booking module with bookings if booking_module && (bookings = staff_api.get_module_state(booking_module["id"].as_s).get["bookings"]?) - bookings = JSON.parse(bookings.as_s).as_a.map { |b| Event.from_json(b.to_json) } + bookings = Array(Event).from_json(bookings.as_s) # If both start_epoch and end_epoch are passed if start_epoch && end_epoch diff --git a/drivers/place/event_scrape_spec.cr b/drivers/place/event_scrape_spec.cr index 0a6d3967b6..c59793aeb3 100644 --- a/drivers/place/event_scrape_spec.cr +++ b/drivers/place/event_scrape_spec.cr @@ -12,6 +12,7 @@ class StaffAPI < DriverSpecs::MockDriver sys_1 = { id: "sys-1", + name: "Room 1", zones: ["placeos-zone-id"] } @@ -22,6 +23,7 @@ class StaffAPI < DriverSpecs::MockDriver sys_1, { id: "sys-2", + name: "Room 2", zones: ["zone-1"] } ]