Class: RSSFeed::RSSChannel
Overview
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.
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Class Method Summary collapse
Instance Method Summary collapse
-
#category ⇒ Object
Category (optional).
-
#cloud ⇒ Object
Cloud (optional).
-
#copyright ⇒ Object
Copyright (optional).
-
#description ⇒ Object
Description (required).
-
#docs ⇒ Object
Documentation link (optional).
-
#generator ⇒ Object
Generator (optional).
-
#image ⇒ Object
Image (optional).
-
#initialize(doc) ⇒ RSSChannel
constructor
A new instance of RSSChannel.
-
#items ⇒ Object
Array of channel items (optional).
-
#language ⇒ Object
Language (optional).
-
#last_build_date ⇒ Object
Last build date (optional).
-
#link ⇒ Object
Link (required).
-
#managing_editor ⇒ Object
Managing editor (optional).
-
#open_search ⇒ Object
Open Search extensions (optional).
-
#pub_date ⇒ Object
Publication date (optional).
-
#rating ⇒ Object
PICS rating (optional).
-
#skip_days ⇒ Object
Hint for aggregators telling them which days they can skip (optional).
-
#skip_hours ⇒ Object
Hint for aggregators telling them which hours they can skip (optional).
-
#text_input ⇒ Object
Text input box (optional).
-
#title ⇒ Object
Title (required).
-
#ttl ⇒ Object
Time to live in minutes (optional).
Constructor Details
#initialize(doc) ⇒ RSSChannel
51 52 53 |
# File 'lib/rss_feed/rss_channel.rb', line 51 def initialize(doc) @doc = doc end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
49 50 51 |
# File 'lib/rss_feed/rss_channel.rb', line 49 def doc @doc end |
Class Method Details
.open(string_or_io, url = nil, encoding = nil) {|channel| ... } ⇒ Object
55 56 57 58 59 60 |
# 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 |
Instance Method Details
#category ⇒ Object
Category (optional).
107 108 109 110 111 |
# 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 ⇒ Object
Cloud (optional)
124 125 126 127 128 |
# 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 ⇒ Object
Copyright (optional).
83 84 85 |
# File 'lib/rss_feed/rss_channel.rb', line 83 def copyright @doc.at_xpath("rss/channel/copyright").try(:content) end |
#description ⇒ Object
Description (required).
73 74 75 |
# File 'lib/rss_feed/rss_channel.rb', line 73 def description @doc.at_xpath("rss/channel/description").content end |
#docs ⇒ Object
Documentation link (optional).
119 120 121 |
# File 'lib/rss_feed/rss_channel.rb', line 119 def docs @doc.at_xpath("rss/channel/docs").try(:content) end |
#generator ⇒ Object
Generator (optional).
114 115 116 |
# File 'lib/rss_feed/rss_channel.rb', line 114 def generator @doc.at_xpath("rss/channel/generator").try(:content) end |
#image ⇒ Object
Image (optional).
136 137 138 139 140 |
# File 'lib/rss_feed/rss_channel.rb', line 136 def image if node = @doc.at_xpath("rss/channel/image") RSSImage.new(node) end end |
#items ⇒ Object
Array of channel items (optional).
165 166 167 168 |
# 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 ⇒ Object
Language (optional).
78 79 80 |
# File 'lib/rss_feed/rss_channel.rb', line 78 def language @doc.at_xpath("rss/channel/language").try(:content) end |
#last_build_date ⇒ Object
Last build date (optional).
100 101 102 103 104 |
# 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 ⇒ Object
Link (required).
68 69 70 |
# File 'lib/rss_feed/rss_channel.rb', line 68 def link @doc.at_xpath("rss/channel/link").content end |
#managing_editor ⇒ Object
Managing editor (optional).
88 89 90 |
# File 'lib/rss_feed/rss_channel.rb', line 88 def managing_editor @doc.at_xpath("rss/channel/managingEditor").try(:content) end |
#open_search ⇒ Object
Open Search extensions (optional)
171 172 173 |
# File 'lib/rss_feed/rss_channel.rb', line 171 def open_search @open_search ||= OpenSearch.new(@doc) end |
#pub_date ⇒ Object
Publication date (optional).
93 94 95 96 97 |
# 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 |
#rating ⇒ Object
PICS rating (optional).
143 144 145 |
# File 'lib/rss_feed/rss_channel.rb', line 143 def @doc.at_xpath("rss/channel/rating").try(:content) end |
#skip_days ⇒ Object
Hint for aggregators telling them which days they can skip (optional).
160 161 162 |
# File 'lib/rss_feed/rss_channel.rb', line 160 def skip_days @doc.at_xpath("rss/channel/skipDays").try(:content).nonzero? end |
#skip_hours ⇒ Object
Hint for aggregators telling them which hours they can skip (optional).
155 156 157 |
# File 'lib/rss_feed/rss_channel.rb', line 155 def skip_hours @doc.at_xpath("rss/channel/skipHours").try(:content).nonzero? end |
#text_input ⇒ Object
Text input box (optional).
148 149 150 151 152 |
# 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 ⇒ Object
Title (required).
63 64 65 |
# File 'lib/rss_feed/rss_channel.rb', line 63 def title @doc.at_xpath("rss/channel/title").content end |
#ttl ⇒ Object
Time to live in minutes (optional).
131 132 133 |
# File 'lib/rss_feed/rss_channel.rb', line 131 def ttl @doc.at_xpath("rss/channel/ttl").try(:content).nonzero? end |