class SimpleRSS

Constants

VERSION

Attributes

entries[R]
items[R]
source[R]

Public Class Methods

feed_tags() click to toggle source
# File lib/simple-rss.rb, line 52
def feed_tags
        @@feed_tags
end
feed_tags=(ft) click to toggle source
# File lib/simple-rss.rb, line 55
def feed_tags=(ft)
        @@feed_tags = ft
end
item_tags() click to toggle source
# File lib/simple-rss.rb, line 59
def item_tags
        @@item_tags
end
item_tags=(it) click to toggle source
# File lib/simple-rss.rb, line 62
def item_tags=(it)
        @@item_tags = it
end
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
feed()
Alias for: channel

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=['"]?([^'"]*)['" ]/i ? $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)>}i
  
        # Feed's title and link
        feed_content = $1 if @source =~ %r{(.*?)<(rss:|atom:)?(item|entry).*?>.*?</(rss:|atom:)?(item|entry)>}i
        
        @@feed_tags.each do |tag|
                if feed_content && feed_content =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)</(rss:|atom:)?#{tag}>}i
                        nil
                elsif feed_content && feed_content =~ %r{<(rss:|atom:)?#{tag}(.*?)\/\s*>}i
                        nil
                elsif @source =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)</(rss:|atom:)?#{tag}>}i
                        nil
                elsif @source =~ %r{<(rss:|atom:)?#{tag}(.*?)\/\s*>}i
                        nil
                end
                
                if $2 || $3
tag_cleaned = clean_tag(tag)
instance_variable_set("@#{ tag_cleaned }", clean_content(tag, $2, $3))
self.class.send(:attr_reader, tag_cleaned)
                end
        end

        # RSS items' title, link, and description
        @source.scan( %r{<(rss:|atom:)?(item|entry)([\s][^>]*)?>(.*?)</(rss:|atom:)?(item|entry)>}i ) 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}>}i
    nil
                        elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)rel=['"]#{rel}['"](.*?)/\s*>}i
                          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}>}i
    nil
                        elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)#{attrib}=['"](.*?)['"](.*?)/\s*>}i
                          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}>}i
                                nil
                        elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)/\s*>}i
                                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 =~ /([^-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]%)/ then
              CGI.unescape(content).gsub(/(<!\[CDATA\[|\]\]>)/,'').strip
      else
              content.gsub(/(<!\[CDATA\[|\]\]>)/,'').strip
      end
end