Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of RSS and Atom feeds #35

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/controllers/feed_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class FeedController < ApplicationController
skip_before_action :authenticate_user!

# GET /feed.rss
# GET /feed.atom
def index
@talks = Talk.all.order(created_at: :desc).includes(:speakers).limit(100)
krapans marked this conversation as resolved.
Show resolved Hide resolved

respond_to do |format|
format.rss { render layout: false }
format.atom { render layout: false }
end
end
end
16 changes: 16 additions & 0 deletions app/views/feed/index.atom.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
atom_feed do |feed|
feed.title("Ruby Videos")
feed.updated(@talks.first.updated_at)

@talks.each do |talk|
feed.entry(talk) do |entry|
entry.title(talk.title)
entry.summary(talk.description) if talk.description.present?
entry.link(href: talk_url(talk.slug))
entry.author do |author|
author_names = talk.speakers.pluck(:name).join(", ")
author.name(author_names.present? ? author_names : "Unknown")
end
end
end
end
18 changes: 18 additions & 0 deletions app/views/feed/index.rss.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0" do
xml.channel do
xml.title "Ruby Videos"
xml.description "A collection of talks of Ruby conferences around the world"
xml.link root_url

@talks.each do |talk|
xml.item do
xml.title talk.title
xml.description talk.description
xml.pubDate DateTime.parse(talk.date.to_s).rfc822
xml.link talk_url(talk.slug)
xml.guid talk_url(talk.slug)
end
end
end
end
4 changes: 4 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<%= vite_client_tag %>
<%= vite_javascript_tag "application" %>
<%= vite_stylesheet_tag "application.css" %>

<%= auto_discovery_link_tag :rss, feed_url(format: :rss) %>
<%= auto_discovery_link_tag :atom, feed_url(format: :atom) %>

<%= yield :head %>
</head>

Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
get "sign_up", to: "registrations#new"
post "sign_up", to: "registrations#create"

# rss feed
get "feed", to: "feed#index"

resources :sessions, only: [:index, :show, :destroy]
resource :password, only: [:edit, :update]
namespace :identity do
Expand Down
29 changes: 29 additions & 0 deletions test/controllers/feed_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "test_helper"

class FeedControllerTest < ActionDispatch::IntegrationTest
setup do
@talks = talks(:one, :two)
frozen_date_time = DateTime.new(2022, 1, 15, 10, 0, 0)
@talks.each do |talk|
talk.update!(created_at: frozen_date_time, updated_at: frozen_date_time)
end
end

test "should get rss format" do
get feed_url(format: :rss)

assert_response :success
assert_equal "/feed.rss", path
assert_equal "application/rss+xml; charset=utf-8", response.content_type
assert_equal File.read("test/fixtures/files/feed.rss"), response.parsed_body
end

test "should get atom format" do
get feed_url(format: :atom)

assert_response :success
assert_equal "/feed.atom", path
assert_equal "application/atom+xml; charset=utf-8", response.content_type
assert_equal File.read("test/fixtures/files/feed.atom"), response.parsed_body
end
end
32 changes: 32 additions & 0 deletions test/fixtures/files/feed.atom
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
<id>tag:www.example.com,2005:/feed</id>
<link rel="alternate" type="text/html" href="http://www.example.com"/>
<link rel="self" type="application/atom+xml" href="http://www.example.com/feed.atom"/>
<title>Ruby Videos</title>
<updated>2022-01-15T10:00:00Z</updated>
<entry>
<id>tag:www.example.com,2005:Talk/298486374</id>
<published>2022-01-15T10:00:00Z</published>
<updated>2022-01-15T10:00:00Z</updated>
<link rel="alternate" type="text/html" href="http://www.example.com/talks/talk-title-2"/>
<title>talk title 2</title>
<summary>talk descritpion 2</summary>
<link href="http://www.example.com/talks/talk-title-2"/>
<author>
<name>Obie Fernandez</name>
</author>
</entry>
<entry>
<id>tag:www.example.com,2005:Talk/980190962</id>
<published>2022-01-15T10:00:00Z</published>
<updated>2022-01-15T10:00:00Z</updated>
<link rel="alternate" type="text/html" href="http://www.example.com/talks/talk-title-1"/>
<title>talk title 1</title>
<summary>talk descritpion 1</summary>
<link href="http://www.example.com/talks/talk-title-1"/>
<author>
<name>Obie Fernandez</name>
</author>
</entry>
</feed>
22 changes: 22 additions & 0 deletions test/fixtures/files/feed.rss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Ruby Videos</title>
<description>A collection of talks of Ruby conferences around the world</description>
<link>http://www.example.com/</link>
<item>
<title>talk title 2</title>
<description>talk descritpion 2</description>
<pubDate>Sat, 1 Jan 2022 00:00:00 +0000</pubDate>
<link>http://www.example.com/talks/talk-title-2</link>
<guid>http://www.example.com/talks/talk-title-2</guid>
</item>
<item>
<title>talk title 1</title>
<description>talk descritpion 1</description>
<pubDate>Sat, 1 Jan 2022 00:00:00 +0000</pubDate>
<link>http://www.example.com/talks/talk-title-1</link>
<guid>http://www.example.com/talks/talk-title-1</guid>
</item>
</channel>
</rss>
4 changes: 4 additions & 0 deletions test/fixtures/talks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ one:
description: talk descritpion 1
slug: talk-title-1
year: 2022
speakers: one
date: 2022-01-01

two:
title: talk title 2
description: talk descritpion 2
slug: talk-title-2
year: 2022
speakers: one
date: 2022-01-01