class RSSFeed::RSSChannel
RSSFeed::RSSChannel
is the central place for working with feeds in the RSS format.
Opening an RSS feed from the network or a file system is done like this:
feed = RSSFeed::RSSChannel.open(open("http://example.com/rss.xml"))
If you have a file you should do:
f = File.open("rss.xml") feed = RSSFeed::RSSChannel.open(f) f.close
If you have an XML string you can do:
feed = RSSFeed::RSSChannel.open("<rss version='1.0'> ...")
One can open and parse the feed like so:
RSSFeed::RSSChannel.open(...) do |feed| puts feed.title feed.items do |item| puts item.title end end
You can access OpenSearch
extensions by using RSSChannel.open_search
. Access to other embedded XML types are available by using RSSChannel.doc
+ directly. It's a Nokogiri::XML
instance.
RSSFeed
uses Nokogiri for parsing.
Attributes
Public Class Methods
# File lib/rss_feed/rss_channel.rb, line 51 def initialize(doc) @doc = doc end
# File lib/rss_feed/rss_channel.rb, line 55 def self.open(string_or_io, url = nil, encoding = nil) doc = Nokogiri::XML(string_or_io, url, encoding) channel = RSSChannel.new(doc) yield channel if block_given? channel end
Public Instance Methods
Category (optional).
# File lib/rss_feed/rss_channel.rb, line 107 def category if node = @doc.at_xpath("rss/channel/category") RSSCategory.new(node) end end
Cloud (optional)
# File lib/rss_feed/rss_channel.rb, line 124 def cloud if node = @doc.at_xpath("rss/channel/cloud") RSSCloud.new(node) end end
Copyright (optional).
# File lib/rss_feed/rss_channel.rb, line 83 def copyright @doc.at_xpath("rss/channel/copyright").try(:content) end
Description (required).
# File lib/rss_feed/rss_channel.rb, line 73 def description @doc.at_xpath("rss/channel/description").content end
Documentation link (optional).
# File lib/rss_feed/rss_channel.rb, line 119 def docs @doc.at_xpath("rss/channel/docs").try(:content) end
Generator (optional).
# File lib/rss_feed/rss_channel.rb, line 114 def generator @doc.at_xpath("rss/channel/generator").try(:content) end
Image (optional).
# File lib/rss_feed/rss_channel.rb, line 136 def image if node = @doc.at_xpath("rss/channel/image") RSSImage.new(node) end end
Array of channel items (optional).
# File lib/rss_feed/rss_channel.rb, line 165 def items nodes = @doc.xpath("rss/channel/item") || [] nodes.map { |node| RSSChannelItem.new(node) } end
Language (optional).
# File lib/rss_feed/rss_channel.rb, line 78 def language @doc.at_xpath("rss/channel/language").try(:content) end
Last build date (optional).
# File lib/rss_feed/rss_channel.rb, line 100 def last_build_date if date = @doc.at_xpath("rss/channel/lastBuildDate").try(:content) Time.rfc822(date) end end
Link (required).
# File lib/rss_feed/rss_channel.rb, line 68 def link @doc.at_xpath("rss/channel/link").content end
Managing editor (optional).
# File lib/rss_feed/rss_channel.rb, line 88 def managing_editor @doc.at_xpath("rss/channel/managingEditor").try(:content) end
Open Search extensions (optional)
# File lib/rss_feed/rss_channel.rb, line 171 def open_search @open_search ||= OpenSearch.new(@doc) end
Publication date (optional).
# File lib/rss_feed/rss_channel.rb, line 93 def pub_date if date = @doc.at_xpath("rss/channel/pubDate").try(:content) Time.rfc822(date) end end
PICS rating (optional).
# File lib/rss_feed/rss_channel.rb, line 143 def rating @doc.at_xpath("rss/channel/rating").try(:content) end
Hint for aggregators telling them which days they can skip (optional).
# File lib/rss_feed/rss_channel.rb, line 160 def skip_days @doc.at_xpath("rss/channel/skipDays").try(:content).nonzero? end
Hint for aggregators telling them which hours they can skip (optional).
# File lib/rss_feed/rss_channel.rb, line 155 def skip_hours @doc.at_xpath("rss/channel/skipHours").try(:content).nonzero? end
Text input box (optional).
# File lib/rss_feed/rss_channel.rb, line 148 def text_input if node = @doc.at_xpath("rss/channel/textInput") RSSTextInput.new(node) end end
Title (required).
# File lib/rss_feed/rss_channel.rb, line 63 def title @doc.at_xpath("rss/channel/title").content end
Time to live in minutes (optional).
# File lib/rss_feed/rss_channel.rb, line 131 def ttl @doc.at_xpath("rss/channel/ttl").try(:content).nonzero? end