class SimpleRSS
Constants
- VERSION
Attributes
entries[R]
items[R]
source[R]
Public Class Methods
new(source, options={})
click to toggle source
# File lib/simple-rss.rb, line 40 def initialize(source, options={}) @source = source.respond_to?(:read) ? source.read : source.to_s @items = Array.new @options = Hash.new.update(options) parse end
parse(source, options={})
click to toggle source
The strict attribute is for compatibility with Ruby's standard RSS parser
# File lib/simple-rss.rb, line 67 def parse(source, options={}) new source, options end
Public Instance Methods
channel()
click to toggle source
# File lib/simple-rss.rb, line 48 def channel() self end
Also aliased as: feed
Private Instance Methods
clean_content(tag, attrs, content)
click to toggle source
# File lib/simple-rss.rb, line 138 def clean_content(tag, attrs, content) content = content.to_s case tag when :pubDate, :lastBuildDate, :published, :updated, :expirationDate, :modified, :'dc:date' Time.parse(content) rescue unescape(content) when :author, :contributor, :skipHours, :skipDays unescape(content.gsub(/<.*?>/,'')) else content.empty? && "#{attrs} " =~ /href=['"]?([^'"]*)['" ]/mi ? $1.strip : unescape(content) end end
clean_tag(tag)
click to toggle source
# File lib/simple-rss.rb, line 150 def clean_tag(tag) tag.to_s.gsub(':','_').intern end
parse()
click to toggle source
# File lib/simple-rss.rb, line 74 def parse raise SimpleRSSError, "Poorly formatted feed" unless @source =~ %r{<(channel|feed).*?>.*?</(channel|feed)>}mi # Feed's title and link feed_content = $1 if @source =~ %r{(.*?)<(rss:|atom:)?(item|entry).*?>.*?</(rss:|atom:)?(item|entry)>}mi @@feed_tags.each do |tag| if feed_content && feed_content =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)</(rss:|atom:)?#{tag}>}mi nil elsif feed_content && feed_content =~ %r{<(rss:|atom:)?#{tag}(.*?)\/\s*>}mi nil elsif @source =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)</(rss:|atom:)?#{tag}>}mi nil elsif @source =~ %r{<(rss:|atom:)?#{tag}(.*?)\/\s*>}mi nil end if $2 || $3 tag_cleaned = clean_tag(tag) instance_variable_set("@#{ tag_cleaned }", clean_content(tag, $2, $3)) self.class.class_eval("attr_reader :#{ tag_cleaned }") end end # RSS items' title, link, and description @source.scan( %r{<(rss:|atom:)?(item|entry)([\s][^>]*)?>(.*?)</(rss:|atom:)?(item|entry)>}mi ) do |match| item = Hash.new @@item_tags.each do |tag| if tag.to_s.include?("+") tag_data = tag.to_s.split("+") tag = tag_data[0] rel = tag_data[1] if match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)rel=['"]#{rel}['"](.*?)>(.*?)</(rss:|atom:)?#{tag}>}mi nil elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)rel=['"]#{rel}['"](.*?)/\s*>}mi nil end item[clean_tag("#{tag}+#{rel}")] = clean_content(tag, $3, $4) if $3 || $4 elsif tag.to_s.include?("#") tag_data = tag.to_s.split("#") tag = tag_data[0] attrib = tag_data[1] if match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)#{attrib}=['"](.*?)['"](.*?)>(.*?)</(rss:|atom:)?#{tag}>}mi nil elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)#{attrib}=['"](.*?)['"](.*?)/\s*>}mi nil end item[clean_tag("#{tag}_#{attrib}")] = clean_content(tag, attrib, $3) if $3 else if match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)</(rss:|atom:)?#{tag}>}mi nil elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)/\s*>}mi nil end item[clean_tag(tag)] = clean_content(tag, $2, $3) if $2 || $3 end end def item.method_missing(name, *args) self[name] end @items << item end end
unescape(content)
click to toggle source
# File lib/simple-rss.rb, line 154 def unescape(content) if content.respond_to?(:force_encoding) && content.force_encoding("binary") =~ /([^-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]%)/n then CGI.unescape(content).gsub(/(<!\[CDATA\[|\]\]>)/,'').strip else content.gsub(/(<!\[CDATA\[|\]\]>)/,'').strip end end