From b3c1b634bf9d4493eb0115573e11d25b1858cf9c Mon Sep 17 00:00:00 2001 From: Alessandro Lepore Date: Wed, 14 Jun 2023 23:48:48 +0200 Subject: [PATCH] Add basic weather alert report --- lib/fortuna_luca/reports/allerta_meteo.rb | 47 +++++++++++++++++++ .../weather/allerta_meteo/client.rb | 20 ++++++++ rakelib/send_allerta_meteo_report.rake | 5 ++ 3 files changed, 72 insertions(+) create mode 100644 lib/fortuna_luca/reports/allerta_meteo.rb create mode 100644 lib/fortuna_luca/weather/allerta_meteo/client.rb create mode 100644 rakelib/send_allerta_meteo_report.rake diff --git a/lib/fortuna_luca/reports/allerta_meteo.rb b/lib/fortuna_luca/reports/allerta_meteo.rb new file mode 100644 index 0000000..6e80168 --- /dev/null +++ b/lib/fortuna_luca/reports/allerta_meteo.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require_relative "report" +require_relative "../weather/allerta_meteo/client" + +module FortunaLuca + module Reports + class AllertaMeteo + include Report + + def call + return unless show? + + config.each do |chat_id| + send_telegram_message(chat_id, message) if message + end + + true + end + + private + + def message + return unless tomorrow_entry + + [ + "⚠️ #{tomorrow_entry.title}", + tomorrow_entry.summary, + tomorrow_entry.links.first + ].join("\n") + end + + def tomorrow_entry + @tomorrow_entry ||= feed_entries.first + end + + def feed_entries + FortunaLuca::Weather::AllertaMeteo::Client.new.call + end + + # JSON Config format: ["chat_id"] + def config + @config ||= JSON.parse(env_or_blank("REPORTS_ALLERTA_METEO_CONFIG")) + end + end + end +end diff --git a/lib/fortuna_luca/weather/allerta_meteo/client.rb b/lib/fortuna_luca/weather/allerta_meteo/client.rb new file mode 100644 index 0000000..da314f4 --- /dev/null +++ b/lib/fortuna_luca/weather/allerta_meteo/client.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "httpclient" +require "feedjira" + +module FortunaLuca + module Weather + module AllertaMeteo + class Client + URL = "https://allertameteo.regione.marche.it/compila-allerta-portlet/feed?feed=allerte-bollettini" + + # @return [Array] The feed entries + def call + data = HTTPClient.get(URL).body + Feedjira.parse(data).entries + end + end + end + end +end diff --git a/rakelib/send_allerta_meteo_report.rake b/rakelib/send_allerta_meteo_report.rake new file mode 100644 index 0000000..2e42088 --- /dev/null +++ b/rakelib/send_allerta_meteo_report.rake @@ -0,0 +1,5 @@ +require_relative '../lib/fortuna_luca/reports/allerta_meteo' + +task :send_allerta_meteo_report do + FortunaLuca::Reports::AllertaMeteo.new(Date.today.succ).call +end