Skip to content

Commit

Permalink
Add ATOM syndication feeds to the site (#80)
Browse files Browse the repository at this point in the history
The following endpoints have been defined:

* Recent videos (master feed).
  Location: /videos/feed
  Purpose: always syndicates the most recent 15 videos on this site.

* Videos for a playlist
  Location: /playlist/:id/feed
  Purpose: always syndicates all the videos in a playlist.

* Videos for a topic
  Location: /topics/:id/feed
  Purpose: always syndicates the most recent 15 videos part of a
  playlist in a particular topic on this site.

Only the playlist feed syndicates the entire list of videos. Other feeds
discard old items as new videos are published on the site.
  • Loading branch information
danirod authored Dec 6, 2017
1 parent ef28db7 commit 822e7bc
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 13 deletions.
22 changes: 15 additions & 7 deletions app/controllers/playlists_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
class PlaylistsController < ApplicationController


before_action :playlist_set, only: [:show, :feed]

def index
@playlists = Playlist.all.order(created_at: :desc)
end

def show

def feed
@videos = @playlist.videos.includes(:playlist, playlist: [:topic]).visible.reverse
@updated_at = Video.order(updated_at: :desc).limit(1).pluck(:updated_at).first
render format: :xml, template: 'playlists/feed.xml.erb', layout: false
end

private

def playlist_set
@playlist = Playlist.friendly.find(params[:id])
if params[:id] != @playlist.slug
redirect_to @playlist, status: 301
end
redirect_to @playlist, status: 301 if params[:id] != @playlist.slug
end

end
16 changes: 13 additions & 3 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
class TopicsController < ApplicationController


before_action :topic_set, only: [:show, :feed]

def index
@topics = Topic.all
end

def show
@topic = Topic.friendly.find(params[:id])
def feed
@videos = Video.visible.joins(:playlist).includes(:playlist, playlist: [:topic]).where(playlists: { topic: @topic }).order(published_at: :desc).limit(15)
@updated_at = Video.order(updated_at: :desc).limit(1).pluck(:updated_at).first
render format: :xml, template: 'topics/feed.xml.erb', layout: false
end

private

def topic_set
@topic = Topic.friendly.find(params[:id])
redirect_to topic_path(@topic) if params[:id] != @topic.slug
end
end
6 changes: 6 additions & 0 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ def show
authenticate_user! if @video.scheduled? && @video.private
@in_playlist = @video.higher_items(5) + [@video] + @video.lower_items(5)
end

def feed
@videos = Video.visible.includes(:playlist, playlist: [:topic]).order(published_at: :desc).limit(15)
@updated_at = Video.order(updated_at: :desc).limit(1).pluck(:updated_at).first
render format: :xml, template: 'videos/feed.xml.erb', layout: false
end
end
2 changes: 2 additions & 0 deletions app/views/layouts/_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<title><%= content_for?(:title) ? content_for(:title) : "makigas" %></title>
<%= tag(:meta, name: 'description', content: content_for(:description)) if content_for?(:description) %>
<%= tag(:link, rel: 'canonical', href: canonical_url) %>
<%= tag(:link, rel: 'alternate', href: feed_videos_url, title: t('.videos_feed'), type: 'application/atom+xml') %>

<%= content_for(:meta) if content_for?(:meta) -%>
<%= content_for(:twitter) -%>
<%= content_for(:facebook) -%>
Expand Down
21 changes: 21 additions & 0 deletions app/views/playlists/feed.xml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="es-ES">

<title type="text">makigas (<%= @playlist.title %>)</title>
<subtitle type="text">Lista de reproducción para el tema <%= @playlist.title %></subtitle>
<updated><%= @updated_at.iso8601 %></updated>

<id><%= feed_playlist_url(@playlist) %></id>
<icon><%= @playlist.thumbnail.url %></icon>
<logo><%= @playlist.thumbnail.url %></logo>
<link rel="self" href="<%= feed_playlist_url(@playlist) %>" type="application/atom+xml" />
<link rel="alternate" href="<%= playlist_url(@playlist) %>" type="text/html" />

<author><name>makigas.es</name></author>
<generator uri="https://github.com/makigas/makigas">makigas.es</generator>

<%- @videos.each do |video| -%>
<%= render 'videos/atom', video: video %>
<%- end -%>

</feed>
5 changes: 5 additions & 0 deletions app/views/playlists/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<meta property="og:image" content="<%= image_url(@playlist.card.url, only_path: false) %>">
<% end %>

<% content_for :meta do %>
<%= tag(:link, rel: :alternate, href: feed_playlist_url(@playlist), title: t('.meta.playlist_feed', playlist: @playlist.title), type: 'application/atom+xml') %>
<%= tag(:link, rel: :alternate, href: feed_topic_url(@playlist.topic), title: t('.meta.topic_feed', topic: @playlist.topic.title), type: 'application/atom+xml') if @playlist.topic.present? %>
<% end %>

<% if @playlist.topic %>
<% content_for :header do %>
<style>
Expand Down
21 changes: 21 additions & 0 deletions app/views/topics/feed.xml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="es-ES">

<title type="text">makigas (<%= @topic.title %>)</title>
<subtitle type="text">Vídeos recientes publicados en el tema <%= @topic.title %></subtitle>
<updated><%= @updated_at.iso8601 %></updated>

<id><%= feed_topic_url(@topic) %></id>
<icon><%= @topic.thumbnail.url %></icon>
<logo><%= @topic.thumbnail.url %></logo>
<link rel="self" href="<%= feed_topic_url(@topic) %>" type="application/atom+xml" />
<link rel="alternate" href="<%= topic_url(@topic) %>" type="text/html" />

<author><name>makigas.es</name></author>
<generator uri="https://github.com/makigas/makigas">makigas.es</generator>

<%- @videos.each do |video| -%>
<%= render 'videos/atom', video: video %>
<%- end -%>

</feed>
4 changes: 4 additions & 0 deletions app/views/topics/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<meta property="og:image" content="<%= image_url(@topic.thumbnail.url, only_path: false) %>">
<% end %>

<% content_for :meta do %>
<%= tag(:link, rel: :alternate, href: feed_topic_url(@topic), title: t('.meta.topic_feed', topic: @topic.title), type: 'application/atom+xml') %>
<% end %>

<% content_for :header do %>
<style>
.main-header, .hero-tron { background: <%= @topic.color %>; }
Expand Down
12 changes: 12 additions & 0 deletions app/views/videos/_atom.xml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<entry>
<id><%= video_url(video) %></id>
<updated><%= video.published_at.iso8601 %></updated>
<link rel="alternate" href="<%= video_url(video) %>" type="text/html" />

<title type="html"><![CDATA[<%= video.title %>]]></title>
<summary type="html"><![CDATA[<%= video.description %>]]></summary>
<category term="<%= video.playlist.title %>" />
<%- if video.playlist.topic.present? -%>
<category term="<%= video.playlist.topic.title %>" />
<%- end -%>
</entry>
21 changes: 21 additions & 0 deletions app/views/videos/feed.xml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="es-ES">

<title type="text">makigas</title>
<subtitle type="text">Vídeos recientes publicados en makigas</subtitle>
<updated><%= @updated_at.iso8601 %></updated>

<id><%= feed_videos_url %></id>
<icon><%= URI.join(root_url, 'makigas.png') %></icon>
<logo><%= URI.join(root_url, 'makigas.png') %></logo>
<link rel="self" href="<%= feed_videos_url %>" type="application/atom+xml" />
<link rel="alternate" href="<%= videos_url %>" type="text/html" />

<author><name>makigas.es</name></author>
<generator uri="https://github.com/makigas/makigas">makigas.es</generator>

<%- @videos.each do |video| -%>
<%= render 'videos/atom', video: video %>
<%- end -%>

</feed>
2 changes: 2 additions & 0 deletions app/views/videos/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<meta property="og:image" content="<%= image_url(@video.playlist.card.url, only_path: false) %>">
<% end %>
<% content_for :meta do %>
<%= tag(:link, rel: :alternate, href: feed_playlist_url(@playlist), title: t('.meta.playlist_feed', playlist: @playlist.title), type: 'application/atom+xml') %>
<%= tag(:link, rel: :alternate, href: feed_topic_url(@playlist.topic), title: t('.meta.topic_feed', topic: @playlist.topic.title), type: 'application/atom+xml') if @playlist.topic.present? %>
<% if @video.scheduled? %>
<meta name="robots" content="noindex">
<% end %>
Expand Down
12 changes: 11 additions & 1 deletion config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ es:
message: "Me obligan a informarte que este sitio usa cookies."
dismiss: "Me entero"
learn_more: "Lee más sobre las cookies"
head:
videos_feed: Vídeos recientes (ATOM)
header:
navigation: Desplegar navegación
playlists: Listas
Expand Down Expand Up @@ -108,6 +110,10 @@ es:
one: 1 episodio
other: "%{count} episodios"
title: Todas las listas de reproducción
show:
meta:
playlist_feed: "Lista: %{playlist} (ATOM)"
topic_feed: "Tema: %{topic} (ATOM)"
topics:
index:
meta:
Expand All @@ -117,6 +123,7 @@ es:
show:
meta:
description: Explora las listas de reproducción que hay dentro de esta categoría o encuentra contenido adicional.
topic_feed: "Tema: %{topic} (ATOM)"
episode:
one: 1 episodio
other: "%{count} episodios"
Expand Down Expand Up @@ -150,4 +157,7 @@ es:
one: Explora el resto de vídeos de esta lista de reproducción.
other: Una lista de reproducción de %{count} episodios.
this_playlist: Lista de reproducción
this_topic: Este tema
this_topic: Este tema
meta:
playlist_feed: "Lista: %{playlist} (ATOM)"
topic_feed: "Tema: %{topic} (ATOM)"
12 changes: 10 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@

# Main application routes
root to: 'front#index'
resources :topics, only: [:index, :show], constraints: { format: :html }
resources :videos, only: :index, constraints: { format: :html }

resources :topics, only: [:index, :show], constraints: { format: :html } do
get :feed, on: :member
end

resources :videos, only: :index, constraints: { format: :html } do
get :feed, on: :collection
end

resources :playlists, path: 'series', only: [:index, :show], constraints: { format: :html } do
get :feed, on: :member
resources :videos, path: '/', only: [:show], constraints: { format: :html }
end
get :terms, to: 'pages#terms'
Expand Down

0 comments on commit 822e7bc

Please sign in to comment.