Generating RSS Feed for Podcast in Rails 5

The episode model has published method.

  def self.published
    where('published_at <= ?', Time.now.utc)
  end

The index action in the episodes controller looks like this:

def index
  published_episodes = Episode.published
  respond_to do |format|
    format.html do
      @episodes = published_episodes
    end
    format.rss do
      @episodes = published_episodes.free.order('position desc')
    end
  end
end

Define routes.rb:

resources :episodes

Create index.rss.builder:

title = "Rails Screencasts"
author = "Bala Paranj"
description = "Learn practical tips and techniques on how to develop web applications using Ruby on Rails web framework. The episodes are short to minimize the amount of content to avoid overwhelming learners. The episodes are designed to give developers a quick way to get their feet wet and learn by building web apps."
keywords = "rails, ruby on rails, screencasts, podcasts, tips, tricks, tutorials, training, programming, railscast, web application framework"
ext = 'mp4'

#encoding: UTF-8

xml.instruct! :xml, :version => "1.0"

xml.rss :version => "2.0", "xmlns:itunes" => "http://www.itunes.com/dtds/podcast-1.0.dtd",  "xmlns:media" => "http://search.yahoo.com/mrss/", "xmlns:atom" => "http://www.w3.org/2005/Atom" do
  xml.channel do
    xml.tag!("atom:link",  "href"=>"https://rubyplus.com/episodes.rss", "rel"=>"self", "type"=>"application/rss+xml") 
    xml.title title
    xml.link 'https://www.rubyplus.com'
    xml.description description
    xml.language 'en'
    xml.pubDate @episodes.first.created_at.to_s(:rfc822)
    xml.lastBuildDate @episodes.first.created_at.to_s(:rfc822)
    xml.itunes :author, author
    xml.itunes :keywords, keywords
    xml.itunes :explicit, 'clean'
    xml.itunes :image, :href => "http://images.rubyplus.com/RAILS-res-3000px-winning-purple.png"
    xml.itunes :owner do
      xml.itunes :name, author
      xml.itunes :email, 'feedback@rubyplus.com'
    end
    xml.itunes :block, 'no'
    xml.itunes :category, :text => 'Technology' do
      xml.itunes :category, :text => 'Software How-To'
    end
    xml.itunes :category, :text => 'Education' do
      xml.itunes :category, :text => 'Training'
    end

    @episodes.each do  |episode|
      xml.item do
        xml.title episode.title
        xml.description episode.description
        xml.pubDate episode.created_at.to_s(:rfc822)
        xml.enclosure :url => "http://media.rubyplus.com/#{episode.file_name}", :length => episode.file_size, :type => 'video/mp4'
        xml.link episode_url(episode)
        xml.guid({:isPermaLink => "false"}, episode.permalink)
        xml.itunes :author, author
        xml.itunes :subtitle, truncate(episode.description, :length => 150)
        xml.itunes :summary, episode.description
        xml.itunes :explicit, 'no'
        xml.itunes :duration, episode.duration
      end
    end
  end
end

You can see this in action at RubyPlus episodes page. You will be able to subscribe to the screencast on iTunes.


Related Articles