feat: add RSS feed functionality
Some checks failed
Docker / docker (push) Has been cancelled

- Introduce RssController to handle RSS feed requests
- Add a new route for the RSS feed
- Implement RSS feed view to display weather art
- Update application layout to include RSS feed link
- Set content type for RSS responses

This commit adds an RSS feed feature that allows users to
subscribe to updates on daily AI-generated weather art.
The feed includes the latest weather art and relevant
metadata, enhancing user engagement and accessibility.
This commit is contained in:
songtianlun 2025-02-19 17:38:49 +08:00
parent c35f09660a
commit 9fe92b1fc4
10 changed files with 60 additions and 0 deletions

View File

@ -67,6 +67,8 @@ gem "mini_magick", "~> 4.13.2"
gem "redis", "~> 5.3"
gem "builder", "~> 3.3"
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"

View File

@ -523,6 +523,7 @@ DEPENDENCIES
aws-sdk-s3 (~> 1.170)
bootsnap
brakeman
builder (~> 3.3)
bullet (~> 8.0)
capybara
cssbundling-rails

View File

@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
include SeoConcern
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
before_action :log_browser_info
before_action :set_content_type_for_rss, if: -> { request.format.rss? }
# allow_browser versions: :modern
# allow_browser versions: :modern,
# patterns: [
@ -78,4 +79,8 @@ class ApplicationController < ActionController::Base
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def set_content_type_for_rss
response.headers["Content-Type"] = "application/rss+xml; charset=utf-8"
end
end

View File

@ -0,0 +1,9 @@
class RssController < ApplicationController
def feed
@weather_arts = WeatherArt.order(created_at: :desc).limit(20)
respond_to do |format|
format.rss { render layout: false }
end
end
end

View File

@ -0,0 +1,2 @@
module RssHelper
end

View File

@ -1,6 +1,13 @@
<!-- 页脚 -->
<footer class="footer footer-center p-8 bg-base-200 text-base-content">
<div class="container mx-auto flex flex-col gap-2">
<%= link_to rss_feed_path(format: :rss), class: "btn btn-ghost", title: "RSS Feed" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 5c7.18 0 13 5.82 13 13M6 11a7 7 0 017 7m-6 0a1 1 0 11-2 0 1 1 0 012 0z" />
</svg>
<!-- <span class="ml-2">RSS Feed</span>-->
<% end %>
<div id="busuanzi_container" class="text-xs opacity-70">
<div class="space-x-2">
<span>Page: </span>

View File

@ -33,6 +33,8 @@
<%# Includes all stylesheet files in app/assets/stylesheets %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= auto_discovery_link_tag :rss, rss_feed_url(format: :rss), title: 'RSS Feed' %>
<script defer data-domain="todayaiweather.com" src="https://plausible.frytea.com/js/script.js"></script>
<script defer src="https://busuanzi.frytea.com/js"></script>

View File

@ -0,0 +1,24 @@
# app/views/rss/feed.rss.builder
xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0",
"xmlns:atom" => "http://www.w3.org/2005/Atom" do
xml.channel do
xml.title "Today AI Weather Art"
xml.description "Daily AI-generated weather art and forecasts"
xml.link root_url
xml.language "en"
xml.atom :link, href: rss_feed_url(format: :rss), rel: "self", type: "application/rss+xml"
@weather_arts.each do |art|
xml.item do
xml.title "#{art.city.full_name} Weather Art"
xml.description art.description
xml.pubDate art.created_at.to_fs(:rfc822)
xml.link city_weather_art_url(art.city, art)
xml.guid city_weather_art_url(art.city, art)
# 如果有图片,添加图片链接
xml.enclosure url: rails_blob_url(art.webp_image.processed), type: "image/jpeg" if art.image.attached?
end
end
end
end

View File

@ -25,6 +25,7 @@ Rails.application.routes.draw do
get "cities/show"
get "home/index"
get "sitemaps/*path", to: "sitemaps#show", format: false
get "feed", to: "rss#feed", format: "rss", as: :rss_feed
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)

View File

@ -0,0 +1,7 @@
require "test_helper"
class RssControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end