### Install Ruby RSS Gem Source: https://github.com/ruby/rss/blob/master/README.md Instructions to add the RSS gem to a Ruby project's Gemfile and install it using Bundler, or to install it directly using the gem command. ```ruby gem 'rss' ``` -------------------------------- ### Produce Atom Feed with Ruby Source: https://github.com/ruby/rss/blob/master/README.md Example of generating an Atom feed using the Ruby RSS gem's DSL. It creates a basic feed with channel information and a single item. ```ruby 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 rss ``` -------------------------------- ### Ruby RSS Parser Configuration and Backend Selection Source: https://context7.com/ruby/rss/llms.txt This Ruby code snippet illustrates how to inspect and configure the RSS parser's backend. It shows how to list available parsers, get the current default parser, and set a new default parser (like REXMLParser). It also demonstrates parsing with specific options, including overriding the default parser and controlling validation and unknown element handling. ```ruby require 'rss' # Check available parsers puts "Available parsers:" RSS::AVAILABLE_PARSERS.each do |parser| puts " - #{parser}" end # Get current default parser current_parser = RSS::Parser.default_parser puts "\nCurrent parser: #{current_parser}" # Set default parser (REXML is pure Ruby, always available) RSS::Parser.default_parser = RSS::REXMLParser puts "Parser set to: #{RSS::Parser.default_parser}" # Parse with specific parser (override default) xml = 'Test' feed = RSS::Parser.parse( xml, validate: true, ignore_unknown_element: true, parser_class: RSS::REXMLParser ) puts "\nParsed with: #{RSS::Parser.default_parser}" puts "Feed title: #{feed.channel.title}" # Parse with different options begin # Strict: validate and reject unknown elements strict_feed = RSS::Parser.parse( xml, validate: true, ignore_unknown_element: false ) # Lenient: no validation, ignore unknown elements lenient_feed = RSS::Parser.parse( xml, validate: false, ignore_unknown_element: true ) puts "Parsing successful" rescue RSS::UnknownTagError => e puts "Unknown tag found: #{e.message}" rescue RSS::NotExpectedTagError => e puts "Unexpected tag: #{e.message}" end # Output: # Available parsers: # - RSS::REXMLParser # Current parser: RSS::REXMLParser # Parser set to: RSS::REXMLParser # Parsed with: RSS::REXMLParser # Feed title: Test ``` -------------------------------- ### Convert RSS Feeds Between Formats (Ruby) Source: https://context7.com/ruby/rss/llms.txt This example shows how to convert RSS feeds between different versions (e.g., RSS 2.0 to RSS 1.0) and to Atom format using the 'rss' gem. It includes optional transformation blocks to handle missing required fields for each target format. The converted feeds are then written to files. ```ruby require 'rss' # Parse any format feed original_feed = RSS::Parser.parse(File.read('feed-2.0.xml')) # Convert RSS 2.0 to RSS 1.0 rss10 = original_feed.to_rss("1.0") do |maker| # Handle required fields for RSS 1.0 maker.channel.about ||= maker.channel.link maker.channel.description ||= "No description" maker.items.each do |item| item.title ||= "No title" item.link ||= "UNKNOWN" end end File.write('feed-1.0.xml', rss10) # Convert to Atom atom_feed = original_feed.to_atom("feed") do |maker| maker.channel.updated ||= Time.now.to_s maker.channel.id ||= maker.channel.link maker.items.each do |item| item.id ||= item.link item.updated ||= Time.now.to_s end end File.write('feed-atom.xml', atom_feed) # Convert to different version using to_xml rss20 = original_feed.to_xml("2.0") File.write('feed-2.0-converted.xml', rss20) puts "Converted feeds created successfully" ``` -------------------------------- ### Parse RSS Feed with Ruby Source: https://github.com/ruby/rss/blob/master/README.md Example of consuming an RSS feed using the Ruby RSS gem. It parses a feed from a URL and prints the channel title and individual item titles. ```ruby 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}" end ``` -------------------------------- ### Parse iTunes Podcast Metadata in Ruby RSS Source: https://context7.com/ruby/rss/llms.txt This example shows how to parse an RSS feed with iTunes-specific podcast metadata using the Ruby RSS library. It covers accessing elements like author, image, duration, and explicit content flags. The 'rss' gem is a prerequisite. ```ruby require 'rss' podcast_xml = <<-XML Tech Talk Podcast http://podcast.example.com Weekly tech discussions Tech Talk Team Weekly discussions about technology trends Your guide to tech John Doe john@podcast.example.com no technology,programming,software episodic Episode 1: Introduction to Ruby http://podcast.example.com/ep1 Our first episode about Ruby Mon, 01 Jan 2024 10:00:00 GMT Jane Smith Getting started with Ruby In this episode we discuss Ruby basics 00:45:30 no ruby,programming,beginners 1 1 full XML feed = RSS::Parser.parse(podcast_xml) ``` -------------------------------- ### Parse and Access Dublin Core Metadata in Ruby RSS Source: https://context7.com/ruby/rss/llms.txt This snippet demonstrates how to parse an RSS feed containing Dublin Core metadata elements using the Ruby RSS library. It shows how to access and print fields like creator, date, subject, and rights. Ensure the 'rss' gem is installed. ```ruby require 'rss' xml_with_dc = <<-XML Research Papers http://research.example.com Latest research publications Study on Climate Change http://research.example.com/climate A comprehensive study Dr. Jane Smith 2024-01-15T10:00:00Z Climate Science Environmental Studies University Press © 2024 University. CC BY-NC en Research Article application/pdf DOI:10.1234/example.2024.001 Journal of Climate Studies Vol. 42 XML feed = RSS::Parser.parse(xml_with_dc) feed.items.each do |item| puts "Title: #{item.title}" # Dublin Core metadata if item.respond_to?(:dc_creator) puts "Creator: #{item.dc_creator}" end if item.respond_to?(:dc_date) puts "DC Date: #{item.dc_date}" end if item.respond_to?(:dc_subjects) puts "Subjects: #{item.dc_subjects.join(', ')}" end if item.respond_to?(:dc_publisher) puts "Publisher: #{item.dc_publisher}" end if item.respond_to?(:dc_rights) puts "Rights: #{item.dc_rights}" end if item.respond_to?(:dc_language) puts "Language: #{item.dc_language}" end if item.respond_to?(:dc_type) puts "Type: #{item.dc_type}" end if item.respond_to?(:dc_format) puts "Format: #{item.dc_format}" end if item.respond_to?(:dc_identifier) puts "Identifier: #{item.dc_identifier}" end if item.respond_to?(:dc_source) puts "Source: #{item.dc_source}" end end ``` -------------------------------- ### Create RSS/Atom Feeds with RSS::Maker DSL Source: https://context7.com/ruby/rss/llms.txt Creates RSS and Atom feeds programmatically using a block-based DSL with `RSS::Maker.make`. Supports RSS versions 0.91, 1.0, 2.0, and Atom 1.0. It automatically handles validation and XML generation, allowing for detailed configuration of channel and item metadata. ```ruby require 'rss' # Create RSS 2.0 feed rss = RSS::Maker.make("2.0") do |maker| # Set encoding and metadata maker.encoding = "UTF-8" # Configure channel maker.channel.language = "en" maker.channel.title = "Tech News" maker.channel.link = "http://technews.example.com" maker.channel.description = "Latest technology news and updates" maker.channel.managingEditor = "editor@example.com" maker.channel.webMaster = "webmaster@example.com" maker.channel.pubDate = Time.now # Add first item maker.items.new_item do |item| item.title = "Ruby 3.3 Released" item.link = "http://technews.example.com/ruby-3-3" item.description = "The Ruby core team announced the release of Ruby 3.3 with performance improvements" item.author = "admin@example.com" item.pubDate = Time.now item.guid.content = "http://technews.example.com/ruby-3-3" item.guid.isPermaLink = true end # Add second item maker.items.new_item do |item| item.title = "New JavaScript Framework" item.link = "http://technews.example.com/new-js-framework" item.description = "A new lightweight JavaScript framework promises better performance" item.pubDate = Time.now - 86400 # Yesterday end end puts rss ``` -------------------------------- ### Generate Atom Feed in Ruby Source: https://context7.com/ruby/rss/llms.txt This snippet shows how to create an Atom feed using the RSS::Maker library. It configures channel information and adds a new item with its associated details. The output is a string representing the Atom feed. ```ruby require 'rss' atom = RSS::Maker.make("atom") do |maker| maker.channel.author = "John Doe" maker.channel.updated = Time.now.to_s maker.channel.about = "http://blog.example.com/feed" maker.channel.title = "My Personal Blog" maker.channel.subtitle = "Thoughts on programming and life" maker.items.new_item do |item| item.link = "http://blog.example.com/post-1" item.title = "Getting Started with Ruby" item.updated = Time.now.to_s item.summary = "An introduction to Ruby programming" item.content = "

