Install the rss gem
masterTo use the RSS library in your Ruby application, add it to your Gemfile:
gem 'rss'Then run bundle. Alternatively, you can install it directly via the command line:
gem install rssrepository·master·Indexed 42 days ago
https://github.com/ruby/rssA Ruby gem for parsing and creating RSS and Atom feeds. It supports RSS 0.91, 1.0, 2.0, and Atom (RFC 4287) formats, providing tools like RSS::Parser for reading feeds and RSS::Maker for generating them via a Builder-style DSL. The library also includes specialized support for iTunes podcast namespaces, allowing the configuration of itunes_ prefixed accessors and media enclosures.
To use the RSS library in your Ruby application, add it to your Gemfile:
gem 'rss'Then run bundle. Alternatively, you can install it directly via the command line:
gem install rssThe RSS library provides tooling for the following formats:
The RSS::Atom::Content class manages the content of an entry. It supports two modes:
src attribute to point to a URI. In this mode, the element should not contain inline text.type attribute to specify a MIME media type (e.g., text/plain, text/html, application/xhtml+xml).If the type is xhtml, you can provide XML content via the xml attribute. The library provides helper methods to check the content type:
inline_text?: Returns true if the type is text or html (or nil).inline_html?: Returns true if the type is html or text/html.inline_xhtml?: Returns true if the type is xhtml.out_of_line?: Returns true if a src attribute is present.content = RSS::Atom::Content.new
content.type = "text/html"
content.xml = "<div>Hello World</div>"
# or
content.src = "http://example.com/content.xml"Atom uses a PersonConstruct to describe individuals or entities. This is implemented in RSS::Atom::Author and RSS::Atom::Contributor. These elements can contain:
name: The name of the person/entity.uri: A URI identifying the person/entity.email: An email address for the person/entity.You can generate RSS 1.0 feeds using a Builder-style DSL provided by RSS::Maker.make("1.0"). The block yields a maker object which allows you to configure the channel and add items.
Key components of the DSL:
maker.channel: Used to set channel-level metadata like language, author, about, link, title, and description.maker.items.new_item: Used to create new feed items. Inside the block, you can set item.link, item.title, and item.updated.To output the generated feed, call puts on the object returned by RSS::Maker.make.
require "rss"
rss = RSS::Maker.make("1.0") do |maker|
maker.channel.language = "en"
maker.channel.author = "matz"
maker.channel.about = "About my feed."
maker.channel.updated = Time.now.to_s
maker.channel.link = "http://www.ruby-lang.org/en/feeds/news.rss"
maker.channel.title = "Example Feed"
maker.channel.description = "A longer description of my feed."
maker.items.new_item do |item|
item.link = "http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/"
item.title = "Ruby 1.9.2-p136 is released"
item.updated = Time.now.to_s
end
end
puts rssThe rss/image.rb module provides support for the Image XML namespace (http://purl.org/rss/1.0/modules/image/). This allows you to attach image metadata to RSS Channel and Item objects.
When using RSS::RDF::Item, you can attach an image_item which supports:
about: A URI describing the image.resource: A URI pointing to the image resource.width: The width of the image (integer).height: The height of the image (integer).When using RSS::RDF::Channel, you can attach an image_favicon which supports:
about: A URI describing the favicon.size: The size of the favicon. Must be one of: small, medium, or large.Note that width and height are aliased to image_width and image_height respectively, and size is aliased to image_size for favicons.
# Example: Adding image metadata to an item
# (Assuming an RSS 1.0 channel/item context)
# For an Item:
item.image_item = RSS::ImageItem.new(about_uri, resource_uri)
item.image_item.width = 800
item.image_item.height = 600
# For a Channel (Favicon):
channel.image_favicon = RSS::ImageFavicon.new(about_uri, 'large')
channel.image_favicon.size = 'medium'You can generate RSS 2.0 feeds using a Builder-style DSL provided by RSS::Maker.make("2.0"). The DSL allows you to configure the channel properties and add multiple items via maker.items.new_item blocks.
Key components of the DSL:
maker.channel: Used to set channel-level metadata like title, description, link, language, author, and updated.maker.items.new_item: A block used to define individual feed items. Inside this block, you can set item.title, item.link, item.updated, item.author, and item.pubDate (or the alias item.date).require "rss"
rss = RSS::Maker.make("2.0") do |maker|
maker.channel.language = "en"
maker.channel.author = "matz"
maker.channel.updated = Time.now.to_s
maker.channel.link = "http://www.ruby-lang.org/en/feeds/news.rss"
maker.channel.title = "Example Feed"
maker.channel.description = "A longer description of my feed."
maker.items.new_item do |item|
item.link = "http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/"
item.title = "Ruby 1.9.2-p136 is released"
item.updated = Time.now.to_s
end
end
puts rssFor RSS 2.0 feeds, you can include iTunes podcast metadata by using itunes_ prefixed accessors on both the channel and individual items. This allows you to set properties like itunes_author, itunes_summary, itunes_image, and itunes_category.
You can also define media enclosures for audio files using the enclosure object on an item.
require "rss"
rss = RSS::Maker.make("2.0") do |maker|
maker.channel.title = "Example Podcast"
maker.channel.link = "https://example.com/podcast"
maker.channel.description = "An example podcast feed"
maker.channel.itunes_author = "Example Author"
maker.channel.itunes_summary = "A podcast created with RSS::Maker"
maker.channel.itunes_image = "https://example.com/podcast.png"
maker.channel.itunes_explicit = false
maker.channel.itunes_type = "serial"
maker.channel.itunes_owner.itunes_name = "Example Author"
maker.channel.itunes_owner.itunes_email = "author@example.com"
maker.channel.itunes_categories.new_category.text = "Technology"
maker.items.new_item do |item|
item.title = "Episode 1"
item.link = "https://example.com/podcast/1"
item.itunes_title = "Getting started"
item.itunes_author = "Example Author"
item.itunes_summary = "The first episode"
item.itunes_duration = "12:34"
item.itunes_season = 1
item.itunes_episode = 1
item.itunes_episodeType = "full"
item.enclosure.url = "https://example.com/podcast/1.mp3"
item.enclosure.length = 12_345_678
item.enclosure.type = "audio/mpeg"
end
end
puts rssYou can read RSS or Atom feeds using RSS::Parser.parse. This method accepts a source (such as a URL or string) and returns an object containing the feed data. You can access the channel information via .channel and iterate through feed entries using .items.
The library supports RSS 0.91, 1.0, 2.0, and Atom formats.
require 'rss'
feed = RSS::Parser.parse('https://www.ruby-lang.org/en/feeds/news.rss')
puts "Title: #{feed.channel.title}"
feed.items.each do |item|
puts "Item: #{item.title}"
endUse RSS::Maker.make(type) to generate new feeds using a Builder-style DSL. The type argument specifies the format (e.g., "atom", "2.0", "1.0").
Inside the block, you can configure the maker.channel and add items using maker.items.new_item.
require "rss"
rss = RSS::Maker.make("atom") do |maker|
maker.channel.author = "matz"
maker.channel.updated = Time.now.to_s
maker.channel.about = "https://www.ruby-lang.org/en/feeds/news.rss"
maker.channel.title = "Example Feed"
maker.items.new_item do |item|
item.link = "https://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/"
item.title = "Ruby 1.9.2-p136 is released"
item.updated = Time.now.to_s
end
end
puts rssThe RSS Maker classes validate required fields before generating the feed. If a required field is missing, a NotSetError is raised.
Common required fields include:
about, link, description (if provided), title (if provided).link, title (if provided).url, title, link.link, title, description, name.You can generate RSS 0.91 feeds using a Builder-like DSL provided by RSS::Maker.make("0.91"). The DSL allows you to configure the channel (language, author, title, etc.), add an image, and define multiple items using the maker.items.new_item block.
require "rss"
rss = RSS::Maker.make("0.91") do |maker|
maker.channel.language = "en"
maker.channel.author = "matz"
maker.channel.updated = Time.now.to_s
maker.channel.link = "http://www.ruby-lang.org/en/feeds/news.rss"
maker.channel.title = "Example Feed"
maker.channel.description = "A longer description of my feed."
maker.image.url = "http://www.ruby-lang.org/images/logo.gif"
maker.image.title = "An image"
maker.items.new_item do |item|
item.link = "http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/"
item.title = "Ruby 1.9.2-p136 is released"
item.updated = Time.now.to_s
end
end
puts rss