Class: RSSFeed::RSSChannel

Inherits:
Object show all
Defined in:
lib/rss_feed/rss_channel.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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

#docObject (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

Yields:

  • (channel)


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

#categoryObject

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

#cloudObject

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 (optional).



83
84
85
# File 'lib/rss_feed/rss_channel.rb', line 83

def copyright
  @doc.at_xpath("rss/channel/copyright").try(:content)
end

#descriptionObject

Description (required).



73
74
75
# File 'lib/rss_feed/rss_channel.rb', line 73

def description
  @doc.at_xpath("rss/channel/description").content
end

#docsObject

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

#generatorObject

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

#imageObject

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

#itemsObject

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

#languageObject

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_dateObject

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 (required).



68
69
70
# File 'lib/rss_feed/rss_channel.rb', line 68

def link 
  @doc.at_xpath("rss/channel/link").content
end

#managing_editorObject

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_searchObject

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_dateObject

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

#ratingObject

PICS rating (optional).



143
144
145
# File 'lib/rss_feed/rss_channel.rb', line 143

def rating
  @doc.at_xpath("rss/channel/rating").try(:content)
end

#skip_daysObject

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_hoursObject

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_inputObject

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

#titleObject

Title (required).



63
64
65
# File 'lib/rss_feed/rss_channel.rb', line 63

def title
  @doc.at_xpath("rss/channel/title").content
end

#ttlObject

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