Ruby is a dynamic, open source programming language...

" end end puts atom ``` -------------------------------- ### Parse and Access RSS 1.0 (RDF) Feed Elements in Ruby Source: https://context7.com/ruby/rss/llms.txt Illustrates parsing an RSS 1.0 feed in RDF format and accessing its channel and item elements. It specifically shows how to retrieve the 'about' URI for both the channel and items, along with standard elements like title, link, and description. ```ruby require 'rss' rdf_xml = <<-XML Example RSS 1.0 http://example.org An example RSS 1.0 feed First Item http://example.org/item1 Description of first item Second Item http://example.org/item2 Description of second item XML feed = RSS::Parser.parse(rdf_xml) # Access channel with RDF about attribute puts "Channel About: #{feed.channel.about}" # RDF specific puts "Title: #{feed.channel.title}" puts "Link: #{feed.channel.link}" puts "Description: #{feed.channel.description}" # Access items feed.items.each do |item| puts "\nItem About: #{item.about}" # RDF resource URI puts " Title: #{item.title}" puts " Link: #{item.link}" puts " Description: #{item.description}" end ``` -------------------------------- ### Parse and Access Atom 1.0 Feed Elements in Ruby Source: https://context7.com/ruby/rss/llms.txt Demonstrates parsing an Atom 1.0 XML feed and accessing its various elements like title, subtitle, ID, updated date, author, links, rights, and entries. It also shows how to iterate through individual entries and access their specific details, including multiple authors and rich content. ```ruby require 'rss' atom_xml = <<-XML Example Atom Feed A sample feed demonstrating Atom urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6 2024-01-01T12:00:00Z John Doe john@example.org http://example.org/~john/ Copyright (c) 2024, John Doe Atom-Powered Robots Run Amok urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a 2024-01-01T12:00:00Z 2024-01-01T10:00:00Z Some summary text here <p>This is the full content</p> Jane Smith jane@example.org Another Article urn:uuid:2225c695-cfb8-4ebb-aaaa-80da344efa6b 2023-12-31T18:00:00Z Another article summary XML feed = RSS::Parser.parse(atom_xml) # Access feed-level properties puts "Title: #{feed.title.content}" puts "Subtitle: #{feed.subtitle.content}" puts "ID: #{feed.id.content}" puts "Updated: #{feed.updated.content}" # Access feed author if feed.author puts "Author Name: #{feed.author.name.content}" puts "Author Email: #{feed.author.email.content}" puts "Author URI: #{feed.author.uri.content}" if feed.author.uri end # Access feed links feed.links.each do |link| puts "Link: #{link.href} (rel: #{link.rel})" end puts "Rights: #{feed.rights.content}" if feed.rights # Access entries (Atom's equivalent to items) puts "\nNumber of entries: #{feed.entries.size}" feed.entries.each_with_index do |entry, i| puts "\nEntry #{i + 1}:" puts " Title: #{entry.title.content}" puts " ID: #{entry.id.content}" puts " Updated: #{entry.updated.content}" puts " Published: #{entry.published.content}" if entry.published puts " Summary: #{entry.summary.content}" if entry.summary # Access entry links entry.links.each do |link| puts " Link: #{link.href}" end # Access entry content if entry.content puts " Content Type: #{entry.content.type}" puts " Content: #{entry.content.content}" end # Access entry authors (can have multiple) entry.authors.each do |author| puts " Author: #{author.name.content}" end end ``` -------------------------------- ### Parse and Blend Multiple RSS Feeds in Ruby Source: https://context7.com/ruby/rss/llms.txt This snippet demonstrates how to read multiple RSS feed files, parse them using the RSS gem, handle potential parsing errors, merge them into a single blended feed, and save the result. It includes error handling for invalid or malformed feeds and ensures the blended feed has a limited number of recent items. ```ruby # Parse multiple feed files ['feed1.xml', 'feed2.xml', 'feed3.xml'].each do |filename| begin rss = RSS::Parser.parse(File.read(filename)) rss.output_encoding = "UTF-8" feeds << rss rescue RSS::InvalidRSSError => e # Try non-validating parse rss = RSS::Parser.parse(File.read(filename), false) feeds << rss if rss rescue RSS::Error => e puts "Error parsing #{filename}: #{e.message}" end end # Create blended feed blended = RSS::Maker.make("1.0") do |maker| maker.encoding = "UTF-8" maker.channel.about = "http://example.com/blended-feed.rdf" maker.channel.title = "Blended News Feed" maker.channel.link = "http://example.com" maker.channel.description = "Combined feed from multiple sources" # Copy all items from all source feeds feeds.each do |feed| feed.items.each do |item| # Use setup_maker to copy item with all metadata item.setup_maker(maker.items) end end # Ensure required fields maker.items.each do |item| item.title ||= "Untitled" item.link ||= "http://example.com" end # Sort items by date (most recent first) maker.items.do_sort = true # Limit to 15 most recent items maker.items.max_size = 15 end # Save blended feed File.write('blended-feed.xml', blended) puts "Blended feed created with #{blended.items.size} items" # Output: Blended feed created with 15 items ``` -------------------------------- ### Parse and Access RSS 2.0 Feed Elements in Ruby Source: https://context7.com/ruby/rss/llms.txt This snippet demonstrates parsing an XML string containing an RSS 2.0 feed and accessing its channel and item elements. It uses the RSS::Parser library and shows how to retrieve metadata like title, link, description, and publication dates. The output includes the parsed feed's XML. ```ruby require 'rss' xml = <<-XML Example News http://news.example.com Latest news updates en-us Copyright 2024 Example Corp editor@example.com webmaster@example.com Wed, 01 Jan 2024 12:00:00 GMT Wed, 01 Jan 2024 14:00:00 GMT Custom RSS Generator 1.0 60 Breaking News http://news.example.com/breaking Major announcement today reporter@example.com http://news.example.com/breaking#comments Wed, 01 Jan 2024 10:00:00 GMT http://news.example.com/breaking Technology Update http://news.example.com/tech-update New technology developments Tue, 31 Dec 2023 15:00:00 GMT XML feed = RSS::Parser.parse(xml) # Access channel properties puts "Version: #{feed.feed_version}" # => "2.0" puts "Title: #{feed.channel.title}" # => "Example News" puts "Link: #{feed.channel.link}" # => "http://news.example.com" puts "Description: #{feed.channel.description}" # => "Latest news updates" puts "Language: #{feed.channel.language}" # => "en-us" puts "Copyright: #{feed.channel.copyright}" # => "Copyright 2024 Example Corp" puts "Editor: #{feed.channel.managingEditor}" # => "editor@example.com" puts "Webmaster: #{feed.channel.webMaster}" # => "webmaster@example.com" puts "Pub Date: #{feed.channel.pubDate}" # => Wed, 01 Jan 2024 12:00:00 GMT puts "Generator: #{feed.channel.generator}" # => "Custom RSS Generator 1.0" puts "TTL: #{feed.channel.ttl} minutes" # => 60 minutes # Access items (convenience accessor) puts "\nNumber of items: #{feed.items.size}" # => 2 feed.items.each_with_index do |item, i| puts "\nItem #{i + 1}:" puts " Title: #{item.title}" puts " Link: #{item.link}" puts " Description: #{item.description}" puts " Author: #{item.author}" if item.author puts " Comments: #{item.comments}" if item.comments puts " Date: #{item.date}" # Access GUID if item.guid puts " GUID: #{item.guid.content}" puts " Is PermaLink: #{item.guid.PermaLink?}" end end # Set output encoding for non-ASCII content feed.output_encoding = "UTF-8" puts "\nXML Output:\n#{feed.to_s}" ``` -------------------------------- ### Generate RSS 1.0 Feed in Ruby Source: https://context7.com/ruby/rss/llms.txt This code generates an RSS 1.0 feed in RDF format using RSS::Maker. It sets channel details, adds an item, and configures item sorting and maximum size. The generated feed is an XML string. ```ruby require 'rss' rss10 = RSS::Maker.make("1.0") do |maker| maker.channel.about = "http://example.com/feed.rdf" maker.channel.title = "Example Feed" maker.channel.link = "http://example.com" maker.channel.description = "An example RSS 1.0 feed" maker.items.new_item do |item| item.title = "Sample Item" item.link = "http://example.com/item1" item.description = "This is a sample item" end # RSS 1.0 specific: items are sorted maker.items.do_sort = true maker.items.max_size = 15 end # Output is valid XML that can be saved or served # File.write('feed.xml', rss10) # Assuming 'rss' was a typo and should be 'rss10' puts rss10 ``` -------------------------------- ### Parse RSS/Atom Feeds with RSS::Parser Source: https://context7.com/ruby/rss/llms.txt Parses RSS and Atom feeds from URLs, files, or XML strings using `RSS::Parser.parse`. Supports validation, unknown element handling, and multiple XML parsers. It returns a parsed feed object, and errors can be caught using `RSS::InvalidRSSError` or `RSS::Error`. ```ruby require 'rss' # Parse from URL feed = RSS::Parser.parse('https://www.ruby-lang.org/en/feeds/news.rss') # Parse from file path feed = RSS::Parser.parse('/path/to/feed.xml') # Parse XML string directly xml_string = <<-XML My Blog http://example.com Latest posts from my blog First Post http://example.com/first This is my first post Mon, 01 Jan 2024 12:00:00 GMT XML feed = RSS::Parser.parse(xml_string) # Access parsed data puts "Feed Title: #{feed.channel.title}" puts "Feed Link: #{feed.channel.link}" puts "Feed Description: #{feed.channel.description}" puts "\nItems:" feed.items.each do |item| puts "- #{item.title}" puts " URL: #{item.link}" puts " Published: #{item.pubDate}" puts " Description: #{item.description}" end # Parse with options using hash syntax feed = RSS::Parser.parse( xml_string, validate: false, # Disable validation ignore_unknown_element: true # Ignore unrecognized tags ) # Parse with positional arguments feed = RSS::Parser.parse(xml_string, true, true) # Handle parsing errors begin feed = RSS::Parser.parse(xml_string) rescue RSS::InvalidRSSError => e # Try non-validating parse for malformed feeds feed = RSS::Parser.parse(xml_string, false) rescue RSS::Error => e puts "Failed to parse feed: #{e.message}" end # Output: # Feed Title: My Blog # Feed Link: http://example.com # Feed Description: Latest posts from my blog # # Items: # - First Post # URL: http://example.com/first # Published: Mon, 01 Jan 2024 12:00:00 GMT # Description: This is my first post ``` -------------------------------- ### Access iTunes Metadata from RSS Channel and Items (Ruby) Source: https://context7.com/ruby/rss/llms.txt This code snippet shows how to access specific iTunes metadata for both the channel and individual items within an RSS feed. It checks for the existence of certain methods before accessing them to prevent errors. Dependencies include the 'rss' gem. ```ruby puts "Podcast Title: #{feed.channel.title}" if feed.channel.respond_to?(:itunes_author) puts "iTunes Author: #{feed.channel.itunes_author}" puts "iTunes Summary: #{feed.channel.itunes_summary}" puts "iTunes Subtitle: #{feed.channel.itunes_subtitle}" puts "iTunes Explicit: #{feed.channel.itunes_explicit}" puts "iTunes Keywords: #{feed.channel.itunes_keywords}" puts "iTunes Type: #{feed.channel.itunes_type}" if feed.channel.itunes_image puts "iTunes Artwork: #{feed.channel.itunes_image.href}" end if feed.channel.itunes_owner puts "Owner Name: #{feed.channel.itunes_owner.itunes_name}" puts "Owner Email: #{feed.channel.itunes_owner.itunes_email}" end end # Access episode iTunes metadata feed.items.each_with_index do |item, i| puts "\nEpisode #{i + 1}: #{item.title}" if item.respond_to?(:itunes_author) puts " iTunes Author: #{item.itunes_author}" end if item.respond_to?(:itunes_duration) puts " Duration: #{item.itunes_duration}" end if item.respond_to?(:itunes_episode) puts " Episode Number: #{item.itunes_episode}" end if item.respond_to?(:itunes_season) puts " Season: #{item.itunes_season}" end if item.respond_to?(:itunes_episode_type) puts " Episode Type: #{item.itunes_episode_type}" end if item.respond_to?(:itunes_explicit) puts " Explicit: #{item.itunes_explicit}" end if item.respond_to?(:itunes_image) puts " Episode Art: #{item.itunes_image.href}" end end ``` -------------------------------- ### Access Content Encoded from RSS Feed (Ruby) Source: https://context7.com/ruby/rss/llms.txt This code demonstrates how to parse an RSS feed that includes the 'content:encoded' module and access the full HTML content of items. It requires the 'rss' gem and parses an XML string containing the content. The output includes the title, description, and the full HTML content if available. ```ruby require 'rss' xml_with_content = <<-XML My Blog http://blog.example.com Personal blog My First Post http://blog.example.com/first-post A short summary of the post Welcome to My Blog

