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 feedFirst Item
http://example.org/item1
Description of first itemSecond 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 FeedA sample feed demonstrating Atomurn:uuid:60a76c80-d399-11d9-b91C-0003939e0af62024-01-01T12:00:00ZJohn Doejohn@example.orghttp://example.org/~john/Copyright (c) 2024, John DoeAtom-Powered Robots Run Amokurn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a2024-01-01T12:00:00Z2024-01-01T10:00:00ZSome summary text here<p>This is the full content</p>Jane Smithjane@example.orgAnother Articleurn:uuid:2225c695-cfb8-4ebb-aaaa-80da344efa6b2023-12-31T18:00:00ZAnother 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 updatesen-usCopyright 2024 Example Corpeditor@example.comwebmaster@example.comWed, 01 Jan 2024 12:00:00 GMTWed, 01 Jan 2024 14:00:00 GMTCustom RSS Generator 1.060Breaking News
http://news.example.com/breaking
Major announcement todayreporter@example.comhttp://news.example.com/breaking#commentsWed, 01 Jan 2024 10:00:00 GMThttp://news.example.com/breakingTechnology Update
http://news.example.com/tech-update
New technology developmentsTue, 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 blogFirst Post
http://example.com/first
This is my first postMon, 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 blogMy First Post
http://blog.example.com/first-post
A short summary of the postWelcome to My Blog
]]>
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.