Skip to content

Commit

Permalink
Add basic weather alert report
Browse files Browse the repository at this point in the history
  • Loading branch information
alepore committed Jun 14, 2023
1 parent 790dc89 commit 687a480
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/fortuna_luca/reports/allerta_meteo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.find do |entry|
entry.title.match?(/^Allerta .* valida .* #{date.strftime("%d-%m-%Y")}/)
end
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
20 changes: 20 additions & 0 deletions lib/fortuna_luca/weather/allerta_meteo/client.rb
Original file line number Diff line number Diff line change
@@ -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<Feedjira::Parser::AtomEntry>] The feed entries
def call
data = HTTPClient.get(URL).body
Feedjira.parse(data).entries
end
end
end
end
end
5 changes: 5 additions & 0 deletions rakelib/send_allerta_meteo_report.rake
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 687a480

Please sign in to comment.