This is the full HTML content of my post.

Post image

Multiple paragraphs with links.

]]>
XML feed = RSS::Parser.parse(xml_with_content) feed.items.each do |item| puts "Title: #{item.title}" puts "Description: #{item.description}" # Access full HTML content if item.respond_to?(:content_encoded) puts "\nFull Content:" puts item.content_encoded end end ``` -------------------------------- ### Ruby RSS Feed Error Handling and Validation Source: https://context7.com/ruby/rss/llms.txt This Ruby code defines a function `parse_feed` to robustly parse RSS feeds. It includes strategies for handling various errors such as invalid RSS, malformed XML, and general parsing errors by attempting both validating and non-validating parses. It also shows safe access to feed data and setting output encoding with error checks. ```ruby require 'rss' def parse_feed(filename) xml_content = File.read(filename) # Try strict validation first begin feed = RSS::Parser.parse(xml_content, validate: true) puts "✓ Feed parsed successfully with validation" return feed rescue RSS::InvalidRSSError => e puts "✗ Validation error: #{e.message}" puts " Attempting non-validating parse..." # Try without validation for malformed feeds begin feed = RSS::Parser.parse(xml_content, validate: false) puts "✓ Feed parsed without validation" return feed rescue RSS::Error => e puts "✗ Parse error: #{e.message}" return nil end rescue RSS::NotWellFormedError => e puts "✗ XML not well-formed: #{e.message}" puts " Line: #{e.line}" if e.line puts " Element: #{e.element}" if e.element return nil rescue RSS::Error => e puts "✗ RSS error: #{e.class} - #{e.message}" return nil rescue => e puts "✗ Unexpected error: #{e.class} - #{e.message}" return nil end end # Parse with error handling feed = parse_feed('test-feed.xml') if feed # Set encoding with error handling begin feed.output_encoding = "UTF-8" rescue RSS::UnknownConversionError => e puts "Warning: Character encoding conversion failed: #{e.message}" end # Access feed data safely begin puts "Feed: #{feed.channel.title}" puts "Items: #{feed.items.size}" feed.items.each do |item| puts "- #{item.title || 'No title'}" end rescue => e puts "Error accessing feed data: #{e.message}" end else puts "Failed to parse feed" end # Example output for invalid feed: # ✗ Validation error: Missing required element: # Attempting non-validating parse... # ✓ Feed parsed without validation # Feed: Example Feed # Items: 5 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.